@@ -32,24 +32,33 @@ def __init__(self, schema, root, document_ast, operation_name, args, request_con
3232 to execute, which we will pass throughout the other execution
3333 methods."""
3434 errors = []
35- operations = {}
35+ operation = None
3636 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+
5155 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+
5362 variables = get_variable_values (schema , operation .variable_definitions or [], args )
5463
5564 self .schema = schema
0 commit comments