graphql/utilities/build_ast_schema.py
def build_ast_schema(
    document_ast: DocumentNode,
    assume_valid: bool = False,
    assume_valid_sdl: bool = False,
) -> GraphQLSchema:
    """Build a GraphQL Schema from a given AST.

    This takes the ast of a schema document produced by the parse function in
    src/language/parser.py.

    If no schema definition is provided, then it will look for types named Query
    and Mutation.

    Given that AST it constructs a GraphQLSchema. The resulting schema has no
    resolve methods, so execution will use default resolvers.

    When building a schema from a GraphQL service's introspection result, it might
    be safe to assume the schema is valid. Set `assume_valid` to True to assume the
    produced schema is valid. Set `assume_valid_sdl` to True to assume it is already
    a valid SDL document.
    """
    if not isinstance(document_ast, DocumentNode):
        raise TypeError("Must provide a Document AST.")

    if not (assume_valid or assume_valid_sdl):
        from ..validation.validate import assert_valid_sdl

        assert_valid_sdl(document_ast)

    schema_def: Optional[SchemaDefinitionNode] = None
    type_defs: List[TypeDefinitionNode] = []
    append_type_def = type_defs.append
    node_map: TypeDefinitionsMap = {}
    directive_defs: List[DirectiveDefinitionNode] = []
    append_directive_def = directive_defs.append
    for def_ in document_ast.definitions:
        if isinstance(def_, SchemaDefinitionNode):
            schema_def = def_
        elif isinstance(def_, TypeDefinitionNode):
            def_ = cast(TypeDefinitionNode, def_)
            type_name = def_.name.value
            if type_name in node_map:
                raise TypeError(f"Type '{type_name}' was defined more than once.")
            append_type_def(def_)
            node_map[type_name] = def_
        elif isinstance(def_, DirectiveDefinitionNode):
            append_directive_def(def_)

    if schema_def:
        operation_types: Dict[OperationType, Any] = get_operation_types(
            schema_def, node_map
        )
    else:
        operation_types = {
            OperationType.QUERY: node_map.get("Query"),
            OperationType.MUTATION: node_map.get("Mutation"),
            OperationType.SUBSCRIPTION: node_map.get("Subscription"),
        }

    def resolve_type(type_ref: NamedTypeNode):
        raise TypeError(f"Type {type_ref.name.value!r} not found in document.")

    definition_builder = ASTDefinitionBuilder(
        node_map, assume_valid=assume_valid, resolve_type=resolve_type
    )

    directives = [
        definition_builder.build_directive(directive_def)
        for directive_def in directive_defs
    ]

    # If specified directives were not explicitly declared, add them.
    if not any(directive.name == "skip" for directive in directives):
        directives.append(GraphQLSkipDirective)
    if not any(directive.name == "include" for directive in directives):
        directives.append(GraphQLIncludeDirective)
    if not any(directive.name == "deprecated" for directive in directives):
        directives.append(GraphQLDeprecatedDirective)

    # Note: While this could make early assertions to get the correctly typed values
    # below, that would throw immediately while type system validation with
    # `validate_schema()` will produce more actionable results.
    query_type = operation_types.get(OperationType.QUERY)
    mutation_type = operation_types.get(OperationType.MUTATION)
    subscription_type = operation_types.get(OperationType.SUBSCRIPTION)
    return GraphQLSchema(
        query=cast(GraphQLObjectType, definition_builder.build_type(query_type))
        if query_type
        else None,
        mutation=cast(GraphQLObjectType, definition_builder.build_type(mutation_type))
        if mutation_type
        else None,
        subscription=cast(
            GraphQLObjectType, definition_builder.build_type(subscription_type)
        )
        if subscription_type
        else None,
        types=[definition_builder.build_type(node) for node in type_defs],
        directives=directives,
        ast_node=schema_def,
        assume_valid=assume_valid,
    )
Build a GraphQL Schema from a given AST.

    This takes the ast of a schema document produced by the parse function in
    src/language/parser.py.

    If no schema definition is provided, then it will look for types named Query
    and Mutation.

    Given that AST it constructs a GraphQLSchema. The resulting schema has no
    resolve methods, so execution will use default resolvers.

    When building a schema from a GraphQL service's introspection result, it might
    be safe to assume the schema is valid. Set `assume_valid` to True to assume the
    produced schema is valid. Set `assume_valid_sdl` to True to assume it is already
    a valid SDL document.
    
Build a GraphQL Schema from a given AST.
This takes the ast of a schema document produced by the parse function in
src/language/parser.py.

If no schema definition is provided, then it will look for types named Query
and Mutation.

Given that AST it constructs a GraphQLSchema. The resulting schema has no
resolve methods, so execution will use default resolvers.

When building a schema from a GraphQL service's introspection result, it might
be safe to assume the schema is valid. Set `assume_valid` to True to assume the
produced schema is valid. Set `assume_valid_sdl` to True to assume it is already
a valid SDL document.
Build a GraphQL Schema from a given AST . This takes the ast of a schema document produced by the parse function in src / language / parser . py . If no schema definition is provided then it will look for types named Query and Mutation . Given that AST it constructs a GraphQLSchema . The resulting schema has no resolve methods so execution will use default resolvers . When building a schema from a GraphQL service ' s introspection result it might be safe to assume the schema is valid . Set ` assume_valid ` to True to assume the produced schema is valid . Set ` assume_valid_sdl ` to True to assume it is already a valid SDL document .
def build_ast_schema(
    document_ast: DocumentNode,
    assume_valid: bool = False,
    assume_valid_sdl: bool = False,
) -> GraphQLSchema:
    if not isinstance(document_ast, DocumentNode):
        raise TypeError("Must provide a Document AST.")
    if not (assume_valid or assume_valid_sdl):
        from ..validation.validate import assert_valid_sdl
        assert_valid_sdl(document_ast)
    schema_def: Optional[SchemaDefinitionNode] = None
    type_defs: List[TypeDefinitionNode] = []
    append_type_def = type_defs.append
    node_map: TypeDefinitionsMap = {}
    directive_defs: List[DirectiveDefinitionNode] = []
    append_directive_def = directive_defs.append
    for def_ in document_ast.definitions:
        if isinstance(def_, SchemaDefinitionNode):
            schema_def = def_
        elif isinstance(def_, TypeDefinitionNode):
            def_ = cast(TypeDefinitionNode, def_)
            type_name = def_.name.value
            if type_name in node_map:
                raise TypeError(f"Type '{type_name}' was defined more than once.")
            append_type_def(def_)
            node_map[type_name] = def_
        elif isinstance(def_, DirectiveDefinitionNode):
            append_directive_def(def_)
    if schema_def:
        operation_types: Dict[OperationType, Any] = get_operation_types(
            schema_def, node_map
        )
    else:
        operation_types = {
            OperationType.QUERY: node_map.get("Query"),
            OperationType.MUTATION: node_map.get("Mutation"),
            OperationType.SUBSCRIPTION: node_map.get("Subscription"),
        }
    def resolve_type(type_ref: NamedTypeNode):
        raise TypeError(f"Type {type_ref.name.value!r} not found in document.")
    definition_builder = ASTDefinitionBuilder(
        node_map, assume_valid=assume_valid, resolve_type=resolve_type
    )
    directives = [
        definition_builder.build_directive(directive_def)
        for directive_def in directive_defs
    ]
    if not any(directive.name == "skip" for directive in directives):
        directives.append(GraphQLSkipDirective)
    if not any(directive.name == "include" for directive in directives):
        directives.append(GraphQLIncludeDirective)
    if not any(directive.name == "deprecated" for directive in directives):
        directives.append(GraphQLDeprecatedDirective)
    query_type = operation_types.get(OperationType.QUERY)
    mutation_type = operation_types.get(OperationType.MUTATION)
    subscription_type = operation_types.get(OperationType.SUBSCRIPTION)
    return GraphQLSchema(
        query=cast(GraphQLObjectType, definition_builder.build_type(query_type))
        if query_type
        else None,
        mutation=cast(GraphQLObjectType, definition_builder.build_type(mutation_type))
        if mutation_type
        else None,
        subscription=cast(
            GraphQLObjectType, definition_builder.build_type(subscription_type)
        )
        if subscription_type
        else None,
        types=[definition_builder.build_type(node) for node in type_defs],
        directives=directives,
        ast_node=schema_def,
        assume_valid=assume_valid,
    )
def build_ast_schema ( document_ast : DocumentNode , assume_valid : bool = False , assume_valid_sdl : bool = False , ) -> GraphQLSchema : if not isinstance ( document_ast , DocumentNode ) : raise TypeError ( "Must provide a Document AST." ) if not ( assume_valid or assume_valid_sdl ) : from . . validation . validate import assert_valid_sdl assert_valid_sdl ( document_ast ) schema_def : Optional [ SchemaDefinitionNode ] = None type_defs : List [ TypeDefinitionNode ] = [ ] append_type_def = type_defs . append node_map : TypeDefinitionsMap = { } directive_defs : List [ DirectiveDefinitionNode ] = [ ] append_directive_def = directive_defs . append for def_ in document_ast . definitions : if isinstance ( def_ , SchemaDefinitionNode ) : schema_def = def_ elif isinstance ( def_ , TypeDefinitionNode ) : def_ = cast ( TypeDefinitionNode , def_ ) type_name = def_ . name . value if type_name in node_map : raise TypeError ( f"Type '{type_name}' was defined more than once." ) append_type_def ( def_ ) node_map [ type_name ] = def_ elif isinstance ( def_ , DirectiveDefinitionNode ) : append_directive_def ( def_ ) if schema_def : operation_types : Dict [ OperationType , Any ] = get_operation_types ( schema_def , node_map ) else : operation_types = { OperationType . QUERY : node_map . get ( "Query" ) , OperationType . MUTATION : node_map . get ( "Mutation" ) , OperationType . SUBSCRIPTION : node_map . get ( "Subscription" ) , } def resolve_type ( type_ref : NamedTypeNode ) : raise TypeError ( f"Type {type_ref.name.value!r} not found in document." ) definition_builder = ASTDefinitionBuilder ( node_map , assume_valid = assume_valid , resolve_type = resolve_type ) directives = [ definition_builder . build_directive ( directive_def ) for directive_def in directive_defs ] if not any ( directive . name == "skip" for directive in directives ) : directives . append ( GraphQLSkipDirective ) if not any ( directive . name == "include" for directive in directives ) : directives . append ( GraphQLIncludeDirective ) if not any ( directive . name == "deprecated" for directive in directives ) : directives . append ( GraphQLDeprecatedDirective ) query_type = operation_types . get ( OperationType . QUERY ) mutation_type = operation_types . get ( OperationType . MUTATION ) subscription_type = operation_types . get ( OperationType . SUBSCRIPTION ) return GraphQLSchema ( query = cast ( GraphQLObjectType , definition_builder . build_type ( query_type ) ) if query_type else None , mutation = cast ( GraphQLObjectType , definition_builder . build_type ( mutation_type ) ) if mutation_type else None , subscription = cast ( GraphQLObjectType , definition_builder . build_type ( subscription_type ) ) if subscription_type else None , types = [ definition_builder . build_type ( node ) for node in type_defs ] , directives = directives , ast_node = schema_def , assume_valid = assume_valid , )
Build a GraphQL Schema from a given AST.
Build a GraphQL Schema from a given AST .
"""Build a GraphQL Schema from a given AST.

    This takes the ast of a schema document produced by the parse function in
    src/language/parser.py.

    If no schema definition is provided, then it will look for types named Query
    and Mutation.

    Given that AST it constructs a GraphQLSchema. The resulting schema has no
    resolve methods, so execution will use default resolvers.

    When building a schema from a GraphQL service's introspection result, it might
    be safe to assume the schema is valid. Set `assume_valid` to True to assume the
    produced schema is valid. Set `assume_valid_sdl` to True to assume it is already
    a valid SDL document.
    """ # If specified directives were not explicitly declared, add them. # Note: While this could make early assertions to get the correctly typed values # below, that would throw immediately while type system validation with # `validate_schema()` will produce more actionable results.
document_ast
DocumentNode
assume_valid
bool
assume_valid_sdl
bool