Skip to content

Commit 55769e8

Browse files
radekwlskjkimbo
andauthored
Add headers support to GraphiQL (#1016)
Co-authored-by: Jonathan Kim <[email protected]>
1 parent 2308965 commit 55769e8

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed

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/settings.py

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

4650
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/views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ def dispatch(self, request, *args, **kwargs):
167167
subscriptions_transport_ws_sri=self.subscriptions_transport_ws_sri,
168168
# The SUBSCRIPTION_PATH setting.
169169
subscription_path=self.subscription_path,
170+
# GraphiQL headers tab,
171+
graphiql_header_editor_enabled=graphene_settings.GRAPHIQL_HEADER_EDITOR_ENABLED,
170172
)
171173

172174
if self.batch:

0 commit comments

Comments
 (0)