From 9f366e93c60e1e01e635a50b8c424b93093e5ec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Diemer?= Date: Mon, 2 Jul 2018 09:34:34 +0200 Subject: [PATCH 1/2] __wip__ add failed test Just to ease review. TODO: merge with next commit. --- graphene/types/tests/test_mutation.py | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/graphene/types/tests/test_mutation.py b/graphene/types/tests/test_mutation.py index df17477d..6ef5a072 100644 --- a/graphene/types/tests/test_mutation.py +++ b/graphene/types/tests/test_mutation.py @@ -158,3 +158,47 @@ def test_mutation_allow_to_have_custom_args(): assert field.description == 'Create a user' assert field.deprecation_reason == 'Is deprecated' assert field.type == NonNull(CreateUser) + + +def test_mutation_as_subclass(): + class BaseCreateUser(Mutation): + + class Arguments: + name = String() + + name = String() + + def mutate(self, info, **args): + return args + + class CreateUserWithPlanet(BaseCreateUser): + + class Arguments(BaseCreateUser.Arguments): + planet = String() + + planet = String() + + def mutate(self, info, **args): + return CreateUserWithPlanet(**args) + + class MyMutation(ObjectType): + create_user_with_planet = CreateUserWithPlanet.Field() + + class Query(ObjectType): + a = String() + + schema = Schema(query=Query, mutation=MyMutation) + result = schema.execute(''' mutation mymutation { + createUserWithPlanet(name:"Peter", planet: "earth") { + name + planet + } + } + ''') + assert not result.errors + assert result.data == { + 'createUserWithPlanet': { + 'name': 'Peter', + 'planet': 'earth', + } + } From 181e75c952a3a357c4386749e09a46c547c02b74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Diemer?= Date: Fri, 29 Jun 2018 16:49:33 +0200 Subject: [PATCH 2/2] Fetch fields from parent classes in mutations The goal of this commit is to be able to subclass mutations like this: ``` class BaseMutation(graphene.Mutation): class Arguments: name = graphene.String() def mutate(self, info, **kwargs): # do something class ChildMutation(BaseMutation): class Arguments(BaseMutation.Arguments): other_arg = graphene.String() def mutate(self, info, **kwargs): # do other things ``` Note: vars(x).get(key, gettattr(x, key)) is used instead of the simpler gettatrr(x, key) for python2.7 compat. Indeed python2 and python3 lead to different results for class Foo(object): def bar(self): pass getattr(Foo, 'bar') # python 2.7 : > unbound method bar # python 3.x : > function Foo.bar --- graphene/utils/props.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphene/utils/props.py b/graphene/utils/props.py index 5162720e..5ef3ba0a 100644 --- a/graphene/utils/props.py +++ b/graphene/utils/props.py @@ -11,5 +11,5 @@ _all_vars = set(dir(_OldClass) + dir(_NewClass)) def props(x): return { - key: value for key, value in vars(x).items() if key not in _all_vars + key: vars(x).get(key, getattr(x, key)) for key in dir(x) if key not in _all_vars }