@@ -32,24 +32,33 @@ def __init__(self, schema, root, document_ast, operation_name, args, request_con
32
32
to execute, which we will pass throughout the other execution
33
33
methods."""
34
34
errors = []
35
- operations = {}
35
+ operation = None
36
36
fragments = {}
37
- for statement in document_ast .definitions :
38
- if isinstance (statement , ast .OperationDefinition ):
39
- name = ''
40
- if statement .name :
41
- name = statement .name .value
42
- operations [name ] = statement
43
- elif isinstance (statement , ast .FragmentDefinition ):
44
- fragments [statement .name .value ] = statement
45
- if not operation_name and len (operations ) != 1 :
46
- raise GraphQLError (
47
- 'Must provide operation name '
48
- 'if query contains multiple operations' )
49
- op_name = operation_name or next (iter (operations .keys ()))
50
- operation = operations .get (op_name )
37
+
38
+ for definition in document_ast .definitions :
39
+ if isinstance (definition , ast .OperationDefinition ):
40
+ if not operation_name and operation :
41
+ raise GraphQLError ('Must provide operation name if query contains multiple operations' )
42
+
43
+ if not operation_name or definition .name and definition .name .value == operation_name :
44
+ operation = definition
45
+
46
+ elif isinstance (definition , ast .FragmentDefinition ):
47
+ fragments [definition .name .value ] = definition
48
+
49
+ else :
50
+ raise GraphQLError (
51
+ u'GraphQL cannot execute a request containing a {}.' .format (definition .__class__ .__name__ ),
52
+ definition
53
+ )
54
+
51
55
if not operation :
52
- raise GraphQLError ('Unknown operation name: {}' .format (op_name ))
56
+ if operation_name :
57
+ raise GraphQLError (u'Unknown operation named "{}".' .format (operation_name ))
58
+
59
+ else :
60
+ raise GraphQLError ('Must provide an operation.' )
61
+
53
62
variables = get_variable_values (schema , operation .variable_definitions or [], args )
54
63
55
64
self .schema = schema
0 commit comments