Skip to content

Commit 628c996

Browse files
committed
build_schema/extend_schema: test standard scalars
Replicates graphql/graphql-js@0a98379
1 parent 6026f8e commit 628c996

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

tests/utilities/test_build_ast_schema.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
GraphQLDeprecatedDirective,
99
GraphQLIncludeDirective,
1010
GraphQLSkipDirective,
11+
GraphQLID,
12+
GraphQLInt,
13+
GraphQLFloat,
14+
GraphQLString,
15+
GraphQLBoolean,
1116
assert_directive,
1217
assert_enum_type,
1318
assert_input_object_type,
@@ -96,6 +101,30 @@ def simple_type():
96101
)
97102
assert cycle_sdl(sdl) == sdl
98103

104+
schema = build_schema(sdl)
105+
# Built-ins are used
106+
assert schema.get_type("Int") is GraphQLInt
107+
assert schema.get_type("Float") is GraphQLFloat
108+
assert schema.get_type("String") is GraphQLString
109+
assert schema.get_type("Boolean") is GraphQLBoolean
110+
assert schema.get_type("ID") is GraphQLID
111+
112+
def include_standard_type_only_if_it_is_used():
113+
schema = build_schema(
114+
"""
115+
type Query {
116+
str: String
117+
}
118+
"""
119+
)
120+
121+
# Only String and Boolean are used by introspection types
122+
assert schema.get_type("Int") is None
123+
assert schema.get_type("Float") is None
124+
assert schema.get_type("String") is GraphQLString
125+
assert schema.get_type("Boolean") is GraphQLBoolean
126+
assert schema.get_type("ID") is None
127+
99128
def with_directives():
100129
sdl = dedent(
101130
"""

tests/utilities/test_extend_schema.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
from graphql.pyutils import dedent
66
from graphql.type import (
77
GraphQLArgument,
8+
GraphQLBoolean,
89
GraphQLDirective,
910
GraphQLEnumType,
1011
GraphQLEnumValue,
1112
GraphQLField,
13+
GraphQLFloat,
1214
GraphQLID,
1315
GraphQLInputField,
1416
GraphQLInputObjectType,
17+
GraphQLInt,
1518
GraphQLInterfaceType,
1619
GraphQLList,
1720
GraphQLNonNull,
@@ -30,7 +33,7 @@
3033
specified_directives,
3134
validate_schema,
3235
)
33-
from graphql.utilities import extend_schema, print_schema
36+
from graphql.utilities import build_schema, extend_schema, print_schema
3437

3538
# Test schema.
3639

@@ -210,6 +213,54 @@ def extends_objects_by_adding_new_fields():
210213
query_type = assert_object_type(extended_schema.get_type("Query"))
211214
assert query_type.fields["foo"].type == foo_type
212215

216+
def extends_objects_with_standard_type_fields():
217+
schema = build_schema(
218+
"""
219+
type Query {
220+
str: String
221+
}
222+
"""
223+
)
224+
225+
# Only String and Boolean are used by introspection types
226+
assert schema.get_type("Int") is None
227+
assert schema.get_type("Float") is None
228+
assert schema.get_type("String") is GraphQLString
229+
assert schema.get_type("Boolean") is GraphQLBoolean
230+
assert schema.get_type("ID") is None
231+
232+
extend_ast = parse(
233+
"""
234+
extend type Query {
235+
bool: Boolean
236+
}
237+
"""
238+
)
239+
extended_schema = extend_schema(schema, extend_ast)
240+
241+
assert extended_schema.get_type("Int") is None
242+
assert extended_schema.get_type("Float") is None
243+
assert extended_schema.get_type("String") is GraphQLString
244+
assert extended_schema.get_type("Boolean") is GraphQLBoolean
245+
assert extended_schema.get_type("ID") is None
246+
247+
extend_twice_ast = parse(
248+
"""
249+
extend type Query {
250+
int: Int
251+
float: Float
252+
id: ID
253+
}
254+
"""
255+
)
256+
extended_twice_schema = extend_schema(schema, extend_twice_ast)
257+
258+
assert extended_twice_schema.get_type("Int") is GraphQLInt
259+
assert extended_twice_schema.get_type("Float") is GraphQLFloat
260+
assert extended_twice_schema.get_type("String") is GraphQLString
261+
assert extended_twice_schema.get_type("Boolean") is GraphQLBoolean
262+
assert extended_twice_schema.get_type("ID") is GraphQLID
263+
213264
def extends_enums_by_adding_new_values():
214265
extended_schema = extend_test_schema(
215266
"""

0 commit comments

Comments
 (0)