Skip to content

Commit 33c6a54

Browse files
committed
Merge branch 'master' into v3
2 parents da9f41c + bd553be commit 33c6a54

File tree

12 files changed

+97
-18
lines changed

12 files changed

+97
-18
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
strategy:
99
max-parallel: 4
1010
matrix:
11-
django: ["2.2", "3.0"]
11+
django: ["2.2", "3.0", "3.1"]
1212
python-version: ["3.6", "3.7", "3.8"]
1313

1414
steps:

docs/settings.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,24 @@ Default: ``None``
186186
GRAPHENE = {
187187
'SUBSCRIPTION_PATH': "/ws/graphql"
188188
}
189+
190+
191+
``GRAPHIQL_HEADER_EDITOR_ENABLED``
192+
---------------------
193+
194+
GraphiQL starting from version 1.0.0 allows setting custom headers in similar fashion to query variables.
195+
196+
Set to ``False`` if you want to disable GraphiQL headers editor tab for some reason.
197+
198+
This setting is passed to ``headerEditorEnabled`` GraphiQL options, for details refer to GraphiQLDocs_.
199+
200+
.. _GraphiQLDocs: https://github.com/graphql/graphiql/tree/main/packages/graphiql#options
201+
202+
203+
Default: ``True``
204+
205+
.. code:: python
206+
207+
GRAPHENE = {
208+
'GRAPHIQL_HEADER_EDITOR_ENABLED': True,
209+
}

graphene_django/compat.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ class MissingType(object):
88
from django.contrib.postgres.fields import (
99
ArrayField,
1010
HStoreField,
11-
JSONField,
11+
JSONField as PGJSONField,
1212
RangeField,
1313
)
1414
except ImportError:
15-
ArrayField, HStoreField, JSONField, RangeField = (MissingType,) * 4
15+
ArrayField, HStoreField, PGJSONField, RangeField = (MissingType,) * 4
16+
17+
try:
18+
# JSONField is only available from Django 3.1
19+
from django.db.models import JSONField
20+
except ImportError:
21+
JSONField = MissingType

graphene_django/converter.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
from graphql import GraphQLError, assert_valid_name
2727
from graphql.pyutils import register_description
2828

29-
from .compat import ArrayField, HStoreField, JSONField, RangeField
30-
from .fields import DjangoConnectionField, DjangoListField
29+
from .compat import ArrayField, HStoreField, JSONField, PGJSONField, RangeField
30+
from .fields import DjangoListField, DjangoConnectionField
3131
from .settings import graphene_settings
32+
from .utils import import_single_dispatch
3233
from .utils.str_converters import to_const
3334

3435

@@ -296,8 +297,9 @@ def convert_postgres_array_to_list(field, registry=None):
296297

297298

298299
@convert_django_field.register(HStoreField)
300+
@convert_django_field.register(PGJSONField)
299301
@convert_django_field.register(JSONField)
300-
def convert_postgres_field_to_string(field, registry=None):
302+
def convert_pg_and_json_field_to_string(field, registry=None):
301303
return JSONString(
302304
description=get_django_field_description(field), required=not field.null
303305
)

graphene_django/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
"DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME": None,
4141
# Use a separate path for handling subscriptions.
4242
"SUBSCRIPTION_PATH": None,
43+
# By default GraphiQL headers editor tab is enabled, set to False to hide it
44+
# This sets headerEditorEnabled GraphiQL option, for details go to
45+
# https://github.com/graphql/graphiql/tree/main/packages/graphiql#options
46+
"GRAPHIQL_HEADER_EDITOR_ENABLED": True,
4347
}
4448

4549
if settings.DEBUG:

graphene_django/static/graphene_django/graphiql.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@
6161
var fetchURL = locationQuery(otherParams);
6262

6363
// Defines a GraphQL fetcher using the fetch API.
64-
function httpClient(graphQLParams) {
65-
var headers = {
66-
Accept: "application/json",
67-
"Content-Type": "application/json",
68-
};
64+
function httpClient(graphQLParams, opts) {
65+
if (typeof opts === 'undefined') {
66+
opts = {};
67+
}
68+
var headers = opts.headers || {};
69+
headers['Accept'] = headers['Accept'] || 'application/json';
70+
headers['Content-Type'] = headers['Content-Type'] || 'application/json';
6971
if (csrftoken) {
70-
headers["X-CSRFToken"] = csrftoken;
72+
headers['X-CSRFToken'] = csrftoken
7173
}
7274
return fetch(fetchURL, {
7375
method: "post",
@@ -108,7 +110,7 @@
108110
var activeSubscription = null;
109111

110112
// Define a GraphQL fetcher that can intelligently route queries based on the operation type.
111-
function graphQLFetcher(graphQLParams) {
113+
function graphQLFetcher(graphQLParams, opts) {
112114
var operationType = getOperationType(graphQLParams);
113115

114116
// If we're about to execute a new operation, and we have an active subscription,
@@ -126,7 +128,7 @@
126128
},
127129
};
128130
} else {
129-
return httpClient(graphQLParams);
131+
return httpClient(graphQLParams, opts);
130132
}
131133
}
132134

@@ -173,6 +175,7 @@
173175
onEditQuery: onEditQuery,
174176
onEditVariables: onEditVariables,
175177
onEditOperationName: onEditOperationName,
178+
headerEditorEnabled: GRAPHENE_SETTINGS.graphiqlHeaderEditorEnabled,
176179
query: parameters.query,
177180
};
178181
if (parameters.variables) {

graphene_django/templates/graphene/graphiql.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
{% if subscription_path %}
4646
subscriptionPath: "{{subscription_path}}",
4747
{% endif %}
48+
graphiqlHeaderEditorEnabled: {{ graphiql_header_editor_enabled|yesno:"true,false" }},
4849
};
4950
</script>
5051
<script src="{% static 'graphene_django/graphiql.js' %}"></script>

graphene_django/tests/test_converter.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
from graphene.types.datetime import Date, DateTime, Time
1212
from graphene.types.json import JSONString
1313

14-
from ..compat import ArrayField, HStoreField, JSONField, MissingType, RangeField
14+
from ..compat import (
15+
ArrayField,
16+
HStoreField,
17+
JSONField,
18+
PGJSONField,
19+
MissingType,
20+
RangeField,
21+
)
1522
from ..converter import (
1623
convert_django_field,
1724
convert_django_field_with_choices,
@@ -352,8 +359,13 @@ def test_should_postgres_hstore_convert_string():
352359
assert_conversion(HStoreField, JSONString)
353360

354361

355-
@pytest.mark.skipif(JSONField is MissingType, reason="JSONField should exist")
362+
@pytest.mark.skipif(PGJSONField is MissingType, reason="PGJSONField should exist")
356363
def test_should_postgres_json_convert_string():
364+
assert_conversion(PGJSONField, JSONString)
365+
366+
367+
@pytest.mark.skipif(JSONField is MissingType, reason="JSONField should exist")
368+
def test_should_json_convert_string():
357369
assert_conversion(JSONField, JSONString)
358370

359371

graphene_django/tests/test_types.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from graphene.relay import Node
1010

1111
from .. import registry
12+
from ..filter import DjangoFilterConnectionField
1213
from ..types import DjangoObjectType, DjangoObjectTypeOptions
1314
from .models import Article as ArticleModel
1415
from .models import Reporter as ReporterModel
@@ -662,3 +663,28 @@ class Query(ObjectType):
662663
}
663664
"""
664665
)
666+
667+
668+
@with_local_registry
669+
def test_django_objecttype_name_connection_propagation():
670+
class Reporter(DjangoObjectType):
671+
class Meta:
672+
model = ReporterModel
673+
name = "CustomReporterName"
674+
filter_fields = ["email"]
675+
interfaces = (Node,)
676+
677+
class Query(ObjectType):
678+
reporter = Node.Field(Reporter)
679+
reporters = DjangoFilterConnectionField(Reporter)
680+
681+
assert Reporter._meta.name == "CustomReporterName"
682+
schema = str(Schema(query=Query))
683+
684+
assert "type CustomReporterName implements Node {" in schema
685+
assert "type CustomReporterNameConnection {" in schema
686+
assert "type CustomReporterNameEdge {" in schema
687+
688+
assert "type Reporter implements Node {" not in schema
689+
assert "type ReporterConnection {" not in schema
690+
assert "type ReporterEdge {" not in schema

graphene_django/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def __init_subclass_with_meta__(
246246
connection_class = Connection
247247

248248
connection = connection_class.create_type(
249-
"{}Connection".format(cls.__name__), node=cls
249+
"{}Connection".format(options.get("name") or cls.__name__), node=cls
250250
)
251251

252252
if connection is not None:

graphene_django/views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ def dispatch(self, request, *args, **kwargs):
154154
subscriptions_transport_ws_sri=self.subscriptions_transport_ws_sri,
155155
# The SUBSCRIPTION_PATH setting.
156156
subscription_path=self.subscription_path,
157+
# GraphiQL headers tab,
158+
graphiql_header_editor_enabled=graphene_settings.GRAPHIQL_HEADER_EDITOR_ENABLED,
157159
)
158160

159161
if self.batch:

tox.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tox]
22
envlist =
3-
py{36,37,38}-django{22,30,master},
3+
py{36,37,38}-django{22,30,31,master},
44
black,flake8
55

66
[gh-actions]
@@ -13,6 +13,7 @@ python =
1313
DJANGO =
1414
2.2: django22
1515
3.0: django30
16+
3.1: django31
1617
master: djangomaster
1718

1819
[testenv]
@@ -28,6 +29,7 @@ deps =
2829
django21: Django>=2.1,<2.2
2930
django22: Django>=2.2,<3.0
3031
django30: Django>=3.0a1,<3.1
32+
django31: Django>=3.1,<3.2
3133
djangomaster: https://github.com/django/django/archive/master.zip
3234
commands = {posargs:py.test --cov=graphene_django graphene_django examples}
3335

0 commit comments

Comments
 (0)