Skip to content
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

Add headers support to GraphiQL #1016

Merged
merged 7 commits into from
Aug 7, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,24 @@ Default: ``None``
GRAPHENE = {
'SUBSCRIPTION_PATH': "/ws/graphql"
}


``GRAPHIQL_HEADER_EDITOR_ENABLED``
---------------------

GraphiQL starting from version 1.0.0 allows setting custom headers in similar fashion to query variables.

Set to ``False`` if you want to disable GraphiQL headers editor tab for some reason.

This setting is passed to ``headerEditorEnabled`` GraphiQL options, for details refer to GraphiQLDocs_.

.. _GraphiQLDocs: https://github.com/graphql/graphiql/tree/main/packages/graphiql#options


Default: ``True``

.. code:: python

GRAPHENE = {
'GRAPHIQL_HEADER_EDITOR_ENABLED': True,
}
4 changes: 4 additions & 0 deletions graphene_django/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
"DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME": None,
# Use a separate path for handling subscriptions.
"SUBSCRIPTION_PATH": None,
# By default GraphiQL headers editor tab is enabled, set to False to hide it
# This sets headerEditorEnabled GraphiQL option, for details go to
# https://github.com/graphql/graphiql/tree/main/packages/graphiql#options
"GRAPHIQL_HEADER_EDITOR_ENABLED": True,
}

if settings.DEBUG:
Expand Down
19 changes: 11 additions & 8 deletions graphene_django/static/graphene_django/graphiql.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@
var fetchURL = locationQuery(otherParams);

// Defines a GraphQL fetcher using the fetch API.
function httpClient(graphQLParams) {
var headers = {
Accept: "application/json",
"Content-Type": "application/json",
};
function httpClient(graphQLParams, opts) {
if (typeof opts === 'undefined') {
opts = {};
}
radekwlsk marked this conversation as resolved.
Show resolved Hide resolved
var headers = opts.headers || {};
headers['Accept'] = headers['Accept'] || 'application/json';
headers['Content-Type'] = headers['Content-Type'] || 'application/json';
if (csrftoken) {
headers["X-CSRFToken"] = csrftoken;
headers['X-CSRFToken'] = csrftoken
}
return fetch(fetchURL, {
method: "post",
Expand Down Expand Up @@ -108,7 +110,7 @@
var activeSubscription = null;

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

// If we're about to execute a new operation, and we have an active subscription,
Expand All @@ -126,7 +128,7 @@
},
};
} else {
return httpClient(graphQLParams);
return httpClient(graphQLParams, opts);
}
}

Expand Down Expand Up @@ -173,6 +175,7 @@
onEditQuery: onEditQuery,
onEditVariables: onEditVariables,
onEditOperationName: onEditOperationName,
headerEditorEnabled: GRAPHENE_SETTINGS.graphiqlHeaderEditorEnabled,
query: parameters.query,
};
if (parameters.variables) {
Expand Down
1 change: 1 addition & 0 deletions graphene_django/templates/graphene/graphiql.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
{% if subscription_path %}
subscriptionPath: "{{subscription_path}}",
{% endif %}
graphiqlHeaderEditorEnabled: {{ graphiql_header_editor_enabled|yesno:"true,false" }},
};
</script>
<script src="{% static 'graphene_django/graphiql.js' %}"></script>
Expand Down
2 changes: 2 additions & 0 deletions graphene_django/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ def dispatch(self, request, *args, **kwargs):
subscriptions_transport_ws_sri=self.subscriptions_transport_ws_sri,
# The SUBSCRIPTION_PATH setting.
subscription_path=self.subscription_path,
# GraphiQL headers tab,
graphiql_header_editor_enabled=graphene_settings.GRAPHIQL_HEADER_EDITOR_ENABLED,
)

if self.batch:
Expand Down