Skip to content

Commit dc5c971

Browse files
authored
Switch operation_name to operationName in GraphQLTestCase (#936)
* Add op_name test * Replace "operation_name" with "operationName" * Improve test comments * Add method for Python 2.7
1 parent 23b6419 commit dc5c971

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

graphene_django/tests/test_utils.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import json
2+
3+
import pytest
14
from django.utils.translation import gettext_lazy
5+
from mock import patch
26

3-
from ..utils import camelize, get_model_fields
7+
from ..utils import camelize, get_model_fields, GraphQLTestCase
48
from .models import Film, Reporter
59

610

@@ -30,3 +34,27 @@ def test_camelize():
3034
"valueA": "value_b"
3135
}
3236
assert camelize({0: {"field_a": ["errors"]}}) == {0: {"fieldA": ["errors"]}}
37+
38+
39+
@pytest.mark.django_db
40+
@patch("graphene_django.utils.testing.Client.post")
41+
def test_graphql_test_case_op_name(post_mock):
42+
"""
43+
Test that `GraphQLTestCase.query()`'s `op_name` argument produces an `operationName` field.
44+
"""
45+
46+
class TestClass(GraphQLTestCase):
47+
GRAPHQL_SCHEMA = True
48+
49+
def runTest(self):
50+
pass
51+
52+
tc = TestClass()
53+
tc.setUpClass()
54+
tc.query("query { }", op_name="QueryName")
55+
body = json.loads(post_mock.call_args.args[1])
56+
# `operationName` field from https://graphql.org/learn/serving-over-http/#post-request
57+
assert (
58+
"operationName",
59+
"QueryName",
60+
) in body.items(), "Field 'operationName' is not present in the final request."

graphene_django/utils/testing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ def query(self, query, op_name=None, input_data=None, variables=None, headers=No
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. If both ``input_data`` and ``variables``,
35+
to this value. If both ``input_data`` and ``variables``,
3636
are provided, the ``input`` field in the ``variables``
3737
dict will be overwritten with this value.
3838
variables (dict) - If provided, the "variables" field in GraphQL will be
3939
set to this value.
4040
headers (dict) - If provided, the headers in POST request to GRAPHQL_URL
41-
will be set to this value.
41+
will be set to this value.
4242
4343
Returns:
4444
Response object from client
4545
"""
4646
body = {"query": query}
4747
if op_name:
48-
body["operation_name"] = op_name
48+
body["operationName"] = op_name
4949
if variables:
5050
body["variables"] = variables
5151
if input_data:

0 commit comments

Comments
 (0)