Graphene-django: mutate() takes 4 positional arguments but 5 were given

Created on 2 May 2017  Â·  3Comments  Â·  Source: graphql-python/graphene-django

I've seen this issue, so I made sure I use only the Relay classes and interfaces, and I still get this error in /graphql.

https://github.com/graphql-python/graphene-django/issues/78

import graphene
from graphene_django.types import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from .models import Org


class OrgNode(DjangoObjectType):
    class Meta:
        model = Org
        interfaces = (graphene.relay.Node, )


class Query(graphene.AbstractType):
    all_orgs = DjangoFilterConnectionField(OrgNode)

    def resolve_all_orgs(self, args, context, info):
        return Org.objects.all()

import graphene
from graphene import relay, ObjectType, AbstractType, List, String, Field
from graphene_django.types import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from .models import Product
from orgs.models import Org
from orgs.schema import OrgNode



class ProductNode(DjangoObjectType):
    class Meta:
        model = Product
        filter_fields = {
            'name': ['exact', 'icontains', 'istartswith'],
            'org__name': ['icontains'],
            'org__admin__username':['icontains'],
        }
        interfaces = (relay.Node, )




class AddProduct(relay.ClientIDMutation):
    class Input:
        name = String(required=True)

    product = Field(ProductNode)
    org = Field(OrgNode)

    @classmethod
    def mutate(cls, input, context, info):
        product_name = input.get('name')
        org = Org.objects.first()
        org_node = OrgNode(org)
        desc = "description whatever...."
        product = Product.objects.create(name = product_name, org = org, description = desc)
        product_node = ProductNode(p)
        return AddProduct(product = product_node, org = org_node)


class Query(AbstractType):
    all_products = DjangoFilterConnectionField(ProductNode)

    def resolve_all_products(self, args, context, info):
        return Product.objects.all()

class Mutation(AbstractType):
    add_product = AddProduct.Field()


Most helpful comment

That was it. Easy to get lost in the documentation between Relay vs normal GraphQL, Graphene vs DjangoGraphene. Docs need some more love.

All 3 comments

I created another vanilla model Course, with only one field - name, no related models. Still the same.

import graphene
from graphene_django.types import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from .models import Course


class CourseNode(DjangoObjectType):
    class Meta:
        model = Course
        interfaces = (graphene.relay.Node, )

class CreateCourse(graphene.relay.ClientIDMutation):
    class Input:
        name = graphene.String(required=True)

    course = graphene.Field(CourseNode)

    @classmethod
    def mutate(cls, input, args, info):
        name = input.get('name')
        c = Course.objects.create(name = name)
        course = CourseNode(c)
        return CreateCourse(course = course)


class Query(graphene.ObjectType):
    all_courses = DjangoFilterConnectionField(CourseNode)

    def resolve_all_courses(self, args, context, info):
        return Course.objects.all()

class Mutation(graphene.ObjectType):
    create_course = CreateCourse.Field()

In /graphql I then run this:

mutation{
  createCourse(input:{name:"My Course"}){
    course{
      id
    }
  }
}

and get this back:

{
  "errors": [
    {
      "message": "mutate() takes 4 positional arguments but 5 were given",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ]
    }
  ],
  "data": {
    "createCourse": null
  }
}

You are overriding the incorrect method. When extending ClientIdMutation
you should override mutate_and_get_payload not mutate

mutate_and_get_payload creates the clientId injects it into the payload
then calls mutate itself

On May 2, 2017 7:15 AM, "mraak" notifications@github.com wrote:

I created another vanilla model Course, with only one field - name, no
related models. Still the same.

import graphenefrom graphene_django.types import DjangoObjectTypefrom graphene_django.filter import DjangoFilterConnectionFieldfrom .models import Course

class CourseNode(DjangoObjectType):
class Meta:
model = Course
interfaces = (graphene.relay.Node, )
class CreateCourse(graphene.relay.ClientIDMutation):
class Input:
name = graphene.String(required=True)

course = graphene.Field(CourseNode)

@classmethod
def mutate(cls, input, args, info):
    name = input.get('name')
    c = Course.objects.create(name = name)
    course = CourseNode(c)
    return CreateCourse(course = course)

class Query(graphene.ObjectType):
all_courses = DjangoFilterConnectionField(CourseNode)

def resolve_all_courses(self, args, context, info):
    return Course.objects.all()

class Mutation(graphene.ObjectType):
create_course = CreateCourse.Field()

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/graphql-python/graphene-django/issues/165#issuecomment-298648376,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACAlSxsbGS36KsLUXjsvvXMjkNmWwQAtks5r1zqWgaJpZM4NN6Yn
.

That was it. Easy to get lost in the documentation between Relay vs normal GraphQL, Graphene vs DjangoGraphene. Docs need some more love.

Was this page helpful?
0 / 5 - 0 ratings