in official docs: https://docs.graphene-python.org/projects/django/en/latest/queries/#default-queryset. In practice nothing happens. Please help
Here's the unit test for this feature: https://github.com/graphql-python/graphene-django/blob/master/graphene_django/tests/test_query.py#L1026
Maybe you can show us how you defined your object type?
@zbyte64 a lot of thanks for your feedback!
class ContactType(DjangoObjectType):
class Meta:
model = Contact
@classmethod
def get_queryset(cls, queryset, info):
return queryset.filter(kind='PHONE')
class ContactQuery(graphene.ObjectType):
contacts = graphene.List(ContactType)
def resolve_contacts(self, info, **kwargs):
return Contact.objects.all()
The query:
{
contacts {
id
kind
value
}
}
returns:
{
"data": {
"contacts": [
{
"id": "1",
"kind": "INSTAGRAM",
"value": "technodomkz"
},
{
"id": "3",
"kind": "YOUTUBE",
"value": "UCF-HjvMMvgnhXhO4shk9i9Q"
},
{
"id": "4",
"kind": "FACEBOOK",
"value": "technodomkz"
},
{
"id": "5",
"kind": "TWITTER",
"value": "technodom_kz"
},
{
"id": "6",
"kind": "PHONE",
"value": "88000801111"
},
{
"id": "7",
"kind": "PHONE",
"value": "87272799999"
},
{
"id": "8",
"kind": "EMAIL",
"value": "[email protected]"
},
{
"id": "2",
"kind": "WEBSITE",
"value": "www.technodom.kz"
}
]
}
}
instead of:
{
"data": {
"contacts": [
{
"id": "6",
"kind": "PHONE",
"value": "88000801111"
},
{
"id": "7",
"kind": "PHONE",
"value": "87272799999"
}
]
}
}
Try swapping out List for DjangoConnectionField.
Looks like we need to document that get_queryset works with the DjangoConnectionField and that non-relay fields like List don't have the same magic.
It works DjangoConnectionField, thanks! Yeah it needs to correct the document or just do same magic with List ))
@zbyte64 is it generally possible to do this with graphene.List? For example i need to fetch all companies with flag that authorized user subscribed it or not.
class SubscriptionType(DjangoObjectType):
class Meta:
model = Subscription
@classmethod
def get_queryset(cls, queryset, info):
return queryset.filter(client=info.context.user.client)
and then, I could request:
query {
companies {
id
otherFields,
subscriptions {
id
subscribedTime
}
}
}
and I will know that if company has not empty field 'subscriptions', auth user subscribed this company.
Of course I can set the queryset on the endpoint like:
class CompanyQuery(graphene.ObjectType):
companies = DjangoListField(CompanyType)
def resolve_companies(self, info, **kwargs):
return Company.objects.prefetch_related(
Prefetch('subscriptions', queryset=Subscription.objects.filter(client=info.context.user.client))
)
But what about queries like:
query {
programs {
company {
subscribers {
id
}
}
}
points {
company {
subscribers {
id
}
}
}
}
I have some more models that have relations with company model. It's crazy do the filter to querysets for all endpoints(programs, points, etc)
Perhaps there is another way to the solution of this problem? Cause I quite recently started working with graphene !?
I think the resolve_foo methods gets called when you do nested queries. So you can do auth restriction there for each one. Is there a reason why you don't want to use DjangoFilterConnectionField?
@dan-klasson thank you for feedback. The main reason why i don't want use DjangoFilterConnectionField is edges-node conception
@abtuleshov I've create a PR that extends DjangoListField to behaviour how you would expect in your examples: https://github.com/graphql-python/graphene-django/pull/732
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
@jkimbo @abtuleshov is this indeed fixed with PR #732? I can't get it to work:
class CurrencyType(DjangoObjectType):
class Meta:
model = Currency
@classmethod
def get_queryset(cls, queryset, info):
print(cls)
print(queryset)
return queryset.filter(symbol__isnull=False)
all_currencies = graphene.List(CurrencyType)
def resolve_all_currencies(self, info):
return Currency.objects.all()
The get_queryset method is never called, and the filter is not applied.
I think the
resolve_foomethods gets called when you do nested queries. So you can do auth restriction there for each one.
@dan-klasson, unfortunately, the resolve_foo methods are not called on related_name fields.
@pors yeah before it's worked fine with, with PR by @jkimbo, but with the latest updates, get_queryset just began to ignore. It's a shame that everything is done for the sake of relay, but ordinary people have to hardcode )
@abtuleshov thanks for the reply! Do you know which release or commit broke it again? Maybe I can fix it and create a PR.
I abandoned that project for a some time, I think it was a 2.5.0 or 2.6.0 version
Most helpful comment
@zbyte64 is it generally possible to do this with graphene.List? For example i need to fetch all companies with flag that authorized user subscribed it or not.
and then, I could request:
and I will know that if company has not empty field 'subscriptions', auth user subscribed this company.
Of course I can set the queryset on the endpoint like:
But what about queries like:
I have some more models that have relations with company model. It's crazy do the filter to querysets for all endpoints(programs, points, etc)
Perhaps there is another way to the solution of this problem? Cause I quite recently started working with graphene !?