简体   繁体   English

GraphQLError 未知类型“XYZMutationInput”

[英]GraphQLError Unknown type “XYZMutationInput”

How to create graphql input type for DRF serializer?如何为 DRF 串行器创建 graphql 输入类型?

I am using django rest framework (DRF) serializers, graphene-django , and I am able to see the CreateThingMutationInput type defined in graphiql :我正在使用 django rest 框架(DRF)序列化graphene-django ,并且我能够看到在graphiql中定义的CreateThingMutationInput类型:

  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 :我还可以在 types.d.ts 中看到从graphql-codegen types.d.ts定义的类型:

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

related:有关的:

I forgot to add the mutation kwarg to:我忘了将mutation kwarg 添加到:

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 : GraphQLError('Unknown type "Number".',)发生这种情况的另一个原因是,如果查询 function 收到意外参数,例如使用Number而不是ID调用getThing

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GraphQLError:无法在类型“ ProductMarkdown”上查询字段“ category” - GraphQLError: Cannot query field “category” on type “ProductMarkdown” GraphQLError:必须提供查询根类型 - GraphQLError: Query root type must be provided Apollo GraphQL 教程:“GraphQLError: Cannot query field \”id\“ on type \”LaunchConnection\“。” - Apollo GraphQL tutorial: “GraphQLError: Cannot query field \”id\“ on type \”LaunchConnection\“.” GraphQLError: Input Object 类型必须定义一个或多个字段 - GraphQLError: Input Object type must define one or more fields 合并模式后“GraphQLError:在类型中找不到字段:&#39;query_root&#39;” - "GraphQLError: field not found in type: 'query_root'" after merge schema 错误 TS2416:“ApolloError”类型中的属性“originalError”不可分配给基本类型“GraphQLError”中的相同属性 - error TS2416: Property 'originalError' in type 'ApolloError' is not assignable to the same property in base type 'GraphQLError' Kotlin 和 Java 中的 RuntimeException 和 GraphQLError - RuntimeException and GraphQLError in Kotlin and Java 模块构建失败:GraphQLError - Module build failed: GraphQLError 发送变异时出现GraphQLError - GraphQLError when sending mutation 在 GraphQLError 上重试获取 - Retry to fetch on GraphQLError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM