Skip to content

Adds variables arg to GraphQLTestCase.query #699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from Sep 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ Usage:
# Add some more asserts if you like
...

def test_query_with_variables(self):
response = self.query(
'''
query myModel($id: Int!){
myModel(id: $id) {
id
name
}
}
''',
op_name='myModel',
variables={'id': 1}
)

content = json.loads(response.content)

# This validates the status code and if you get errors
self.assertResponseNoErrors(response)

# Add some more asserts if you like
...

def test_some_mutation(self):
response = self.query(
'''
Expand Down
15 changes: 12 additions & 3 deletions graphene_django/utils/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,33 @@ def setUpClass(cls):

cls._client = Client()

def query(self, query, op_name=None, input_data=None):
def query(self, query, op_name=None, input_data=None, variables=None):
"""
Args:
query (string) - GraphQL query to run
op_name (string) - If the query is a mutation or named query, you must
supply the op_name. For annon queries ("{ ... }"),
should be None (default).
input_data (dict) - If provided, the $input variable in GraphQL will be set
to this value
to this value. If both ``input_data`` and ``variables``,
are provided, the ``input`` field in the ``variables``
dict will be overwritten with this value.
variables (dict) - If provided, the "variables" field in GraphQL will be
set to this value.

Returns:
Response object from client
"""
body = {"query": query}
if op_name:
body["operation_name"] = op_name
if variables:
body["variables"] = variables
if input_data:
body["variables"] = {"input": input_data}
if variables in body:
body["variables"]["input"] = input_data
else:
body["variables"] = {"input": input_data}

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