| 1 |
from typing import cast, Union |
| 2 |
|
| 3 |
from ..language import DocumentNode, Source, parse |
| 4 |
from ..type import ( |
| 5 |
GraphQLObjectType, |
| 6 |
GraphQLSchema, |
| 7 |
GraphQLSchemaKwargs, |
| 8 |
specified_directives, |
| 9 |
) |
| 10 |
from .extend_schema import ExtendSchemaImpl |
| 11 |
|
| 12 |
__all__ = [ |
| 13 |
"build_ast_schema", |
| 14 |
"build_schema", |
| 15 |
] |
| 16 |
|
| 17 |
|
| 18 |
def build_ast_schema( |
| 19 |
document_ast: DocumentNode, |
| 20 |
assume_valid: bool = False, |
| 21 |
assume_valid_sdl: bool = False, |
| 22 |
) -> GraphQLSchema: |
| 23 |
"""Build a GraphQL Schema from a given AST. |
| 24 |
|
| 25 |
This takes the ast of a schema document produced by the parse function in |
| 26 |
src/language/parser.py. |
| 27 |
|
| 28 |
If no schema definition is provided, then it will look for types named Query, |
| 29 |
Mutation and Subscription. |
| 30 |
|
| 31 |
Given that AST it constructs a GraphQLSchema. The resulting schema has no |
| 32 |
resolve methods, so execution will use default resolvers. |
| 33 |
|
| 34 |
When building a schema from a GraphQL service's introspection result, it might |
| 35 |
be safe to assume the schema is valid. Set ``assume_valid`` to ``True`` to assume |
| 36 |
the produced schema is valid. Set ``assume_valid_sdl`` to ``True`` to assume it is |
| 37 |
already a valid SDL document. |
| 38 |
""" |
| 39 |
if not isinstance(document_ast, DocumentNode): |
| 40 |
raise TypeError("Must provide valid Document AST.") |
| 41 |
|
| 42 |
if not (assume_valid or assume_valid_sdl): |
| 43 |
from ..validation.validate import assert_valid_sdl |
| 44 |
|
| 45 |
assert_valid_sdl(document_ast) |
| 46 |
|
| 47 |
empty_schema_kwargs = GraphQLSchemaKwargs( |
| 48 |
query=None, |
| 49 |
mutation=None, |
| 50 |
subscription=None, |
| 51 |
description=None, |
| 52 |
types=(), |
| 53 |
directives=(), |
| 54 |
extensions={}, |
| 55 |
ast_node=None, |
| 56 |
extension_ast_nodes=(), |
| 57 |
assume_valid=False, |
| 58 |
) |
| 59 |
schema_kwargs = ExtendSchemaImpl.extend_schema_args( |
| 60 |
empty_schema_kwargs, document_ast, assume_valid |
| 61 |
) |
| 62 |
|
| 63 |
if not schema_kwargs["ast_node"]: |
| 64 |
for type_ in schema_kwargs["types"] or (): |
| 65 |
# Note: While this could make early assertions to get the correctly |
| 66 |
# typed values below, that would throw immediately while type system |
| 67 |
# validation with validate_schema() will produce more actionable results. |
| 68 |
type_name = type_.name |
| 69 |
if type_name == "Query": |
| 70 |
schema_kwargs["query"] = cast(GraphQLObjectType, type_) |
| 71 |
elif type_name == "Mutation": |
| 72 |
schema_kwargs["mutation"] = cast(GraphQLObjectType, type_) |
| 73 |
elif type_name == "Subscription": |
| 74 |
schema_kwargs["subscription"] = cast(GraphQLObjectType, type_) |
| 75 |
|
| 76 |
# If specified directives were not explicitly declared, add them. |
| 77 |
directives = schema_kwargs["directives"] |
| 78 |
directive_names = set(directive.name for directive in directives) |
| 79 |
missing_directives = [] |
| 80 |
for directive in specified_directives: |
| 81 |
if directive.name not in directive_names: |
| 82 |
missing_directives.append(directive) |
| 83 |
if missing_directives: |
| 84 |
schema_kwargs["directives"] = directives + tuple(missing_directives) |
| 85 |
|
| 86 |
return GraphQLSchema(**schema_kwargs) |
| 87 |
|
| 88 |
|
| 89 |
def build_schema( |
| 90 |
source: Union[str, Source], |
| 91 |
assume_valid: bool = False, |
| 92 |
assume_valid_sdl: bool = False, |
| 93 |
no_location: bool = False, |
| 94 |
allow_legacy_fragment_variables: bool = False, |
| 95 |
experimental_directives_on_directive_definitions: bool = False, |
| 96 |
) -> GraphQLSchema: |
| 97 |
"""Build a GraphQLSchema directly from a source document.""" |
| 98 |
return build_ast_schema( |
| 99 |
parse( |
| 100 |
source, |
| 101 |
no_location=no_location, |
| 102 |
allow_legacy_fragment_variables=allow_legacy_fragment_variables, |
| 103 |
experimental_directives_on_directive_definitions=( |
| 104 |
experimental_directives_on_directive_definitions |
| 105 |
), |
| 106 |
), |
| 107 |
assume_valid=assume_valid, |
| 108 |
assume_valid_sdl=assume_valid_sdl, |
| 109 |
) |