jmunsch
jmunsch

Reputation: 24109

GraphQLError Unknown type "XYZMutationInput"

How to create graphql input type for DRF serializer?

I am using django rest framework (DRF) serializers, graphene-django, and I am able to see the CreateThingMutationInput type defined in graphiql:

  mutation TestCreate($input: CreateThingMutationInput!) {
    createProjectThing(input: $input) {
      id
      errors {
        field
        messages
      }
    }
  }

However, I am unable to run:

        schema = graphene.Schema(query=Query)
        result = schema.execute(self.query, variables=variables)

I get:

[GraphQLError('Unknown type "CreateThingMutationInput".',)] 

With the following:

class CreateThingMutation(SerializerMutation):
    class Meta:
        serializer_class = ThingListViewSerializer


class Mutation(graphene.ObjectType):
    debug = graphene.Field(DjangoDebug, name="_debug")

    create_project_thing = CreateThingMutation.Field()

I've also tried:

class CreateThingMutationInput(graphene.ObjectType):
    input = graphene.Field(convert_serializer_to_input_type(ThingListViewSerializer))

As well as trying to define a:

class Input:
    input = graphene.Field(convert_serializer_to_input_type(ThingListViewSerializer))

I can also see the type defined from graphql-codegen in types.d.ts:

export type CreateThingMutationInput = {
  id?: Maybe<Scalars['Int']>,
  ...
}

related:

Upvotes: 1

Views: 3947

Answers (1)

jmunsch
jmunsch

Reputation: 24109

I forgot to add the mutation kwarg to:

schema = graphene.Schema(query=Query)

Should be:

schema = graphene.Schema(query=Query, mutation=Mutation)

Another reason this can happen for GraphQLError('Unknown type "Number".',) is if a query function gets an unexpected argument, for example calling getThing with a Number, instead of an ID:

query TestQueryWontWork(id: Number="") {
   getThing(id: $id)
}

query TestQueryWorks(id: ID!) {
   getThing(id: $id)
}

Upvotes: 2

Related Questions