Alexis
Alexis

Reputation: 521

Nested queries in Strawberry GraphQL Python

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

Upvotes: 2

Views: 2102

Answers (2)

Sher Sanginov
Sher Sanginov

Reputation: 605

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

Upvotes: 1

Alexis
Alexis

Reputation: 521

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

Upvotes: 2

Related Questions