| 1 |
from typing import cast |
| 2 |
|
| 3 |
from ..error import GraphQLError |
| 4 |
from ..language import parse |
| 5 |
from ..type import GraphQLSchema |
| 6 |
from .get_introspection_query import get_introspection_query, IntrospectionQuery |
| 7 |
|
| 8 |
__all__ = ["introspection_from_schema"] |
| 9 |
|
| 10 |
|
| 11 |
def introspection_from_schema( |
| 12 |
schema: GraphQLSchema, |
| 13 |
descriptions: bool = True, |
| 14 |
specified_by_url: bool = True, |
| 15 |
directive_is_repeatable: bool = True, |
| 16 |
schema_description: bool = True, |
| 17 |
input_value_deprecation: bool = True, |
| 18 |
experimental_directive_deprecation: bool = True, |
| 19 |
input_object_one_of: bool = True, |
| 20 |
) -> IntrospectionQuery: |
| 21 |
"""Build an IntrospectionQuery from a GraphQLSchema |
| 22 |
|
| 23 |
IntrospectionQuery is useful for utilities that care about type and field |
| 24 |
relationships, but do not need to traverse through those relationships. |
| 25 |
|
| 26 |
This is the inverse of build_client_schema. The primary use case is outside of the |
| 27 |
server context, for instance when doing schema comparisons. |
| 28 |
""" |
| 29 |
document = parse( |
| 30 |
get_introspection_query( |
| 31 |
descriptions, |
| 32 |
specified_by_url, |
| 33 |
directive_is_repeatable, |
| 34 |
schema_description, |
| 35 |
input_value_deprecation, |
| 36 |
experimental_directive_deprecation, |
| 37 |
input_object_one_of, |
| 38 |
) |
| 39 |
) |
| 40 |
|
| 41 |
from ..execution.execute import execute_sync, ExecutionResult |
| 42 |
|
| 43 |
result = execute_sync(schema, document) |
| 44 |
if not isinstance(result, ExecutionResult): # pragma: no cover |
| 45 |
raise RuntimeError("Introspection cannot be executed") |
| 46 |
if result.errors: # pragma: no cover |
| 47 |
raise result.errors[0] |
| 48 |
if not result.data: # pragma: no cover |
| 49 |
raise GraphQLError("Introspection did not return a result") |
| 50 |
return cast(IntrospectionQuery, result.data) |