Skip to content

Commit bafb41e

Browse files
committed
schema: Always validate arguments
1 parent eef87fc commit bafb41e

File tree

1 file changed

+56
-60
lines changed

1 file changed

+56
-60
lines changed

src/graphql/type/schema.py

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class GraphQLSchema:
102102

103103
_implementations: Dict[str, InterfaceImplementations]
104104
_sub_type_map: Dict[str, Set[str]]
105+
_validation_errors: Optional[List[GraphQLError]]
105106

106107
def __init__(
107108
self,
@@ -122,70 +123,65 @@ def __init__(
122123
check for common mistakes during construction to produce clear and early error
123124
messages.
124125
"""
125-
if assume_valid:
126-
# If this schema was built from a source known to be valid, then it may be
127-
# marked with assume_valid to avoid an additional type system validation.
128-
self._validation_errors: Optional[List[GraphQLError]] = []
126+
# If this schema was built from a source known to be valid, then it may be
127+
# marked with assume_valid to avoid an additional type system validation.
128+
self._validation_errors = [] if assume_valid else None
129+
130+
# Check for common mistakes during construction to produce clear and early
131+
# error messages.
132+
# The query, mutation and subscription types must actually be GraphQL
133+
# object types, but we leave it to the validator to report this error.
134+
if query and not isinstance(query, GraphQLType):
135+
raise TypeError("Expected query to be a GraphQL type.")
136+
if mutation and not isinstance(mutation, GraphQLType):
137+
raise TypeError("Expected mutation to be a GraphQL type.")
138+
if subscription and not isinstance(subscription, GraphQLType):
139+
raise TypeError("Expected subscription to be a GraphQL type.")
140+
if types is None:
141+
types = []
129142
else:
130-
# Otherwise check for common mistakes during construction to produce clear
131-
# and early error messages.
132-
# The query, mutation and subscription types must actually be GraphQL
133-
# object types, but we leave it to the validator to report this error.
134-
if query and not isinstance(query, GraphQLType):
135-
raise TypeError("Expected query to be a GraphQL type.")
136-
if mutation and not isinstance(mutation, GraphQLType):
137-
raise TypeError("Expected mutation to be a GraphQL type.")
138-
if subscription and not isinstance(subscription, GraphQLType):
139-
raise TypeError("Expected subscription to be a GraphQL type.")
140-
if types is None:
141-
types = []
142-
else:
143-
if not is_collection(types) or (
144-
# if reducer has been overridden, don't check types
145-
getattr(self.type_map_reducer, "__func__", None)
146-
is GraphQLSchema.type_map_reducer
147-
and not all(is_named_type(type_) for type_ in types)
148-
):
149-
raise TypeError(
150-
"Schema types must be specified as a collection"
151-
" of GraphQLNamedType instances."
152-
)
153-
if directives is not None:
154-
# noinspection PyUnresolvedReferences
155-
if not is_collection(directives) or (
156-
# if reducer has been overridden, don't check directive types
157-
getattr(self.type_map_directive_reducer, "__func__", None)
158-
is GraphQLSchema.type_map_directive_reducer
159-
and not all(is_directive(directive) for directive in directives)
160-
):
161-
raise TypeError(
162-
"Schema directives must be specified as a collection"
163-
" of GraphQLDirective instances."
164-
)
165-
if not isinstance(directives, FrozenList):
166-
directives = FrozenList(directives)
167-
if extensions is not None and (
168-
not isinstance(extensions, dict)
169-
or not all(isinstance(key, str) for key in extensions)
143+
if not is_collection(types) or (
144+
# if reducer has been overridden, don't check types
145+
getattr(self.type_map_reducer, "__func__", None)
146+
is GraphQLSchema.type_map_reducer
147+
and not all(is_named_type(type_) for type_ in types)
148+
):
149+
raise TypeError(
150+
"Schema types must be specified as a collection"
151+
" of GraphQLNamedType instances."
152+
)
153+
if directives is not None:
154+
# noinspection PyUnresolvedReferences
155+
if not is_collection(directives) or (
156+
# if reducer has been overridden, don't check directive types
157+
getattr(self.type_map_directive_reducer, "__func__", None)
158+
is GraphQLSchema.type_map_directive_reducer
159+
and not all(is_directive(directive) for directive in directives)
160+
):
161+
raise TypeError(
162+
"Schema directives must be specified as a collection"
163+
" of GraphQLDirective instances."
164+
)
165+
if not isinstance(directives, FrozenList):
166+
directives = FrozenList(directives)
167+
if extensions is not None and (
168+
not isinstance(extensions, dict)
169+
or not all(isinstance(key, str) for key in extensions)
170+
):
171+
raise TypeError("Schema extensions must be a dictionary with string keys.")
172+
if ast_node and not isinstance(ast_node, ast.SchemaDefinitionNode):
173+
raise TypeError("Schema AST node must be a SchemaDefinitionNode.")
174+
if extension_ast_nodes:
175+
if not is_collection(extension_ast_nodes) or not all(
176+
isinstance(node, ast.SchemaExtensionNode)
177+
for node in extension_ast_nodes
170178
):
171179
raise TypeError(
172-
"Schema extensions must be a dictionary with string keys."
180+
"Schema extension AST nodes must be specified"
181+
" as a collection of SchemaExtensionNode instances."
173182
)
174-
if ast_node and not isinstance(ast_node, ast.SchemaDefinitionNode):
175-
raise TypeError("Schema AST node must be a SchemaDefinitionNode.")
176-
if extension_ast_nodes:
177-
if not is_collection(extension_ast_nodes) or not all(
178-
isinstance(node, ast.SchemaExtensionNode)
179-
for node in extension_ast_nodes
180-
):
181-
raise TypeError(
182-
"Schema extension AST nodes must be specified"
183-
" as a collection of SchemaExtensionNode instances."
184-
)
185-
if not isinstance(extension_ast_nodes, FrozenList):
186-
extension_ast_nodes = FrozenList(extension_ast_nodes)
187-
188-
self._validation_errors = None
183+
if not isinstance(extension_ast_nodes, FrozenList):
184+
extension_ast_nodes = FrozenList(extension_ast_nodes)
189185

190186
self.extensions = extensions
191187
self.ast_node = ast_node

0 commit comments

Comments
 (0)