3
3
import re
4
4
from ..error import GraphQLError , format_error
5
5
from ..utils import type_from_ast , is_nullish
6
- from ..language import kinds as Kind
6
+ from ..language import ast
7
7
from .values import get_variable_values , get_argument_values
8
8
from ..type .definition import (
9
9
GraphQLScalarType ,
@@ -53,21 +53,21 @@ class ExecutionContext(object):
53
53
54
54
Namely, schema of the type system that is currently executing,
55
55
and the fragments defined in the query document"""
56
- def __init__ (self , schema , root , ast , operation_name , args ):
56
+ def __init__ (self , schema , root , document_ast , operation_name , args ):
57
57
"""Constructs a ExecutionContext object from the arguments passed
58
58
to execute, which we will pass throughout the other execution
59
59
methods."""
60
60
errors = []
61
61
operations = {}
62
62
fragments = {}
63
- for statement in ast [ ' definitions' ] :
64
- if statement [ 'kind' ] == Kind . OPERATION_DEFINITION :
63
+ for statement in document_ast . definitions :
64
+ if isinstance ( statement , ast . OperationDefinition ) :
65
65
name = ''
66
- if statement .get ( ' name' ) :
67
- name = statement [ ' name' ][ ' value' ]
66
+ if statement .name :
67
+ name = statement . name . value
68
68
operations [name ] = statement
69
- elif statement [ 'kind' ] == Kind . FRAGMENT_DEFINITION :
70
- fragments [statement [ ' name' ][ ' value' ] ] = statement
69
+ elif isinstance ( statement , ast . FragmentDefinition ) :
70
+ fragments [statement . name . value ] = statement
71
71
if not operation_name and len (operations ) != 1 :
72
72
raise GraphQLError (
73
73
'Must provide operation name '
@@ -76,7 +76,7 @@ def __init__(self, schema, root, ast, operation_name, args):
76
76
operation = operations .get (op_name )
77
77
if not operation :
78
78
raise GraphQLError ('Unknown operation name: {}' .format (op_name ))
79
- variables = get_variable_values (schema , operation [ 'variableDefinitions' ] or [], args )
79
+ variables = get_variable_values (schema , operation . variable_definitions or [], args )
80
80
81
81
self .schema = schema
82
82
self .fragments = fragments
@@ -113,14 +113,14 @@ def execute(schema, root, ast, operation_name='', args=None):
113
113
def execute_operation (ctx , root , operation ):
114
114
"""Implements the "Evaluating operations" section of the spec."""
115
115
type = get_operation_root_type (ctx .schema , operation )
116
- fields = collect_fields (ctx , type , operation [ 'selectionSet' ] , {}, set ())
117
- if operation [ ' operation' ] == 'mutation' :
116
+ fields = collect_fields (ctx , type , operation . selection_set , {}, set ())
117
+ if operation . operation == 'mutation' :
118
118
return execute_fields_serially (ctx , type , root , fields )
119
119
return execute_fields (ctx , type , root , fields )
120
120
121
121
122
122
def get_operation_root_type (schema , operation ):
123
- op = operation [ ' operation' ]
123
+ op = operation . operation
124
124
if op == 'query' :
125
125
return schema .get_query_type ()
126
126
elif op == 'mutation' :
@@ -156,37 +156,36 @@ def execute_fields(ctx, parent_type, source, fields):
156
156
157
157
158
158
def collect_fields (ctx , type , selection_set , fields , prev_fragment_names ):
159
- for selection in selection_set ['selections' ]:
160
- kind = selection ['kind' ]
161
- directives = selection .get ('directives' )
162
- if kind == Kind .FIELD :
159
+ for selection in selection_set .selections :
160
+ directives = selection .directives
161
+ if isinstance (selection , ast .Field ):
163
162
if not should_include_node (ctx , directives ):
164
163
continue
165
164
name = get_field_entry_key (selection )
166
165
if name not in fields :
167
166
fields [name ] = []
168
167
fields [name ].append (selection )
169
- elif kind == Kind . INLINE_FRAGMENT :
168
+ elif isinstance ( selection , ast . InlineFragment ) :
170
169
if not should_include_node (ctx , directives ) or \
171
170
not does_fragment_condition_match (ctx , selection , type ):
172
171
continue
173
172
collect_fields (
174
- ctx , type , selection [ 'selectionSet' ] ,
173
+ ctx , type , selection . selection_set ,
175
174
fields , prev_fragment_names )
176
- elif kind == Kind . FRAGMENT_SPREAD :
177
- frag_name = selection [ ' name' ][ ' value' ]
175
+ elif isinstance ( selection , ast . FragmentSpread ) :
176
+ frag_name = selection . name . value
178
177
if frag_name in prev_fragment_names or \
179
178
not should_include_node (ctx , directives ):
180
179
continue
181
180
prev_fragment_names .add (frag_name )
182
181
fragment = ctx .fragments .get (frag_name )
183
- frag_directives = fragment .get ( ' directives' )
182
+ frag_directives = fragment .directives
184
183
if not fragment or \
185
184
not should_include_node (ctx , frag_directives ) or \
186
185
not does_fragment_condition_match (ctx , fragment , type ):
187
186
continue
188
187
collect_fields (
189
- ctx , type , fragment [ 'selectionSet' ] ,
188
+ ctx , type , fragment . selection_set ,
190
189
fields , prev_fragment_names )
191
190
return fields
192
191
@@ -197,26 +196,26 @@ def should_include_node(ctx, directives):
197
196
if directives :
198
197
skip_ast = None
199
198
for directive in directives :
200
- if directive [ ' name' ][ ' value' ] == GraphQLSkipDirective .name :
199
+ if directive . name . value == GraphQLSkipDirective .name :
201
200
skip_ast = directive
202
201
break
203
202
if skip_ast :
204
203
args = get_argument_values (
205
204
GraphQLSkipDirective .args ,
206
- skip_ast [ ' arguments' ] ,
205
+ skip_ast . arguments ,
207
206
ctx .variables ,
208
207
)
209
208
return not args .get ('if' )
210
209
211
210
include_ast = None
212
211
for directive in directives :
213
- if directive [ ' name' ][ ' value' ] == GraphQLIncludeDirective .name :
212
+ if directive . name . value == GraphQLIncludeDirective .name :
214
213
include_ast = directive
215
214
break
216
215
if include_ast :
217
216
args = get_argument_values (
218
217
GraphQLIncludeDirective .args ,
219
- include_ast [ ' arguments' ] ,
218
+ include_ast . arguments ,
220
219
ctx .variables ,
221
220
)
222
221
return bool (args .get ('if' ))
@@ -225,7 +224,7 @@ def should_include_node(ctx, directives):
225
224
226
225
227
226
def does_fragment_condition_match (ctx , fragment , type_ ):
228
- conditional_type = type_from_ast (ctx .schema , fragment [ 'typeCondition' ] )
227
+ conditional_type = type_from_ast (ctx .schema , fragment . type_condition )
229
228
if type (conditional_type ) == type (type_ ):
230
229
return True
231
230
if isinstance (conditional_type , (GraphQLInterfaceType , GraphQLUnionType )):
@@ -235,9 +234,9 @@ def does_fragment_condition_match(ctx, fragment, type_):
235
234
236
235
def get_field_entry_key (node ):
237
236
"""Implements the logic to compute the key of a given field’s entry"""
238
- if node [ ' alias' ] :
239
- return node [ ' alias' ][ ' value' ]
240
- return node [ ' name' ][ ' value' ]
237
+ if node . alias :
238
+ return node . alias . value
239
+ return node . name . value
241
240
242
241
243
242
def resolve_field (ctx , parent_type , source , field_asts ):
@@ -256,7 +255,7 @@ def resolve_field(ctx, parent_type, source, field_asts):
256
255
# TODO: find a way to memoize, in case this field is within a list type.
257
256
if field_def .args is not None :
258
257
args = get_argument_values (
259
- field_def .args , field_ast [ ' arguments' ] , ctx .variables
258
+ field_def .args , field_ast . arguments , ctx .variables
260
259
)
261
260
else :
262
261
args = None
@@ -358,7 +357,7 @@ def complete_value(ctx, field_type, field_asts, result):
358
357
subfield_asts = {}
359
358
visited_fragment_names = set ()
360
359
for field_ast in field_asts :
361
- selection_set = field_ast .get ( 'selectionSet' )
360
+ selection_set = field_ast .selection_set
362
361
if selection_set :
363
362
subfield_asts = collect_fields (
364
363
ctx , object_type , selection_set ,
@@ -377,7 +376,7 @@ def camel_to_snake_case(name):
377
376
def default_resolve_fn (source , args , root , field_ast , * _ ):
378
377
"""If a resolve function is not given, then a default resolve behavior is used which takes the property of the source object
379
378
of the same name as the field and returns it as the result, or if it's a function, returns the result of calling that function."""
380
- name = field_ast [ ' name' ][ ' value' ]
379
+ name = field_ast . name . value
381
380
property = getattr (source , name , None )
382
381
if property is None :
383
382
property = getattr (source , camel_to_snake_case (name ), None )
@@ -394,7 +393,7 @@ def get_field_def(schema, parent_type, field_ast):
394
393
are allowed, like on a Union. __schema could get automatically
395
394
added to the query type, but that would require mutating type
396
395
definitions, which would cause issues."""
397
- name = field_ast [ ' name' ][ ' value' ]
396
+ name = field_ast . name . value
398
397
if name == SchemaMetaFieldDef .name and \
399
398
schema .get_query_type () == parent_type :
400
399
return SchemaMetaFieldDef
0 commit comments