A limitation of Mutation?
Hello,
When I do a mutation, I want to be able to return the type directly instead of wrapping it into another field:
So, I want to do:
mutation {
createPerson {
__typename // <-- this should give you 'PersonType', ideally
id
firstName
lastName
}
}
but it seems like in Graphene Django, you have to wrap it inside another field:
mutation {
createPerson {
__typename // <-- in reality, this does not give you 'PersonType'
person {
__typename // <-- this gives you 'PersonType'
id
firstName
lastName
}
}
}
How can I achieve the former?
This is a common pattern. Even the official documentation has this pattern: http://graphql.org/learn/queries/#mutations
@syrusakbary could you provide your thought?
Hi @glusa8,
you can use the Output attr in the mutation:
class CreatePerson(graphene.Mutation):
Output = Person
...
@syrusakbary I tried doing but I got the following:
AssertionError: CreatePerson fields must be a mapping (dict / OrderedDict) with field names as keys or a function which returns such a mapping.
I've tried:
class CreatePerson(graphene.Mutation):
Output = Person
as well as (and I think this is what you meant)
class CreatePerson(graphene.Mutation):
Output = PersonType
but both failed.
The Output =, where is that documented?
I'm using graphene-django==1.3
@syrusakbary , can you address my comment above? Thank you.
The Output attr works for me (GraphiQL autocompletes the corrects fields as possible return values for the mutation), but I'm not sure how to return them from the mutation method.
import graphene
from graphene_django.types import DjangoObjectType
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
class UserType(DjangoObjectType):
class Meta:
model = User
class Login(graphene.Mutation):
class Arguments:
username = graphene.String()
password = graphene.String()
Output = UserType
def mutate(self, info, **kwargs):
user = authenticate(info.context, **kwargs)
if user and user != info.context.user:
login(info.context, user)
return Login(user) # <-- ???
class Mutations(graphene.ObjectType):
login = Login.Field()
class Query(object):
user = graphene.Field(UserType)
def resolve_user(self, info, **kwargs):
return info.context.user
Any ideas??
@honi I made it work by just returning the newly created instance:
def mutate(self, info, **kwargs):
user = authenticate(info.context, **kwargs)
if user and user != info.context.user:
login(info.context, user)
return user