diff --git a/docs/testing.rst b/docs/testing.rst index b111642..031cf6b 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -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( ''' diff --git a/graphene_django/utils/testing.py b/graphene_django/utils/testing.py index db3e9f4..bf70ae3 100644 --- a/graphene_django/utils/testing.py +++ b/graphene_django/utils/testing.py @@ -24,7 +24,7 @@ class GraphQLTestCase(TestCase): 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 @@ -40,8 +40,13 @@ class GraphQLTestCase(TestCase): 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 and isinstance(body, dict): + 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"