Skip to content

Commit ad38c76

Browse files
committed
build_schema: allow to reference introspection types
Replicates graphql/graphql-js@1597f5d
1 parent 597fc28 commit ad38c76

File tree

3 files changed

+301
-247
lines changed

3 files changed

+301
-247
lines changed

src/graphql/validation/rules/known_type_names.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
NamedTypeNode,
1010
TypeDefinitionNode,
1111
)
12-
from ...type import specified_scalar_types
12+
from ...type import introspection_types, specified_scalar_types
1313
from ...pyutils import did_you_mean, suggestion_list
1414
from . import ASTValidationRule, ValidationContext, SDLValidationContext
1515

@@ -55,12 +55,12 @@ def enter_named_type(
5555
except IndexError:
5656
definition_node = parent
5757
is_sdl = is_sdl_node(definition_node)
58-
if is_sdl and type_name in specified_scalar_types:
58+
if is_sdl and type_name in standard_type_names:
5959
return
6060

6161
suggested_types = suggestion_list(
6262
type_name,
63-
list(specified_scalar_types) + self.type_names
63+
list(standard_type_names) + self.type_names
6464
if is_sdl
6565
else self.type_names,
6666
)
@@ -72,6 +72,9 @@ def enter_named_type(
7272
)
7373

7474

75+
standard_type_names = set(specified_scalar_types).union(introspection_types)
76+
77+
7578
def is_sdl_node(value: Union[Node, Collection[Node], None]) -> bool:
7679
return (
7780
value is not None

tests/utilities/test_build_ast_schema.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
assert_object_type,
2929
assert_scalar_type,
3030
assert_union_type,
31+
introspection_types,
3132
validate_schema,
3233
)
3334
from graphql.utilities import build_ast_schema, build_schema, print_schema, print_type
@@ -1208,6 +1209,35 @@ def can_build_invalid_schema():
12081209
errors = validate_schema(schema)
12091210
assert errors
12101211

1212+
def do_not_override_standard_types():
1213+
# Note: not sure it's desired behaviour to just silently ignore override
1214+
# attempts so just documenting it here.
1215+
1216+
schema = build_schema(
1217+
"""
1218+
scalar ID
1219+
1220+
scalar __Schema
1221+
"""
1222+
)
1223+
1224+
assert schema.get_type("ID") is GraphQLID
1225+
assert schema.get_type("__Schema") is introspection_types["__Schema"]
1226+
1227+
def allows_to_reference_introspection_types():
1228+
schema = build_schema(
1229+
"""
1230+
type Query {
1231+
introspectionField: __EnumValue
1232+
}
1233+
"""
1234+
)
1235+
1236+
query_type = assert_object_type(schema.get_type("Query"))
1237+
__EnumValue = introspection_types["__EnumValue"]
1238+
assert query_type.fields["introspectionField"].type is __EnumValue
1239+
assert schema.get_type("__EnumValue") is introspection_types["__EnumValue"]
1240+
12111241
def rejects_invalid_sdl():
12121242
sdl = """
12131243
type Query {

0 commit comments

Comments
 (0)