Как сделать мутационный запрос в GraphQL в Django?
# Model
class Customer(models.Model):
name = models.CharField(max_length=150)
address = models.CharField(max_length=150)
# Node
class CustomerNode(DjangoObjectType):
class Meta:
model = Customer
interfaces = (relay.Node,)
# Mutations
class CreateCustomerMutation(relay.ClientIDMutation):
class Input:
name = graphene.String(required=True)
address = graphene.String()
customer = graphene.Field(CustomerNode)
@classmethod
def mutate_and_get_payload(cls, root, info, **input):
customer_instance = Customer(
name=input.name,
address=input.address,
)
customer_instance.save()
return CreateCustomerMutation(customer=customer_instance)
class Mutation(ObjectType):
create_customer = graphene.Field(CreateCustomerMutation)
# Schema
schema = graphene.Schema(query=Query, mutation=Mutation)
Я просмотрел документацию и другие руководства, но не могу понять, как выполнить мутационный запрос. Я пробовал
# Query 1
mutation {
createCustomer(name: "John", address: "Some address") {
id, name
}
}
# Query 2
mutation {
createCustomer(input: {name: "John", address: "Some address"}) {
id, name
}
}
но он не работает и показывает ошибку -
# Query 1
"Unknown argument 'name' on field 'Mutation.createCustomer'."
"Unknown argument 'address' on field 'Mutation.createCustomer'."
# Query 2
"Unknown argument 'input' on field 'Mutation.createCustomer'."
Что я упускаю? Каков правильный синтаксис/выражение для этого?
Если это кому-то поможет, я изменил это, чтобы заставить его работать.
class Mutation(ObjectType):
create_customer = graphene.Field(CreateCustomerMutation)
Я изменился,
create_customer = graphene.Field(CreateCustomerMutation)
to
create_customer = CreateCustomerMutation.Field()