如何为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']>,
...
}
相关:
答案 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)
}