Debian Codegraph @

src/graphql/utilities/lexicographic_sort_schema.py

raw
1 from typing import Collection, Dict, Optional, Tuple, Union, cast
2
3 from ..language import DirectiveLocation
4 from ..pyutils import inspect, merge_kwargs, natural_comparison_key
5 from ..type import (
6 GraphQLArgument,
7 GraphQLDirective,
8 GraphQLEnumType,
9 GraphQLEnumValue,
10 GraphQLField,
11 GraphQLInputField,
12 GraphQLInputObjectType,
13 GraphQLInputType,
14 GraphQLInterfaceType,
15 GraphQLList,
16 GraphQLNamedType,
17 GraphQLNonNull,
18 GraphQLObjectType,
19 GraphQLSchema,
20 GraphQLUnionType,
21 is_enum_type,
22 is_input_object_type,
23 is_interface_type,
24 is_introspection_type,
25 is_list_type,
26 is_non_null_type,
27 is_object_type,
28 is_scalar_type,
29 is_union_type,
30 )
31
32 __all__ = ["lexicographic_sort_schema"]
33
34
35 def lexicographic_sort_schema(schema: GraphQLSchema) -> GraphQLSchema:
36 """Sort GraphQLSchema.
37
38 This function returns a sorted copy of the given GraphQLSchema.
39 """
40
41 def replace_type(
42 type_: Union[GraphQLList, GraphQLNonNull, GraphQLNamedType],
43 ) -> Union[GraphQLList, GraphQLNonNull, GraphQLNamedType]:
44 if is_list_type(type_):
45 return GraphQLList(replace_type(cast(GraphQLList, type_).of_type))
46 if is_non_null_type(type_):
47 return GraphQLNonNull(replace_type(cast(GraphQLNonNull, type_).of_type))
48 return replace_named_type(cast(GraphQLNamedType, type_))
49
50 def replace_named_type(type_: GraphQLNamedType) -> GraphQLNamedType:
51 return type_map[type_.name]
52
53 def replace_maybe_type(
54 maybe_type: Optional[GraphQLNamedType],
55 ) -> Optional[GraphQLNamedType]:
56 return maybe_type and replace_named_type(maybe_type)
57
58 def sort_directive(directive: GraphQLDirective) -> GraphQLDirective:
59 return GraphQLDirective(
60 **merge_kwargs(
61 directive.to_kwargs(),
62 locations=sorted(directive.locations, key=sort_by_name_key),
63 args=sort_args(directive.args),
64 )
65 )
66
67 def sort_args(args_map: Dict[str, GraphQLArgument]) -> Dict[str, GraphQLArgument]:
68 args = {}
69 for name, arg in sorted(args_map.items()):
70 args[name] = GraphQLArgument(
71 **merge_kwargs(
72 arg.to_kwargs(),
73 type_=replace_type(cast(GraphQLNamedType, arg.type)),
74 )
75 )
76 return args
77
78 def sort_fields(fields_map: Dict[str, GraphQLField]) -> Dict[str, GraphQLField]:
79 fields = {}
80 for name, field in sorted(fields_map.items()):
81 fields[name] = GraphQLField(
82 **merge_kwargs(
83 field.to_kwargs(),
84 type_=replace_type(cast(GraphQLNamedType, field.type)),
85 args=sort_args(field.args),
86 )
87 )
88 return fields
89
90 def sort_input_fields(
91 fields_map: Dict[str, GraphQLInputField],
92 ) -> Dict[str, GraphQLInputField]:
93 return {
94 name: GraphQLInputField(
95 **merge_kwargs(
96 field.to_kwargs(),
97 type_=cast(
98 GraphQLInputType,
99 replace_type(cast(GraphQLNamedType, field.type)),
100 ),
101 )
102 )
103 for name, field in sorted(fields_map.items())
104 }
105
106 def sort_types(array: Collection[GraphQLNamedType]) -> Tuple[GraphQLNamedType, ...]:
107 return tuple(
108 replace_named_type(type_) for type_ in sorted(array, key=sort_by_name_key)
109 )
110
111 def sort_named_type(type_: GraphQLNamedType) -> GraphQLNamedType:
112 if is_scalar_type(type_) or is_introspection_type(type_):
113 return type_
114 if is_object_type(type_):
115 type_ = cast(GraphQLObjectType, type_)
116 return GraphQLObjectType(
117 **merge_kwargs(
118 type_.to_kwargs(),
119 interfaces=lambda: sort_types(type_.interfaces),
120 fields=lambda: sort_fields(type_.fields),
121 )
122 )
123 if is_interface_type(type_):
124 type_ = cast(GraphQLInterfaceType, type_)
125 return GraphQLInterfaceType(
126 **merge_kwargs(
127 type_.to_kwargs(),
128 interfaces=lambda: sort_types(type_.interfaces),
129 fields=lambda: sort_fields(type_.fields),
130 )
131 )
132 if is_union_type(type_):
133 type_ = cast(GraphQLUnionType, type_)
134 return GraphQLUnionType(
135 **merge_kwargs(type_.to_kwargs(), types=lambda: sort_types(type_.types))
136 )
137 if is_enum_type(type_):
138 type_ = cast(GraphQLEnumType, type_)
139 return GraphQLEnumType(
140 **merge_kwargs(
141 type_.to_kwargs(),
142 values={
143 name: GraphQLEnumValue(
144 val.value,
145 description=val.description,
146 deprecation_reason=val.deprecation_reason,
147 extensions=val.extensions,
148 ast_node=val.ast_node,
149 )
150 for name, val in sorted(type_.values.items())
151 },
152 )
153 )
154 if is_input_object_type(type_):
155 type_ = cast(GraphQLInputObjectType, type_)
156 return GraphQLInputObjectType(
157 **merge_kwargs(
158 type_.to_kwargs(),
159 fields=lambda: sort_input_fields(type_.fields),
160 )
161 )
162
163 # Not reachable. All possible types have been considered.
164 raise TypeError(f"Unexpected type: {inspect(type_)}.")
165
166 type_map: Dict[str, GraphQLNamedType] = {
167 type_.name: sort_named_type(type_)
168 for type_ in sorted(schema.type_map.values(), key=sort_by_name_key)
169 }
170
171 return GraphQLSchema(
172 **merge_kwargs(
173 schema.to_kwargs(),
174 types=type_map.values(),
175 directives=[
176 sort_directive(directive)
177 for directive in sorted(schema.directives, key=sort_by_name_key)
178 ],
179 query=cast(
180 Optional[GraphQLObjectType], replace_maybe_type(schema.query_type)
181 ),
182 mutation=cast(
183 Optional[GraphQLObjectType], replace_maybe_type(schema.mutation_type)
184 ),
185 subscription=cast(
186 Optional[GraphQLObjectType],
187 replace_maybe_type(schema.subscription_type),
188 ),
189 )
190 )
191
192
193 def sort_by_name_key(
194 type_: Union[GraphQLNamedType, GraphQLDirective, DirectiveLocation],
195 ) -> Tuple:
196 return natural_comparison_key(type_.name)