strawberry-graphql / strawberry-django

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

Returning a Query type in a mutation's payload #655

Open rcybulski1122012 opened 3 weeks ago

rcybulski1122012 commented 3 weeks ago

I have a Query type in my schema that defines some custom resolvers and uses strawberry_django.field/strawberry_django.node without providing resolver functions.

@strawberry_django.type(SomeModel)
class SomeType:
    id: auto
    name: auto

@strawberry.type(name='Query')
class Query:
    some_type_query: SomeType = strawberry_django.field()

    @strawberry_django.field()
    def custom_resolver(self) -> bool:
        return True

I want to return a Query along with the mutation result, enabling the user to fetch whatever data they need in a single request. However, when I initialize a Query object, I must provide arguments.

@strawberry.type
class MutationPayload:
    some_type: SomeType

    @strawberry_django.field(
    def query(self) -> Query:
        return Query()

@strawberry.type
class Mutation:
    @strawberry_django.mutation()
    def mutation_resolver(self) -> MutationPayload:
        some_model = SomeModel.objects.create(name="test")
        return MutationPayload(some_type=some_model)
{
  "data": null,
  "errors": [
    {
      "message": "Query.__init__() missing 1 required keyword-only argument: 'some_type_query'",
      "locations": [
        {
          "line": 7,
          "column": 5
        }
      ],
      "path": [
        "mutationResolver",
        "query"
      ]
    }
  ]
}

Is it possible to initialize the Query type without providing all the arguments? Or is there an easy way to provide the arguments to the query?

Upvote & Fund

Fund with Polar