Skip to content

Commit 254e59c

Browse files
tlambert03mvanlonden
authored andcommitted
Adds variables arg to GraphQLTestCase.query (#699)
* add variables arg in GraphQLTestCase.query * update GraphQLTestCase.query docstring and remove type check
1 parent ac79b38 commit 254e59c

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

docs/testing.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ Usage:
3737
# Add some more asserts if you like
3838
...
3939
40+
def test_query_with_variables(self):
41+
response = self.query(
42+
'''
43+
query myModel($id: Int!){
44+
myModel(id: $id) {
45+
id
46+
name
47+
}
48+
}
49+
''',
50+
op_name='myModel',
51+
variables={'id': 1}
52+
)
53+
54+
content = json.loads(response.content)
55+
56+
# This validates the status code and if you get errors
57+
self.assertResponseNoErrors(response)
58+
59+
# Add some more asserts if you like
60+
...
61+
4062
def test_some_mutation(self):
4163
response = self.query(
4264
'''

graphene_django/utils/testing.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,33 @@ def setUpClass(cls):
2424

2525
cls._client = Client()
2626

27-
def query(self, query, op_name=None, input_data=None):
27+
def query(self, query, op_name=None, input_data=None, variables=None):
2828
"""
2929
Args:
3030
query (string) - GraphQL query to run
3131
op_name (string) - If the query is a mutation or named query, you must
3232
supply the op_name. For annon queries ("{ ... }"),
3333
should be None (default).
3434
input_data (dict) - If provided, the $input variable in GraphQL will be set
35-
to this value
35+
to this value. If both ``input_data`` and ``variables``,
36+
are provided, the ``input`` field in the ``variables``
37+
dict will be overwritten with this value.
38+
variables (dict) - If provided, the "variables" field in GraphQL will be
39+
set to this value.
3640
3741
Returns:
3842
Response object from client
3943
"""
4044
body = {"query": query}
4145
if op_name:
4246
body["operation_name"] = op_name
47+
if variables:
48+
body["variables"] = variables
4349
if input_data:
44-
body["variables"] = {"input": input_data}
50+
if variables in body:
51+
body["variables"]["input"] = input_data
52+
else:
53+
body["variables"] = {"input": input_data}
4554

4655
resp = self._client.post(
4756
self.GRAPHQL_URL, json.dumps(body), content_type="application/json"

0 commit comments

Comments
 (0)