Skip to content

Commit e45316e

Browse files
committed
build/extend_schema: improve test coverage
Replicates graphql/graphql-js@fd0a3aa
1 parent c39f2ca commit e45316e

File tree

4 files changed

+82
-18
lines changed

4 files changed

+82
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The current stable version 3.0.1 of GraphQL-core is up-to-date
1616
with GraphQL.js version 14.5.8.
1717

1818
All parts of the API are covered by an extensive test suite
19-
of currently 2038 unit tests.
19+
of currently 2044 unit tests.
2020

2121

2222
## Documentation

src/graphql/utilities/extend_schema.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections import defaultdict
2-
from typing import Any, Dict, List, Optional, Union, cast
2+
from typing import Any, Dict, List, Optional, cast
33

44
from ..language import (
55
DirectiveDefinitionNode,
@@ -69,7 +69,7 @@ def extend_schema(
6969
assert_schema(schema)
7070

7171
if not isinstance(document_ast, DocumentNode):
72-
"Must provide valid Document AST."
72+
raise TypeError("Must provide valid Document AST.")
7373

7474
if not (assume_valid or assume_valid_sdl):
7575
from ..validation.validate import assert_valid_sdl_extension
@@ -316,11 +316,6 @@ def resolve_type(type_name: str) -> GraphQLNamedType:
316316
type_map[existing_type_name] = extend_named_type(existing_type)
317317

318318
# Get the extended root operation types.
319-
schema_types: List[Union[SchemaDefinitionNode, SchemaExtensionNode]] = []
320-
if schema_def:
321-
schema_types.append(schema_def)
322-
if schema_extensions:
323-
schema_types.extend(schema_extensions)
324319
operation_types: Dict[OperationType, GraphQLObjectType] = {}
325320
if schema.query_type:
326321
operation_types[OperationType.QUERY] = cast(
@@ -334,10 +329,11 @@ def resolve_type(type_name: str) -> GraphQLNamedType:
334329
operation_types[OperationType.SUBSCRIPTION] = cast(
335330
GraphQLObjectType, replace_named_type(schema.subscription_type)
336331
)
337-
operation_types.update(
338-
# Then, incorporate schema definition and all schema extensions.
339-
ast_builder.get_operation_types(schema_types)
340-
)
332+
# Then, incorporate schema definition and all schema extensions.
333+
if schema_def:
334+
operation_types.update(ast_builder.get_operation_types([schema_def]))
335+
if schema_extensions:
336+
operation_types.update(ast_builder.get_operation_types(schema_extensions))
341337

342338
# Then produce and return a Schema with these types.
343339
get_operation = operation_types.get

tests/utilities/test_build_ast_schema.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ def add(self, _info, x, y):
9191
None,
9292
)
9393

94+
def ignores_non_type_system_definitions():
95+
sdl = """
96+
type Query {
97+
str: String
98+
}
99+
100+
fragment SomeFragment on Query {
101+
str
102+
}
103+
"""
104+
build_schema(sdl)
105+
94106
def empty_type():
95107
sdl = dedent(
96108
"""
@@ -428,9 +440,8 @@ def can_build_recursive_union():
428440
}
429441
"""
430442
)
431-
msg = str(exc_info.value)
432443
assert (
433-
msg == "Hello types must be specified"
444+
str(exc_info.value) == "Hello types must be specified"
434445
" as a collection of GraphQLObjectType instances."
435446
)
436447

@@ -898,8 +909,7 @@ def rejects_invalid_sdl():
898909
"""
899910
with raises(TypeError) as exc_info:
900911
build_schema(sdl)
901-
msg = str(exc_info.value)
902-
assert msg == "Unknown directive '@unknown'."
912+
assert str(exc_info.value) == "Unknown directive '@unknown'."
903913

904914
def allows_to_disable_sdl_validation():
905915
sdl = """
@@ -909,3 +919,21 @@ def allows_to_disable_sdl_validation():
909919
"""
910920
build_schema(sdl, assume_valid=True)
911921
build_schema(sdl, assume_valid_sdl=True)
922+
923+
def throws_on_unknown_types():
924+
sdl = """
925+
type Query {
926+
unknown: UnknownType
927+
}
928+
"""
929+
with raises(TypeError) as exc_info:
930+
build_schema(sdl, assume_valid_sdl=True)
931+
assert str(exc_info.value).endswith("'UnknownType' not found in document.")
932+
933+
def rejects_invalid_ast():
934+
with raises(TypeError) as exc_info:
935+
build_ast_schema(None) # type: ignore
936+
assert str(exc_info.value) == "Must provide valid Document AST."
937+
with raises(TypeError) as exc_info:
938+
build_ast_schema({}) # type: ignore
939+
assert str(exc_info.value) == "Must provide valid Document AST."

tests/utilities/test_extend_schema.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,8 +1189,7 @@ def rejects_invalid_sdl():
11891189

11901190
with raises(TypeError) as exc_info:
11911191
extend_schema(schema, extend_ast)
1192-
msg = str(exc_info.value)
1193-
assert msg == "Unknown directive '@unknown'."
1192+
assert str(exc_info.value) == "Unknown directive '@unknown'."
11941193

11951194
def allows_to_disable_sdl_validation():
11961195
schema = GraphQLSchema()
@@ -1199,6 +1198,30 @@ def allows_to_disable_sdl_validation():
11991198
extend_schema(schema, extend_ast, assume_valid=True)
12001199
extend_schema(schema, extend_ast, assume_valid_sdl=True)
12011200

1201+
def throws_on_unknown_types():
1202+
schema = GraphQLSchema()
1203+
ast = parse(
1204+
"""
1205+
type Query {
1206+
unknown: UnknownType
1207+
}
1208+
"""
1209+
)
1210+
with raises(TypeError) as exc_info:
1211+
extend_schema(schema, ast, assume_valid_sdl=True)
1212+
assert str(exc_info.value).endswith("Unknown type: 'UnknownType'.")
1213+
1214+
def rejects_invalid_ast():
1215+
schema = GraphQLSchema()
1216+
1217+
with raises(TypeError) as exc_info:
1218+
extend_schema(schema, None) # type: ignore
1219+
assert str(exc_info.value) == "Must provide valid Document AST."
1220+
1221+
with raises(TypeError) as exc_info:
1222+
extend_schema(schema, {}) # type: ignore
1223+
assert str(exc_info.value) == "Must provide valid Document AST."
1224+
12021225
def does_not_allow_replacing_a_default_directive():
12031226
schema = GraphQLSchema()
12041227
extend_ast = parse(
@@ -1289,6 +1312,23 @@ def adds_new_root_types_via_schema_extension():
12891312
assert mutation_type.name == "MutationRoot"
12901313
assert print_extension_nodes(extended_schema) == extension_sdl
12911314

1315+
def adds_directive_via_schema_extension():
1316+
schema = build_schema(
1317+
"""
1318+
type Query
1319+
1320+
directive @foo on SCHEMA
1321+
"""
1322+
)
1323+
extension_sdl = dedent(
1324+
"""
1325+
extend schema @foo
1326+
"""
1327+
)
1328+
extended_schema = extend_schema(schema, parse(extension_sdl))
1329+
1330+
assert print_extension_nodes(extended_schema) == extension_sdl
1331+
12921332
def adds_multiple_new_root_types_via_schema_extension():
12931333
schema = build_schema("type Query")
12941334
extend_ast = parse(

0 commit comments

Comments
 (0)