Skip to content

Switch operation_name to operationName in GraphQLTestCase #936

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 4 commits into from
Apr 19, 2020
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
30 changes: 29 additions & 1 deletion graphene_django/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import json

import pytest
from django.utils.translation import gettext_lazy
from mock import patch

from ..utils import camelize, get_model_fields
from ..utils import camelize, get_model_fields, GraphQLTestCase
from .models import Film, Reporter


Expand Down Expand Up @@ -30,3 +34,27 @@ def test_camelize():
"valueA": "value_b"
}
assert camelize({0: {"field_a": ["errors"]}}) == {0: {"fieldA": ["errors"]}}


@pytest.mark.django_db
@patch("graphene_django.utils.testing.Client.post")
def test_graphql_test_case_op_name(post_mock):
"""
Test that `GraphQLTestCase.query()`'s `op_name` argument produces an `operationName` field.
"""

class TestClass(GraphQLTestCase):
GRAPHQL_SCHEMA = True

def runTest(self):
pass

tc = TestClass()
tc.setUpClass()
tc.query("query { }", op_name="QueryName")
body = json.loads(post_mock.call_args.args[1])
# `operationName` field from https://graphql.org/learn/serving-over-http/#post-request
assert (
"operationName",
"QueryName",
) in body.items(), "Field 'operationName' is not present in the final request."
6 changes: 3 additions & 3 deletions graphene_django/utils/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ def query(self, query, op_name=None, input_data=None, variables=None, headers=No
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. If both ``input_data`` and ``variables``,
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.
headers (dict) - If provided, the headers in POST request to GRAPHQL_URL
will be set to this value.
will be set to this value.

Returns:
Response object from client
"""
body = {"query": query}
if op_name:
body["operation_name"] = op_name
body["operationName"] = op_name
if variables:
body["variables"] = variables
if input_data:
Expand Down