GraphQLError未知类型“ XYZMutationInput”

时间:2019-09-24 22:29:48

标签: graphql graphene-python graphene-django graphql-python

如何为DRF序列化器创建graphql输入类型?

我正在使用django rest框架(DRF)序列化程序graphene-django,并且能够看到CreateThingMutationInput中定义的graphiql类型:

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

但是,我无法运行:

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

我得到:

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

具有以下内容:

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


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

    create_project_thing = CreateThingMutation.Field()

我也尝试过:

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

以及尝试定义一个:

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

我还可以在graphql-codegen中看到从types.d.ts定义的类型:

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

相关:

1 个答案:

答案 0 :(得分:0)

我忘了将mutation矮人添加到:

schema = graphene.Schema(query=Query)

应该是:

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

GraphQLError('Unknown type "Number".',)可能发生的另一个原因是,查询函数获得了意外的参数,例如,使用getThing而不是Number来调用ID:< / p>

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

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