33 GraphQLSchema ,
44 GraphQLEnumType ,
55 GraphQLEnumValue ,
6+ GraphQLInputObjectField ,
67 GraphQLInputObjectType ,
78 GraphQLInterfaceType ,
89 GraphQLObjectType ,
1516 GraphQLField ,
1617 GraphQLArgument ,
1718)
19+ from graphql .core .type .definition import is_input_type , is_output_type
1820
1921BlogImage = GraphQLObjectType ('Image' , {
2022 'url' : GraphQLField (GraphQLString ),
@@ -103,6 +105,64 @@ def test_defines_a_mutation_schema():
103105 assert write_mutation .name == 'writeArticle'
104106
105107
108+ def test_includes_nested_input_objects_in_the_map ():
109+ NestedInputObject = GraphQLInputObjectType (
110+ name = 'NestedInputObject' ,
111+ fields = {'value' : GraphQLInputObjectField (GraphQLString )}
112+ )
113+
114+ SomeInputObject = GraphQLInputObjectType (
115+ name = 'SomeInputObject' ,
116+ fields = {'nested' : GraphQLInputObjectField (NestedInputObject )}
117+ )
118+
119+ SomeMutation = GraphQLObjectType (
120+ name = 'SomeMutation' ,
121+ fields = {
122+ 'mutateSomething' : GraphQLField (
123+ type = BlogArticle ,
124+ args = {
125+ 'input' : GraphQLArgument (SomeInputObject )
126+ }
127+ )
128+ }
129+ )
130+
131+ schema = GraphQLSchema (
132+ query = BlogQuery ,
133+ mutation = SomeMutation
134+ )
135+
136+ assert schema .get_type_map ()['NestedInputObject' ] is NestedInputObject
137+
138+
139+ def test_includes_interfaces_thunk_subtypes_in_the_type_map ():
140+ SomeInterface = GraphQLInterfaceType (
141+ name = 'SomeInterface' ,
142+ fields = {
143+ 'f' : GraphQLField (GraphQLInt )
144+ }
145+ )
146+
147+ SomeSubtype = GraphQLObjectType (
148+ name = 'SomeSubtype' ,
149+ fields = {
150+ 'f' : GraphQLField (GraphQLInt )
151+ },
152+ interfaces = lambda : [SomeInterface ],
153+ is_type_of = lambda : True
154+ )
155+
156+ schema = GraphQLSchema (query = GraphQLObjectType (
157+ name = 'Query' ,
158+ fields = {
159+ 'iface' : GraphQLField (SomeInterface )
160+ }
161+ ))
162+
163+ assert schema .get_type_map ()['SomeSubtype' ] is SomeSubtype
164+
165+
106166def test_includes_interfaces_subtypes_in_the_type_map ():
107167 SomeInterface = GraphQLInterfaceType ('SomeInterface' , fields = {'f' : GraphQLField (GraphQLInt )})
108168 SomeSubtype = GraphQLObjectType (
@@ -130,16 +190,41 @@ def test_stringifies_simple_types():
130190
131191
132192def test_identifies_input_types ():
133- pass # TODO
193+ expected = (
194+ (GraphQLInt , True ),
195+ (ObjectType , False ),
196+ (InterfaceType , False ),
197+ (UnionType , False ),
198+ (EnumType , True ),
199+ (InputObjectType , True )
200+ )
201+
202+ for type , answer in expected :
203+ assert is_input_type (type ) == answer
204+ assert is_input_type (GraphQLList (type )) == answer
205+ assert is_input_type (GraphQLNonNull (type )) == answer
134206
135207
136208def test_identifies_output_types ():
137- pass # TODO
209+ expected = (
210+ (GraphQLInt , True ),
211+ (ObjectType , True ),
212+ (InterfaceType , True ),
213+ (UnionType , True ),
214+ (EnumType , True ),
215+ (InputObjectType , False )
216+ )
217+
218+ for type , answer in expected :
219+ assert is_output_type (type ) == answer
220+ assert is_output_type (GraphQLList (type )) == answer
221+ assert is_output_type (GraphQLNonNull (type )) == answer
138222
139223
140224def test_prohibits_nesting_nonnull_inside_nonnull ():
141225 with raises (Exception ) as excinfo :
142226 GraphQLNonNull (GraphQLNonNull (GraphQLInt ))
227+
143228 assert 'Can only create NonNull of a Nullable GraphQLType but got: Int!.' in str (excinfo .value )
144229
145230
@@ -159,3 +244,34 @@ def test_prohibits_putting_non_object_types_in_unions():
159244
160245 assert 'BadUnion may only contain Object types, it cannot contain: ' + str (x ) + '.' \
161246 == str (excinfo .value )
247+
248+
249+ def test_does_not_mutate_passed_field_definitions ():
250+ fields = {
251+ 'field1' : GraphQLField (GraphQLString ),
252+ 'field2' : GraphQLField (GraphQLString , args = {'id' : GraphQLArgument (GraphQLString )}),
253+ }
254+
255+ TestObject1 = GraphQLObjectType (name = 'Test1' , fields = fields )
256+ TestObject2 = GraphQLObjectType (name = 'Test1' , fields = fields )
257+
258+ assert TestObject1 .get_fields () == TestObject2 .get_fields ()
259+ assert fields == {
260+ 'field1' : GraphQLField (GraphQLString ),
261+ 'field2' : GraphQLField (GraphQLString , args = {'id' : GraphQLArgument (GraphQLString )}),
262+ }
263+
264+ input_fields = {
265+ 'field1' : GraphQLInputObjectField (GraphQLString ),
266+ 'field2' : GraphQLInputObjectField (GraphQLString ),
267+ }
268+
269+ TestInputObject1 = GraphQLInputObjectType (name = 'Test1' , fields = input_fields )
270+ TestInputObject2 = GraphQLInputObjectType (name = 'Test2' , fields = input_fields )
271+
272+ assert TestInputObject1 .get_fields () == TestInputObject2 .get_fields ()
273+
274+ assert input_fields == {
275+ 'field1' : GraphQLInputObjectField (GraphQLString ),
276+ 'field2' : GraphQLInputObjectField (GraphQLString ),
277+ }
0 commit comments