| 1 |
from typing import Any, Callable, Dict, List, Optional, Union, cast |
| 2 |
|
| 3 |
from ..language import print_ast, StringValueNode |
| 4 |
from ..language.block_string import is_printable_as_block_string |
| 5 |
from ..pyutils import inspect |
| 6 |
from ..type import ( |
| 7 |
DEFAULT_DEPRECATION_REASON, |
| 8 |
GraphQLArgument, |
| 9 |
GraphQLDirective, |
| 10 |
GraphQLEnumType, |
| 11 |
GraphQLEnumValue, |
| 12 |
GraphQLInputObjectType, |
| 13 |
GraphQLInputType, |
| 14 |
GraphQLInterfaceType, |
| 15 |
GraphQLNamedType, |
| 16 |
GraphQLObjectType, |
| 17 |
GraphQLScalarType, |
| 18 |
GraphQLSchema, |
| 19 |
GraphQLUnionType, |
| 20 |
is_enum_type, |
| 21 |
is_input_object_type, |
| 22 |
is_interface_type, |
| 23 |
is_introspection_type, |
| 24 |
is_object_type, |
| 25 |
is_scalar_type, |
| 26 |
is_specified_directive, |
| 27 |
is_specified_scalar_type, |
| 28 |
is_union_type, |
| 29 |
) |
| 30 |
from .ast_from_value import ast_from_value |
| 31 |
|
| 32 |
__all__ = ["print_schema", "print_introspection_schema", "print_type", "print_value"] |
| 33 |
|
| 34 |
|
| 35 |
def print_schema(schema: GraphQLSchema) -> str: |
| 36 |
return print_filtered_schema( |
| 37 |
schema, lambda n: not is_specified_directive(n), is_defined_type |
| 38 |
) |
| 39 |
|
| 40 |
|
| 41 |
def print_introspection_schema(schema: GraphQLSchema) -> str: |
| 42 |
return print_filtered_schema(schema, is_specified_directive, is_introspection_type) |
| 43 |
|
| 44 |
|
| 45 |
def is_defined_type(type_: GraphQLNamedType) -> bool: |
| 46 |
return not is_specified_scalar_type(type_) and not is_introspection_type(type_) |
| 47 |
|
| 48 |
|
| 49 |
def print_filtered_schema( |
| 50 |
schema: GraphQLSchema, |
| 51 |
directive_filter: Callable[[GraphQLDirective], bool], |
| 52 |
type_filter: Callable[[GraphQLNamedType], bool], |
| 53 |
) -> str: |
| 54 |
directives = filter(directive_filter, schema.directives) |
| 55 |
types = filter(type_filter, schema.type_map.values()) |
| 56 |
|
| 57 |
return "\n\n".join( |
| 58 |
( |
| 59 |
*filter(None, (print_schema_definition(schema),)), |
| 60 |
*map(print_directive, directives), |
| 61 |
*map(print_type, types), |
| 62 |
) |
| 63 |
) |
| 64 |
|
| 65 |
|
| 66 |
def print_schema_definition(schema: GraphQLSchema) -> Optional[str]: |
| 67 |
if schema.description is None and is_schema_of_common_names(schema): |
| 68 |
return None |
| 69 |
|
| 70 |
operation_types = [] |
| 71 |
|
| 72 |
query_type = schema.query_type |
| 73 |
if query_type: |
| 74 |
operation_types.append(f" query: {query_type.name}") |
| 75 |
|
| 76 |
mutation_type = schema.mutation_type |
| 77 |
if mutation_type: |
| 78 |
operation_types.append(f" mutation: {mutation_type.name}") |
| 79 |
|
| 80 |
subscription_type = schema.subscription_type |
| 81 |
if subscription_type: |
| 82 |
operation_types.append(f" subscription: {subscription_type.name}") |
| 83 |
|
| 84 |
return print_description(schema) + "schema {\n" + "\n".join(operation_types) + "\n}" |
| 85 |
|
| 86 |
|
| 87 |
def is_schema_of_common_names(schema: GraphQLSchema) -> bool: |
| 88 |
"""Check whether this schema uses the common naming convention. |
| 89 |
|
| 90 |
GraphQL schema define root types for each type of operation. These types are the |
| 91 |
same as any other type and can be named in any manner, however there is a common |
| 92 |
naming convention: |
| 93 |
|
| 94 |
schema { |
| 95 |
query: Query |
| 96 |
mutation: Mutation |
| 97 |
subscription: Subscription |
| 98 |
} |
| 99 |
|
| 100 |
When using this naming convention, the schema description can be omitted. |
| 101 |
""" |
| 102 |
query_type = schema.query_type |
| 103 |
if query_type and query_type.name != "Query": |
| 104 |
return False |
| 105 |
|
| 106 |
mutation_type = schema.mutation_type |
| 107 |
if mutation_type and mutation_type.name != "Mutation": |
| 108 |
return False |
| 109 |
|
| 110 |
subscription_type = schema.subscription_type |
| 111 |
return not subscription_type or subscription_type.name == "Subscription" |
| 112 |
|
| 113 |
|
| 114 |
def print_type(type_: GraphQLNamedType) -> str: |
| 115 |
if is_scalar_type(type_): |
| 116 |
type_ = cast(GraphQLScalarType, type_) |
| 117 |
return print_scalar(type_) |
| 118 |
if is_object_type(type_): |
| 119 |
type_ = cast(GraphQLObjectType, type_) |
| 120 |
return print_object(type_) |
| 121 |
if is_interface_type(type_): |
| 122 |
type_ = cast(GraphQLInterfaceType, type_) |
| 123 |
return print_interface(type_) |
| 124 |
if is_union_type(type_): |
| 125 |
type_ = cast(GraphQLUnionType, type_) |
| 126 |
return print_union(type_) |
| 127 |
if is_enum_type(type_): |
| 128 |
type_ = cast(GraphQLEnumType, type_) |
| 129 |
return print_enum(type_) |
| 130 |
if is_input_object_type(type_): |
| 131 |
type_ = cast(GraphQLInputObjectType, type_) |
| 132 |
return print_input_object(type_) |
| 133 |
|
| 134 |
# Not reachable. All possible types have been considered. |
| 135 |
raise TypeError(f"Unexpected type: {inspect(type_)}.") |
| 136 |
|
| 137 |
|
| 138 |
def print_scalar(type_: GraphQLScalarType) -> str: |
| 139 |
return ( |
| 140 |
print_description(type_) |
| 141 |
+ f"scalar {type_.name}" |
| 142 |
+ print_specified_by_url(type_) |
| 143 |
) |
| 144 |
|
| 145 |
|
| 146 |
def print_implemented_interfaces( |
| 147 |
type_: Union[GraphQLObjectType, GraphQLInterfaceType], |
| 148 |
) -> str: |
| 149 |
interfaces = type_.interfaces |
| 150 |
return " implements " + " & ".join(i.name for i in interfaces) if interfaces else "" |
| 151 |
|
| 152 |
|
| 153 |
def print_object(type_: GraphQLObjectType) -> str: |
| 154 |
return ( |
| 155 |
print_description(type_) |
| 156 |
+ f"type {type_.name}" |
| 157 |
+ print_implemented_interfaces(type_) |
| 158 |
+ print_fields(type_) |
| 159 |
) |
| 160 |
|
| 161 |
|
| 162 |
def print_interface(type_: GraphQLInterfaceType) -> str: |
| 163 |
return ( |
| 164 |
print_description(type_) |
| 165 |
+ f"interface {type_.name}" |
| 166 |
+ print_implemented_interfaces(type_) |
| 167 |
+ print_fields(type_) |
| 168 |
) |
| 169 |
|
| 170 |
|
| 171 |
def print_union(type_: GraphQLUnionType) -> str: |
| 172 |
types = type_.types |
| 173 |
possible_types = " = " + " | ".join(t.name for t in types) if types else "" |
| 174 |
return print_description(type_) + f"union {type_.name}" + possible_types |
| 175 |
|
| 176 |
|
| 177 |
def print_enum(type_: GraphQLEnumType) -> str: |
| 178 |
values = [ |
| 179 |
print_description(value, " ", not i) |
| 180 |
+ f" {name}" |
| 181 |
+ print_deprecated(value.deprecation_reason) |
| 182 |
for i, (name, value) in enumerate(type_.values.items()) |
| 183 |
] |
| 184 |
return print_description(type_) + f"enum {type_.name}" + print_block(values) |
| 185 |
|
| 186 |
|
| 187 |
def print_input_object(type_: GraphQLInputObjectType) -> str: |
| 188 |
fields = [ |
| 189 |
print_description(field, " ", not i) + " " + print_input_value(name, field) |
| 190 |
for i, (name, field) in enumerate(type_.fields.items()) |
| 191 |
] |
| 192 |
return ( |
| 193 |
print_description(type_) |
| 194 |
+ f"input {type_.name}" |
| 195 |
+ (" @oneOf" if type_.is_one_of else "") |
| 196 |
+ print_block(fields) |
| 197 |
) |
| 198 |
|
| 199 |
|
| 200 |
def print_fields(type_: Union[GraphQLObjectType, GraphQLInterfaceType]) -> str: |
| 201 |
fields = [ |
| 202 |
print_description(field, " ", not i) |
| 203 |
+ f" {name}" |
| 204 |
+ print_args(field.args, " ") |
| 205 |
+ f": {field.type}" |
| 206 |
+ print_deprecated(field.deprecation_reason) |
| 207 |
for i, (name, field) in enumerate(type_.fields.items()) |
| 208 |
] |
| 209 |
return print_block(fields) |
| 210 |
|
| 211 |
|
| 212 |
def print_block(items: List[str]) -> str: |
| 213 |
return " {\n" + "\n".join(items) + "\n}" if items else "" |
| 214 |
|
| 215 |
|
| 216 |
def print_args(args: Dict[str, GraphQLArgument], indentation: str = "") -> str: |
| 217 |
if not args: |
| 218 |
return "" |
| 219 |
|
| 220 |
# If every arg does not have a description, print them on one line. |
| 221 |
if not any(arg.description for arg in args.values()): |
| 222 |
return ( |
| 223 |
"(" |
| 224 |
+ ", ".join(print_input_value(name, arg) for name, arg in args.items()) |
| 225 |
+ ")" |
| 226 |
) |
| 227 |
|
| 228 |
return ( |
| 229 |
"(\n" |
| 230 |
+ "\n".join( |
| 231 |
print_description(arg, f" {indentation}", not i) |
| 232 |
+ f" {indentation}" |
| 233 |
+ print_input_value(name, arg) |
| 234 |
for i, (name, arg) in enumerate(args.items()) |
| 235 |
) |
| 236 |
+ f"\n{indentation})" |
| 237 |
) |
| 238 |
|
| 239 |
|
| 240 |
def print_input_value(name: str, arg: GraphQLArgument) -> str: |
| 241 |
default_ast = ast_from_value(arg.default_value, arg.type) |
| 242 |
arg_decl = f"{name}: {arg.type}" |
| 243 |
if default_ast: |
| 244 |
arg_decl += f" = {print_ast(default_ast)}" |
| 245 |
return arg_decl + print_deprecated(arg.deprecation_reason) |
| 246 |
|
| 247 |
|
| 248 |
def print_directive(directive: GraphQLDirective) -> str: |
| 249 |
return ( |
| 250 |
print_description(directive) |
| 251 |
+ f"directive @{directive.name}" |
| 252 |
+ print_args(directive.args) |
| 253 |
+ print_deprecated(directive.deprecation_reason) |
| 254 |
+ (" repeatable" if directive.is_repeatable else "") |
| 255 |
+ " on " |
| 256 |
+ " | ".join(location.name for location in directive.locations) |
| 257 |
) |
| 258 |
|
| 259 |
|
| 260 |
def print_deprecated(reason: Optional[str]) -> str: |
| 261 |
if reason is None: |
| 262 |
return "" |
| 263 |
if reason != DEFAULT_DEPRECATION_REASON: |
| 264 |
ast_value = print_ast(StringValueNode(value=reason)) |
| 265 |
return f" @deprecated(reason: {ast_value})" |
| 266 |
return " @deprecated" |
| 267 |
|
| 268 |
|
| 269 |
def print_specified_by_url(scalar: GraphQLScalarType) -> str: |
| 270 |
if scalar.specified_by_url is None: |
| 271 |
return "" |
| 272 |
ast_value = print_ast(StringValueNode(value=scalar.specified_by_url)) |
| 273 |
return f" @specifiedBy(url: {ast_value})" |
| 274 |
|
| 275 |
|
| 276 |
def print_description( |
| 277 |
def_: Union[ |
| 278 |
GraphQLArgument, |
| 279 |
GraphQLDirective, |
| 280 |
GraphQLEnumValue, |
| 281 |
GraphQLNamedType, |
| 282 |
GraphQLSchema, |
| 283 |
], |
| 284 |
indentation: str = "", |
| 285 |
first_in_block: bool = True, |
| 286 |
) -> str: |
| 287 |
description = def_.description |
| 288 |
if description is None: |
| 289 |
return "" |
| 290 |
|
| 291 |
block_string = print_ast( |
| 292 |
StringValueNode( |
| 293 |
value=description, block=is_printable_as_block_string(description) |
| 294 |
) |
| 295 |
) |
| 296 |
|
| 297 |
prefix = "\n" + indentation if indentation and not first_in_block else indentation |
| 298 |
|
| 299 |
return prefix + block_string.replace("\n", "\n" + indentation) + "\n" |
| 300 |
|
| 301 |
|
| 302 |
def print_value(value: Any, type_: GraphQLInputType) -> str: |
| 303 |
"""@deprecated: Convenience function for printing a Python value""" |
| 304 |
return print_ast(ast_from_value(value, type_)) # type: ignore |