GraphQLError未知类型“ XYZMutationInput”

问题描述 投票:0回答:1

如何为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']>,
  ...
}

相关:

graphql graphene-python graphene-django graphql-python
1个回答
0
投票

我忘了将mutation kwarg添加到:

schema = graphene.Schema(query=Query)

应该是:

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

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

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

query TestQueryWorks(id: ID!) {
   getThing(id: $id)
}
© www.soinside.com 2019 - 2024. All rights reserved.