Neau Adrien Neau Adrien - 1 month ago 25
Python Question

Bug with graphene.Mutation?

I use graphene-django for have a GrapQL API.
I have created a mutation in my schema.py:

class UpdateApplication(graphene.Mutation):
class Input:
id = graphene.String()
name = graphene.String()

application = graphene.Field(ApplicationNode)

@classmethod
def mutate(cls, instance, args, info):
name = args.get('name')
rid = from_global_id(args.get('id'))[1]
update_application = Application.objects.filter(id=rid).update(name=name)

return UpdateApplication(application=update_application)



class Mutation(ObjectType):
update_application = UpdateApplication.Field()

schema = graphene.Schema(mutation=Mutation)


When I run this resquest, I have an error.

mutation update {
updateApplication(id: "QXBwbGljYXRpb25Ob2RlOjE=", name: "foo") {
application {
name
}
}
}


The error:

mutate() takes exactly 4 arguments (5 given)


I put 4 arguments in mutate() not 5... Is it a bug?

Answer

As of graphene 1.0, the context is now passed to mutation and resolve functions by default whereas it required @with_context in previous versions: https://github.com/graphql-python/graphene/blob/master/UPGRADE-v1.0.md

So your mutate functions should look like:

def mutate(self, args, context, info):
    name = args.get('name')
    rid = from_global_id(args.get('id'))[1]
    update_application = Application.objects.filter(id=rid).update(name=name)

    return UpdateApplication(application=update_application)