Skip to content

Commit 36a5bae

Browse files
committed
Added test cases for custom object input containers
1 parent 1cba47b commit 36a5bae

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

graphql/execution/tests/test_variables.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from graphql.error import GraphQLError, format_error
77
from graphql.execution import execute
88
from graphql.language.parser import parse
9-
from graphql.type import (GraphQLArgument, GraphQLField,
9+
from graphql.type import (GraphQLArgument, GraphQLField, GraphQLBoolean,
1010
GraphQLInputObjectField, GraphQLInputObjectType,
1111
GraphQLList, GraphQLNonNull, GraphQLObjectType,
1212
GraphQLScalarType, GraphQLSchema, GraphQLString)
@@ -18,13 +18,23 @@
1818
parse_literal=lambda v: 'DeserializedValue' if v.value == 'SerializedValue' else None
1919
)
2020

21+
22+
class my_special_dict(dict):
23+
pass
24+
25+
2126
TestInputObject = GraphQLInputObjectType('TestInputObject', OrderedDict([
2227
('a', GraphQLInputObjectField(GraphQLString)),
2328
('b', GraphQLInputObjectField(GraphQLList(GraphQLString))),
2429
('c', GraphQLInputObjectField(GraphQLNonNull(GraphQLString))),
2530
('d', GraphQLInputObjectField(TestComplexScalar))
2631
]))
2732

33+
34+
TestCustomInputObject = GraphQLInputObjectType('TestCustomInputObject', OrderedDict([
35+
('a', GraphQLInputObjectField(GraphQLString)),
36+
]), container_type=my_special_dict)
37+
2838
stringify = lambda obj: json.dumps(obj, sort_keys=True)
2939

3040

@@ -47,6 +57,10 @@ def input_to_json(obj, args, context, info):
4757
GraphQLString,
4858
args={'input': GraphQLArgument(TestInputObject)},
4959
resolver=input_to_json),
60+
'fieldWithCustomObjectInput': GraphQLField(
61+
GraphQLBoolean,
62+
args={'input': GraphQLArgument(TestCustomInputObject)},
63+
resolver=lambda root, args, context, info: isinstance(args.get('input'), my_special_dict)),
5064
'fieldWithNullableStringInput': GraphQLField(
5165
GraphQLString,
5266
args={'input': GraphQLArgument(GraphQLString)},
@@ -420,6 +434,20 @@ def test_passes_along_null_for_non_nullable_inputs_if_explcitly_set_in_the_query
420434
})
421435

422436

437+
def test_uses_objectinput_container():
438+
doc = '''
439+
{
440+
fieldWithCustomObjectInput(input: {a: "b"})
441+
}
442+
'''
443+
444+
check(doc, {
445+
'data': {
446+
'fieldWithCustomObjectInput': True
447+
}
448+
})
449+
450+
423451
def test_allows_lists_to_be_null():
424452
doc = '''
425453
query q($input: [String]) {

graphql/type/definition.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,15 +507,18 @@ class GeoPoint(GraphQLInputObjectType):
507507
default_value=0)
508508
}
509509
"""
510-
def __init__(self, name, fields, description=None):
510+
def __init__(self, name, fields, description=None, container_type=None):
511511
assert name, 'Type must be named.'
512512
self.name = name
513513
self.description = description
514-
514+
if container_type is None:
515+
container_type = dict
516+
assert callable(container_type), "container_type must be callable"
517+
self.container_type = container_type
515518
self._fields = fields
516519

517520
def create_container(self, data):
518-
return dict(data)
521+
return self.container_type(data)
519522

520523
@cached_property
521524
def fields(self):

graphql/utils/value_from_ast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def value_from_ast(value_ast, type, variables=None):
6464
# dict if exists
6565
obj[field.out_name or field_name] = field_value
6666

67-
return type.create_container(obj)
67+
return type.create_container(obj)
6868

6969
assert isinstance(type, (GraphQLScalarType, GraphQLEnumType)), \
7070
'Must be input type'

0 commit comments

Comments
 (0)