From 3ee94131ae511d2d83116730dcc4a50258ced5af Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 2 Nov 2017 21:57:10 -0700 Subject: [PATCH] Improved object container initialization. Fixed #585 --- graphene/types/objecttype.py | 2 +- graphene/types/tests/test_mutation.py | 28 +++++++++++++++++++++++++ graphene/types/tests/test_objecttype.py | 28 +++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/graphene/types/objecttype.py b/graphene/types/objecttype.py index fe234b09..a16d2b60 100644 --- a/graphene/types/objecttype.py +++ b/graphene/types/objecttype.py @@ -81,7 +81,7 @@ class ObjectType(BaseType): for name, field in fields_iter: try: - val = kwargs.pop(name) + val = kwargs.pop(name, None) setattr(self, name, val) except KeyError: pass diff --git a/graphene/types/tests/test_mutation.py b/graphene/types/tests/test_mutation.py index 69e14b0c..91ab14d2 100644 --- a/graphene/types/tests/test_mutation.py +++ b/graphene/types/tests/test_mutation.py @@ -105,3 +105,31 @@ def test_mutation_execution(): 'dynamic': 'dynamic', } } + + +def test_mutation_no_fields_output(): + class CreateUser(Mutation): + name = String() + + def mutate(self, info): + return CreateUser() + + class Query(ObjectType): + a = String() + + class MyMutation(ObjectType): + create_user = CreateUser.Field() + + schema = Schema(query=Query, mutation=MyMutation) + result = schema.execute(''' mutation mymutation { + createUser { + name + } + } + ''') + assert not result.errors + assert result.data == { + 'createUser': { + 'name': None, + } + } diff --git a/graphene/types/tests/test_objecttype.py b/graphene/types/tests/test_objecttype.py index 9ad0f817..e7c76d97 100644 --- a/graphene/types/tests/test_objecttype.py +++ b/graphene/types/tests/test_objecttype.py @@ -5,6 +5,8 @@ from ..interface import Interface from ..objecttype import ObjectType from ..unmountedtype import UnmountedType from ..structures import NonNull +from ..scalars import String +from ..schema import Schema class MyType(Interface): @@ -224,3 +226,29 @@ def test_objecttype_with_possible_types_and_is_type_of_should_raise(): 'MyObjectType.Meta.possible_types will cause type collision with ' 'MyObjectType.is_type_of. Please use one or other.' ) + + +def test_objecttype_no_fields_output(): + class User(ObjectType): + name = String() + + class Query(ObjectType): + user = Field(User) + + def resolve_user(self, info): + return User() + + + schema = Schema(query=Query) + result = schema.execute(''' query basequery { + user { + name + } + } + ''') + assert not result.errors + assert result.data == { + 'user': { + 'name': None, + } + }