Debian Codegraph @

src/graphql/utilities/resolve_schema_coordinate.py

raw
1 from typing import NamedTuple, Optional, Union
2
3 from ..language import (
4 ArgumentCoordinateNode,
5 DirectiveArgumentCoordinateNode,
6 DirectiveCoordinateNode,
7 MemberCoordinateNode,
8 SchemaCoordinateNode,
9 Source,
10 TypeCoordinateNode,
11 parse_schema_coordinate,
12 )
13 from ..pyutils import inspect
14 from ..type import (
15 GraphQLArgument,
16 GraphQLDirective,
17 GraphQLEnumType,
18 GraphQLEnumValue,
19 GraphQLField,
20 GraphQLInputField,
21 GraphQLInputObjectType,
22 GraphQLInterfaceType,
23 GraphQLNamedType,
24 GraphQLObjectType,
25 GraphQLSchema,
26 )
27
28 __all__ = [
29 "resolve_schema_coordinate",
30 "resolve_ast_schema_coordinate",
31 "ResolvedNamedType",
32 "ResolvedField",
33 "ResolvedInputField",
34 "ResolvedEnumValue",
35 "ResolvedFieldArgument",
36 "ResolvedDirective",
37 "ResolvedDirectiveArgument",
38 "ResolvedSchemaElement",
39 ]
40
41
42 class ResolvedNamedType(NamedTuple):
43 """A named type resolved from a schema coordinate."""
44
45 type: GraphQLNamedType
46 kind: str = "NamedType"
47
48
49 class ResolvedField(NamedTuple):
50 """A field resolved from a schema coordinate."""
51
52 type: Union[GraphQLObjectType, GraphQLInterfaceType]
53 field: GraphQLField
54 kind: str = "Field"
55
56
57 class ResolvedInputField(NamedTuple):
58 """An input field resolved from a schema coordinate."""
59
60 type: GraphQLInputObjectType
61 input_field: GraphQLInputField
62 kind: str = "InputField"
63
64
65 class ResolvedEnumValue(NamedTuple):
66 """An enum value resolved from a schema coordinate."""
67
68 type: GraphQLEnumType
69 enum_value: GraphQLEnumValue
70 kind: str = "EnumValue"
71
72
73 class ResolvedFieldArgument(NamedTuple):
74 """A field argument resolved from a schema coordinate."""
75
76 type: Union[GraphQLObjectType, GraphQLInterfaceType]
77 field: GraphQLField
78 field_argument: GraphQLArgument
79 kind: str = "FieldArgument"
80
81
82 class ResolvedDirective(NamedTuple):
83 """A directive resolved from a schema coordinate."""
84
85 directive: GraphQLDirective
86 kind: str = "Directive"
87
88
89 class ResolvedDirectiveArgument(NamedTuple):
90 """A directive argument resolved from a schema coordinate."""
91
92 directive: GraphQLDirective
93 directive_argument: GraphQLArgument
94 kind: str = "DirectiveArgument"
95
96
97 ResolvedSchemaElement = Union[
98 ResolvedNamedType,
99 ResolvedField,
100 ResolvedInputField,
101 ResolvedEnumValue,
102 ResolvedFieldArgument,
103 ResolvedDirective,
104 ResolvedDirectiveArgument,
105 ]
106
107
108 def resolve_schema_coordinate(
109 schema: GraphQLSchema, schema_coordinate: Union[str, Source]
110 ) -> Optional[ResolvedSchemaElement]:
111 """Resolve a string schema coordinate in the context of a GraphQL schema.
112
113 A schema coordinate is resolved in the context of a GraphQL schema to uniquely
114 identify a schema element. It returns None if the schema coordinate does not
115 resolve to a schema element, meta-field, or introspection schema element. It will
116 raise an error if the containing schema element (if applicable) does not exist.
117
118 `<https://spec.graphql.org/draft/#sec-Schema-Coordinates.Semantics>`_
119 """
120 return resolve_ast_schema_coordinate(
121 schema, parse_schema_coordinate(schema_coordinate)
122 )
123
124
125 def resolve_type_coordinate(
126 schema: GraphQLSchema, schema_coordinate: TypeCoordinateNode
127 ) -> Optional[ResolvedNamedType]:
128 """TypeCoordinate : Name"""
129 # 1. Let {typeName} be the value of {Name}.
130 type_name = schema_coordinate.name.value
131 type_ = schema.get_type(type_name)
132
133 # 2. Return the type in the {schema} named {typeName} if it exists.
134 if type_ is None:
135 return None
136
137 return ResolvedNamedType(type_)
138
139
140 def resolve_member_coordinate(
141 schema: GraphQLSchema, schema_coordinate: MemberCoordinateNode
142 ) -> Optional[Union[ResolvedField, ResolvedInputField, ResolvedEnumValue]]:
143 """MemberCoordinate : Name . Name"""
144 # 1. Let {typeName} be the value of the first {Name}.
145 # 2. Let {type} be the type in the {schema} named {typeName}.
146 type_name = schema_coordinate.name.value
147 type_ = schema.get_type(type_name)
148
149 # 3. Assert: {type} must exist, and must be an Enum, Input Object, Object or
150 # Interface type.
151 if type_ is None:
152 raise TypeError(
153 f"Expected {inspect(type_name)} to be defined as a type in the schema."
154 )
155 if not isinstance(
156 type_,
157 (
158 GraphQLEnumType,
159 GraphQLInputObjectType,
160 GraphQLObjectType,
161 GraphQLInterfaceType,
162 ),
163 ):
164 raise TypeError(
165 f"Expected {inspect(type_name)}"
166 " to be an Enum, Input Object, Object or Interface type."
167 )
168
169 member_name = schema_coordinate.member_name.value
170
171 # 4. If {type} is an Enum type:
172 if isinstance(type_, GraphQLEnumType):
173 # 1. Let {enumValueName} be the value of the second {Name}.
174 # 2. Return the enum value of {type} named {enumValueName} if it exists.
175 enum_value = type_.values.get(member_name)
176 if enum_value is None:
177 return None
178 return ResolvedEnumValue(type_, enum_value)
179
180 # 5. Otherwise, if {type} is an Input Object type:
181 if isinstance(type_, GraphQLInputObjectType):
182 # 1. Let {inputFieldName} be the value of the second {Name}.
183 # 2. Return the input field of {type} named {inputFieldName} if it exists.
184 input_field = type_.fields.get(member_name)
185 if input_field is None:
186 return None
187 return ResolvedInputField(type_, input_field)
188
189 # 6. Otherwise:
190 # 1. Let {fieldName} be the value of the second {Name}.
191 # 2. Return the field of {type} named {fieldName} if it exists.
192 field = type_.fields.get(member_name)
193 if field is None:
194 return None
195 return ResolvedField(type_, field)
196
197
198 def resolve_argument_coordinate(
199 schema: GraphQLSchema, schema_coordinate: ArgumentCoordinateNode
200 ) -> Optional[ResolvedFieldArgument]:
201 """ArgumentCoordinate : Name . Name ( Name : )"""
202 # 1. Let {typeName} be the value of the first {Name}.
203 # 2. Let {type} be the type in the {schema} named {typeName}.
204 type_name = schema_coordinate.name.value
205 type_ = schema.get_type(type_name)
206
207 # 3. Assert: {type} must exist, and be an Object or Interface type.
208 if type_ is None:
209 raise TypeError(
210 f"Expected {inspect(type_name)} to be defined as a type in the schema."
211 )
212 if not isinstance(type_, (GraphQLObjectType, GraphQLInterfaceType)):
213 raise TypeError(
214 f"Expected {inspect(type_name)} to be an object type or interface type."
215 )
216
217 # 4. Let {fieldName} be the value of the second {Name}.
218 # 5. Let {field} be the field of {type} named {fieldName}.
219 field_name = schema_coordinate.field_name.value
220 field = type_.fields.get(field_name)
221
222 # 6. Assert: {field} must exist.
223 if field is None:
224 raise TypeError(
225 f"Expected {inspect(field_name)} to exist as a field"
226 f" of type {inspect(type_name)} in the schema."
227 )
228
229 # 7. Let {fieldArgumentName} be the value of the third {Name}.
230 field_argument_name = schema_coordinate.argument_name.value
231 field_argument = field.args.get(field_argument_name)
232
233 # 8. Return the argument of {field} named {fieldArgumentName} if it exists.
234 if field_argument is None:
235 return None
236
237 return ResolvedFieldArgument(type_, field, field_argument)
238
239
240 def resolve_directive_coordinate(
241 schema: GraphQLSchema, schema_coordinate: DirectiveCoordinateNode
242 ) -> Optional[ResolvedDirective]:
243 """DirectiveCoordinate : @ Name"""
244 # 1. Let {directiveName} be the value of {Name}.
245 directive_name = schema_coordinate.name.value
246 directive = schema.get_directive(directive_name)
247
248 # 2. Return the directive in the {schema} named {directiveName} if it exists.
249 if directive is None:
250 return None
251
252 return ResolvedDirective(directive)
253
254
255 def resolve_directive_argument_coordinate(
256 schema: GraphQLSchema, schema_coordinate: DirectiveArgumentCoordinateNode
257 ) -> Optional[ResolvedDirectiveArgument]:
258 """DirectiveArgumentCoordinate : @ Name ( Name : )"""
259 # 1. Let {directiveName} be the value of the first {Name}.
260 # 2. Let {directive} be the directive in the {schema} named {directiveName}.
261 directive_name = schema_coordinate.name.value
262 directive = schema.get_directive(directive_name)
263
264 # 3. Assert {directive} must exist.
265 if directive is None:
266 raise TypeError(
267 f"Expected {inspect(directive_name)}"
268 " to be defined as a directive in the schema."
269 )
270
271 # 4. Let {directiveArgumentName} be the value of the second {Name}.
272 directive_argument_name = schema_coordinate.argument_name.value
273 directive_argument = directive.args.get(directive_argument_name)
274
275 # 5. Return the argument of {directive} named {directiveArgumentName} if it exists.
276 if directive_argument is None:
277 return None
278
279 return ResolvedDirectiveArgument(directive, directive_argument)
280
281
282 def resolve_ast_schema_coordinate(
283 schema: GraphQLSchema, schema_coordinate: SchemaCoordinateNode
284 ) -> Optional[ResolvedSchemaElement]:
285 """Resolve schema coordinate from a parsed SchemaCoordinate node."""
286 if isinstance(schema_coordinate, TypeCoordinateNode):
287 return resolve_type_coordinate(schema, schema_coordinate)
288 if isinstance(schema_coordinate, MemberCoordinateNode):
289 return resolve_member_coordinate(schema, schema_coordinate)
290 if isinstance(schema_coordinate, ArgumentCoordinateNode):
291 return resolve_argument_coordinate(schema, schema_coordinate)
292 if isinstance(schema_coordinate, DirectiveCoordinateNode):
293 return resolve_directive_coordinate(schema, schema_coordinate)
294 # DirectiveArgumentCoordinateNode is the only remaining kind.
295 return resolve_directive_argument_coordinate(schema, schema_coordinate)