strawberry-graphql / strawberry-django

Strawberry GraphQL Django extension
https://strawberry.rocks/docs/django
MIT License
415 stars 120 forks source link

Nested Query Fields ("folders" to better organize related fields) #580

Open stygmate opened 4 months ago

stygmate commented 4 months ago

Is there a simple way to move fields that are in base Query Into subfields to namespace/organize them ? (I keep running into issues. 😓)

Upvote & Fund

Fund with Polar

bellini666 commented 4 months ago

Can you give more information about this, with examples if possible?

stygmate commented 4 months ago

@bellini666

with this query:

query MyQuery {
  param {
    equip {
      edges {
        node {
          id
        }
      }
    }
  }
}

this don't work:

@strawberry.type
class Param:
    equip: ListConnectionWithTotalCount[types_.Equip] = strawberry_django.connection()

@strawberry.type
class Query:
    param: Param = strawberry.field()

but this (with a custom resolver) work :

@strawberry.type
class Param:
    equip: ListConnectionWithTotalCount[types_.Equip] = strawberry_django.connection()

@strawberry.type
class Query:
    @strawberry.field()
    def param(self, info) -> Param:
        return Param(equip=types_.Equip.__strawberry_django_definition__.model.objects)

( i get the model manager from strawberry_django_definition 🤪 but i think you have it )

bellini666 commented 4 months ago

Is Param a django type? In your example you wrote it as @strawberry.type, but if it is using @strawberry_django.type(ParamModel), then you can do:

@strawberry.type
class Query:
    param: Param = strawberry_django.field()
    # or if you are using relay and Param inherits from Node
    param: Param = strawberry_django.node()

Let me know if that works

stygmate commented 4 months ago

@bellini666

No, Param is not a Django type, there's no django model named Param. param field (using Param type) is meant to be a "folder" to better organize related fields of my api.

stygmate commented 3 months ago

@bellini666 i see that you have taged this "help wanted", I renamed this issue by adding ("folders" to better organize related fields) to make it more understandable.