graphene-django icon indicating copy to clipboard operation
graphene-django copied to clipboard

A limitation of Mutation?

Open glusa8 opened this issue 9 years ago • 6 comments

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

glusa8 avatar Jul 08 '17 19:07 glusa8

@syrusakbary could you provide your thought?

glusa8 avatar Sep 10 '17 20:09 glusa8

Hi @glusa8,

you can use the Output attr in the mutation:

class CreatePerson(graphene.Mutation):
    Output = Person
    ...

syrusakbary avatar Sep 13 '17 07:09 syrusakbary

@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

glusa8 avatar Sep 13 '17 18:09 glusa8

@syrusakbary , can you address my comment above? Thank you.

glusa8 avatar Sep 22 '17 16:09 glusa8

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 avatar Dec 17 '17 00:12 honi

@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

a3ammar avatar Dec 20 '17 12:12 a3ammar