Closed paulsermon-gemfair closed 2 years ago
I've just worked this out myself, so no worries. I'm going to post some notes in case someone else needs guidance.
When starting with this library you may have been tempted to use DjangoFilterPaginateListField as that looks like it does everything you need (which is what I did), but it is actually the combination of DjangoListObjectField and a customised DjangoListObjectType that gives you everything and a total count. eg. Set up your model Type:
class MyModelType(DjangoObjectType):
class Meta:
model = MyModel
fields = (your fields here)
filter_fields = {
All your filters here
}
Set up your model list type
class MyModelListType(DjangoListObjectType):
class Meta:
model = MyModel
Then set up your main query
class Query(graphene.ObjectType):
my_models = DjangoListObjectField(
MyModelListType,
description="Lists all my models",
)
Your gql query will look like this
query{
myModels {
results {
id
}
totalCount
}
}
and you'll get back
{
"data": {
"myModels": {
"results": [
{
id: 1
}
},
{
id: 2
}
],
"totalCount": 2
}
}
}
Thanks for figuring this out! It would be nice to have this in the docs.
@paulsermon-gemfair were you able to get foreign fields to work with DjangoListObjectType
?
@paulsermon-gemfair were you able to get foreign fields to work with
DjangoListObjectType
?
This should be largely automatic as long as the foreign key model has a type defined. I think.
Thats what I thought as well, but I haven't been able to get it to work for me 🤷
You can try using the DjangoFilterPaginateListField within a type eg:
class MyObjectType(DjangoObjectType):
things = DjangoFilterPaginateListField(
ThingObjectType,
)
that seems to work automatically for me too.
Hi, sorry if this is obvious, but I'm trying to work out if the total count of the result is available in a paginated result. It is mentioned in the Readme as totalCount, but using DjangoFilterPaginateListField doesn't seem to provide it?
Maybe I have misunderstood this, and perhaps I need to provide another resolver for the count, but how can I make use of the existing filter definitions that I can use through DjangoFilterPaginateListField?
Thanks for your help