Skip to content

Commit 27e21e4

Browse files
committed
Added get_operation_type method to GraphQLDocument
1 parent 52de303 commit 27e21e4

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

graphql/backend/base.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
from ..pyutils.cached_property import cached_property
12
from ..language import ast
3+
24
from abc import ABCMeta, abstractmethod
35
import six
46

@@ -18,7 +20,12 @@ def __init__(self, schema, document_string, document_ast, execute):
1820
self.document_ast = document_ast
1921
self.execute = execute
2022

21-
def get_operations(self):
23+
@cached_property
24+
def operations_map(self):
25+
'''
26+
returns a Mapping of operation names and it's associated types.
27+
E.g. {'myQuery': 'query', 'myMutation': 'mutation'}
28+
'''
2229
document_ast = self.document_ast
2330
operations = {}
2431
for definition in document_ast.definitions:
@@ -29,3 +36,15 @@ def get_operations(self):
2936
operation_name = None
3037
operations[operation_name] = definition.operation
3138
return operations
39+
40+
def get_operation_type(self, operation_name):
41+
'''
42+
Returns the operation type ('query', 'mutation', 'subscription' or None)
43+
for the given operation_name.
44+
If no operation_name is provided (and only one operation exists) it will return the
45+
operation type for that operation
46+
'''
47+
operations_map = self.operations_map
48+
if not operation_name and len(operations_map) == 1:
49+
return next(iter(operations_map.values()))
50+
return operations_map.get(operation_name)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from ...language.base import parse
2+
from ..base import GraphQLDocument
3+
from .schema import schema
4+
5+
6+
def create_document(document_string):
7+
document_ast = parse(document_string)
8+
return GraphQLDocument(
9+
schema=schema,
10+
document_string=document_string,
11+
document_ast=document_ast,
12+
execute=lambda *_: None
13+
)
14+
15+
16+
def test_document_operations_map_unnamed_operation():
17+
document = create_document('{ hello }')
18+
assert document.operations_map == {
19+
None: 'query'
20+
}
21+
22+
23+
def test_document_operations_map_multiple_queries():
24+
document = create_document('''
25+
query MyQuery1 { hello }
26+
query MyQuery2 { hello }
27+
''')
28+
assert document.operations_map == {
29+
'MyQuery1': 'query',
30+
'MyQuery2': 'query'
31+
}
32+
33+
34+
def test_document_operations_map_multiple_queries():
35+
document = create_document('''
36+
query MyQuery { hello }
37+
mutation MyMutation { hello }
38+
subscription MySubscription { hello }
39+
''')
40+
assert document.operations_map == {
41+
'MyQuery': 'query',
42+
'MyMutation': 'mutation',
43+
'MySubscription': 'subscription'
44+
}
45+
46+
47+
def test_document_get_operation_type_unnamed_operation():
48+
document = create_document('''
49+
query { hello }
50+
''')
51+
assert document.get_operation_type(None) == 'query'
52+
assert document.get_operation_type('Unknown') is None
53+
54+
55+
def test_document_get_operation_type_multiple_operations():
56+
document = create_document('''
57+
query MyQuery { hello }
58+
mutation MyMutation {hello}
59+
''')
60+
assert document.get_operation_type(None) is None
61+
assert document.get_operation_type('MyQuery') == 'query'
62+
assert document.get_operation_type('MyMutation') == 'mutation'
63+
assert document.get_operation_type('Unexistent') is None
64+
65+
66+
def test_document_get_operation_type_multiple_operations_empty_operation_name():
67+
document = create_document('''
68+
query MyQuery { hello }
69+
mutation {hello}
70+
''')
71+
assert document.get_operation_type(None) is 'mutation'

0 commit comments

Comments
 (0)