From 9408ba70d165284ff7ab5efd159a62e1d40a9a7f Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Sat, 7 Apr 2018 15:31:04 -0700 Subject: [PATCH] Fields default_value are no longer dismissed. Fixed #702. --- graphene/types/objecttype.py | 5 ++++- graphene/types/tests/test_query.py | 24 ++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/graphene/types/objecttype.py b/graphene/types/objecttype.py index a16d2b60..c6e4d5d7 100644 --- a/graphene/types/objecttype.py +++ b/graphene/types/objecttype.py @@ -81,7 +81,10 @@ class ObjectType(BaseType): for name, field in fields_iter: try: - val = kwargs.pop(name, None) + val = kwargs.pop( + name, + field.default_value if isinstance(field, Field) else None + ) setattr(self, name, val) except KeyError: pass diff --git a/graphene/types/tests/test_query.py b/graphene/types/tests/test_query.py index 95e626ef..a106af25 100644 --- a/graphene/types/tests/test_query.py +++ b/graphene/types/tests/test_query.py @@ -9,9 +9,9 @@ from ..inputfield import InputField from ..inputobjecttype import InputObjectType from ..interface import Interface from ..objecttype import ObjectType -from ..scalars import Int, String +from ..scalars import Int, String, Boolean from ..schema import Schema -from ..structures import List +from ..structures import List, NonNull from ..union import Union from ..context import Context @@ -475,3 +475,23 @@ def test_query_annotated_resolvers(): result = test_schema.execute('{ info }', "base") assert not result.errors assert result.data == {'info': 'base-info'} + + +def test_default_as_kwarg_to_NonNull(): + # Related to https://github.com/graphql-python/graphene/issues/702 + class User(ObjectType): + name = String() + is_admin = NonNull(Boolean, default_value=False) + + class Query(ObjectType): + user = Field(User) + + def resolve_user(self, *args, **kwargs): + return User(name="foo") + + schema = Schema(query=Query) + expected = {'user': {'name': 'foo', 'isAdmin': False}} + result = schema.execute("{ user { name isAdmin } }") + + assert not result.errors + assert result.data == expected