2
# 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)

I have gone through documentation and other tutorials but can't seem to figure out how to execute a mutation query. I've tried

# Query 1
mutation {
   createCustomer(name: "John", address: "Some address") {
     id, name
   }
}
# Query 2
mutation {
   createCustomer(input: {name: "John", address: "Some address"}) {
     id, name
   }
}

but it doesn't work and shows error -

# 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'."

What am I missing? What is the correct syntax/expression to do so?

  • Is this a case problem? You have `class Input` with a capital "I" but you're trying to invoke it with a lower case "i" (your second mutation syntax) – Michel Floyd Nov 23 '22 at 23:23
  • No that's not an issue. Input in the query should get passed as kwargs in `mutate_and_get_payload` method. – HappyChappy Nov 24 '22 at 10:00

1 Answers1

0

In case it helps anyone, I modified this to get it working.

class Mutation(ObjectType):
    create_customer = graphene.Field(CreateCustomerMutation)

I changed,

create_customer = graphene.Field(CreateCustomerMutation)

to

create_customer = CreateCustomerMutation.Field()