Add headers support to GraphiQL

* add GRAPHIQL_HEADER_EDITOR_ENABLED setting with False default
* use GRAPHIQL_HEADER_EDITOR_ENABLED to set headerEditorEnabled GraphiQL option
* update graphQLFetcher and httpClient in static/graphiql.js to support custom query/mutation headers
This commit is contained in:
radoslaw.kowalski 2020-07-31 16:57:59 +02:00
parent b552dcac24
commit 49ffbc3716
2 changed files with 26 additions and 9 deletions

View File

@ -41,6 +41,10 @@ DEFAULTS = {
"DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME": None, "DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME": None,
# Use a separate path for handling subscriptions. # Use a separate path for handling subscriptions.
"SUBSCRIPTION_PATH": None, "SUBSCRIPTION_PATH": None,
# Set to True to enable GraphiQL headers editor tab
# This sets headerEditorEnabled GraphiQL option, for details go to
# https://github.com/graphql/graphiql/tree/main/packages/graphiql#options
"GRAPHIQL_HEADER_EDITOR_ENABLED": False,
} }
if settings.DEBUG: if settings.DEBUG:

View File

@ -61,17 +61,29 @@
var fetchURL = locationQuery(otherParams); var fetchURL = locationQuery(otherParams);
// Defines a GraphQL fetcher using the fetch API. // Defines a GraphQL fetcher using the fetch API.
function httpClient(graphQLParams) { function httpClient(graphQLParams, opts = { headers: {} }) {
var headers = { let headers = opts.headers;
Accept: "application/json", // Convert headers to an object.
"Content-Type": "application/json", if (typeof headers === 'string') {
}; headers = JSON.parse(opts.headers);
}
if (csrftoken) { if (csrftoken) {
headers["X-CSRFToken"] = csrftoken; Object.assign(
{
'X-CSRFToken': csrftoken
},
headers,
)
} }
return fetch(fetchURL, { return fetch(fetchURL, {
method: "post", method: "post",
headers: headers, headers: Object.assign(
{
'Accept': 'application/json',
'Content-Type': 'application/json'
},
headers,
),
body: JSON.stringify(graphQLParams), body: JSON.stringify(graphQLParams),
credentials: "include", credentials: "include",
}) })
@ -108,7 +120,7 @@
var activeSubscription = null; var activeSubscription = null;
// Define a GraphQL fetcher that can intelligently route queries based on the operation type. // Define a GraphQL fetcher that can intelligently route queries based on the operation type.
function graphQLFetcher(graphQLParams) { function graphQLFetcher(graphQLParams, opts = { headers: {} }) {
var operationType = getOperationType(graphQLParams); var operationType = getOperationType(graphQLParams);
// If we're about to execute a new operation, and we have an active subscription, // If we're about to execute a new operation, and we have an active subscription,
@ -126,7 +138,7 @@
}, },
}; };
} else { } else {
return httpClient(graphQLParams); return httpClient(graphQLParams, opts);
} }
} }
@ -173,6 +185,7 @@
onEditQuery: onEditQuery, onEditQuery: onEditQuery,
onEditVariables: onEditVariables, onEditVariables: onEditVariables,
onEditOperationName: onEditOperationName, onEditOperationName: onEditOperationName,
headerEditorEnabled: GRAPHENE_SETTINGS.graphiqlHeaderEditorEnabled || false,
query: parameters.query, query: parameters.query,
}; };
if (parameters.variables) { if (parameters.variables) {