2

Can we do nested queries in Strawberry (graphql python pkg)?

Essentially, the same question that was posted here: GraphQL nested query definition

Instead of having

peopleList, peopleSingle, peopleEdit, peopleAdd, peopleDelete
query test {
  people {
    list {
      id
      name
    }
    single(id: 123) {
      id
      name
    }
  }
  company {
    list {
      id
      name
    }
    single(id: 456) {
      id
      name
    }
  }
}

2 Answers 2

2

You can do it by defining a new Strawberry type:

@strawberry.type
class Query:

    @strawberry.field
    def person(self) -> PersonQuery: 
        return PersonQuery()

and


@strawberry.type
class PersonQuery:

    @strawberry.field
    async def all(
            self,
            info,
            name: str,
    ) -> list[Person]:
        """ Get all users """
        # your logic to return a list of Person


    @strawberry.field
    def single(
            self,
            info,
            name: str,
    ) -> Person:
        """ Get a single Person """
        # your logic to return a single Person

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! (esp. for the first part of your answer) The Strawberry docs never show how to write resolvers more than 1 level deep.
1

You can check out this thread on how to make nested queries work in Strawberry Django plus.

https://github.com/blb-ventures/strawberry-django-plus/issues/117

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.