[英]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.