graphene icon indicating copy to clipboard operation
graphene copied to clipboard

Confused about what to use: graphene.NonNull or graphene.Argument

Open glusa8 opened this issue 10 years ago • 2 comments

Dear Graphene Community,

I am confused as to whether to use graphene.NonNull or graphene.Argument to indicate that I want to take an input of type AInput.

Which one should I use?

Is there any other way of saying that a type of AInput goes into the field input?

What is graphene.Argument? I couldn't find it in the latest documentation?

class AInput(InputObjectType):
    foo = graphene.String()
    bar = graphene.String()

class AMutation(Mutation):
    class Input(object):
        input = graphene.NonNull(AInput)

    ok = graphene.Boolean()

    @classmethod
    @with_context
    def mutate(cls, instance, args, context, info):
        ok = True
        return AMutation(ok=ok)

glusa8 avatar Oct 13 '16 23:10 glusa8

The short answer is, what you're doing is fine, you don't need to mess with Argument.

The long answer is what you put in the Input class will get converted by the Mutation's meta class: https://github.com/graphql-python/graphene/blob/master/graphene/types/mutation.py

From there, the attributes defined on Input are passed to Field as args=: https://github.com/graphql-python/graphene/blob/master/graphene/types/field.py

Field init then calls to_argument(args), which runs them through a few conditions of interest to you:


        if isinstance(arg, Dynamic):
            arg = arg.get_type()
            if arg is None:
                # If the Dynamic type returned None
                # then we skip the Argument
                continue

        if isinstance(arg, UnmountedType):
            arg = arg.Argument()

        if not isinstance(arg, Argument):
            raise ValueError('Unknown argument "{}".'.format(default_name))

So as you can see, it handles Dynamic types, UnmountedType (this is what you're using), and Argument types.

Btw, if you mean to use this for Relay, then you can use the ClientIDMutation parent:


class AMutation(graphene.relay.ClientIDMutation):
    class Input:
        foo = graphene.String()
        bar = graphene.String()

    ok = graphene.Boolean()

    @classmethod
    def mutate_and_get_payload(cls, input, context, info):
        ok = True
        return AMutation(ok=ok)

yfilali avatar Nov 02 '16 04:11 yfilali

@yfilali comment should address the issue. I will close it, but feel free to re-open it if you still have problems with it :)

syrusakbary avatar Nov 15 '16 04:11 syrusakbary