Coder Social home page Coder Social logo

Comments (7)

paulsermon-gemfair avatar paulsermon-gemfair commented on June 2, 2024 1

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
    }
  }
}

from graphene-django-extras.

paulsermon-gemfair avatar paulsermon-gemfair commented on June 2, 2024

I've just worked this out myself, so no worries. I'm going to post some notes in case someone else needs guidance.

from graphene-django-extras.

fireteam99 avatar fireteam99 commented on June 2, 2024

Thanks for figuring this out! It would be nice to have this in the docs.

from graphene-django-extras.

fireteam99 avatar fireteam99 commented on June 2, 2024

@paulsermon-gemfair were you able to get foreign fields to work with DjangoListObjectType?

from graphene-django-extras.

paulsermon-gemfair avatar paulsermon-gemfair commented on June 2, 2024

@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.

from graphene-django-extras.

fireteam99 avatar fireteam99 commented on June 2, 2024

Thats what I thought as well, but I haven't been able to get it to work for me 🤷

from graphene-django-extras.

paulsermon-gemfair avatar paulsermon-gemfair commented on June 2, 2024

You can try using the DjangoFilterPaginateListField within a type eg:

class MyObjectType(DjangoObjectType):

    things = DjangoFilterPaginateListField(
        ThingObjectType,
    )

that seems to work automatically for me too.

from graphene-django-extras.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.