Use argument's default_value regardless if the input field is required (#1326)

* Use argument's default value regardless if the input field is required

* Add a test

* Format code
This commit is contained in:
Minh Tu Le 2021-04-19 10:03:11 -07:00 committed by GitHub
parent f622f1f53c
commit c08379ed85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 4 deletions

View File

@ -26,7 +26,6 @@ from graphql import (
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
Undefined,
)
from graphql.execution import ExecutionContext
from graphql.execution.values import get_argument_values
@ -313,9 +312,7 @@ class TypeMap(dict):
arg_type,
out_name=arg_name,
description=arg.description,
default_value=Undefined
if isinstance(arg.type, NonNull)
else arg.default_value,
default_value=arg.default_value,
)
subscribe = field.wrap_subscribe(
self.get_function_for_type(

View File

@ -6,6 +6,7 @@ from graphql.type import (
GraphQLInputField,
GraphQLInputObjectType,
GraphQLInterfaceType,
GraphQLNonNull,
GraphQLObjectType,
GraphQLString,
)
@ -94,6 +95,21 @@ def test_objecttype():
}
def test_required_argument_with_default_value():
class MyObjectType(ObjectType):
foo = String(bar=String(required=True, default_value="x"))
type_map = create_type_map([MyObjectType])
graphql_type = type_map["MyObjectType"]
foo_field = graphql_type.fields["foo"]
bar_argument = foo_field.args["bar"]
assert bar_argument.default_value == "x"
assert isinstance(bar_argument.type, GraphQLNonNull)
assert bar_argument.type.of_type == GraphQLString
def test_dynamic_objecttype():
class MyObjectType(ObjectType):
"""Description"""