Skip to content

Commit

Permalink
fix: fix graphiql request failure
Browse files Browse the repository at this point in the history
  • Loading branch information
kiendang authored and firaskafri committed May 3, 2023
1 parent a8ceca7 commit 95a0642
Showing 1 changed file with 14 additions and 27 deletions.
41 changes: 14 additions & 27 deletions graphene_django/static/graphene_django/graphiql.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,40 +60,27 @@

function trueLambda() { return true; };

var fetcher = GraphiQL.createFetcher({
var headers = {};
var cookies = ("; " + document.cookie).split("; csrftoken=");
if (cookies.length == 2) {
csrftoken = cookies.pop().split(";").shift();
} else {
csrftoken = document.querySelector("[name=csrfmiddlewaretoken]").value;
}
if (csrftoken) {
headers['X-CSRFToken'] = csrftoken
}

var graphQLFetcher = GraphiQL.createFetcher({
url: fetchURL,
wsClient: graphqlWs.createClient({
url: subscribeURL,
shouldRetry: trueLambda,
lazy: true,
})
}),
headers: headers
})

function graphQLFetcher(graphQLParams, opts) {
if (typeof opts === 'undefined') {
opts = {};
}
var headers = opts.headers || {};
headers['Accept'] = headers['Accept'] || 'application/json';
headers['Content-Type'] = headers['Content-Type'] || 'application/json';

// Parse the cookie value for a CSRF token
var csrftoken;
var cookies = ("; " + document.cookie).split("; csrftoken=");
if (cookies.length == 2) {
csrftoken = cookies.pop().split(";").shift();
} else {
csrftoken = document.querySelector("[name=csrfmiddlewaretoken]").value;
}
if (csrftoken) {
headers['X-CSRFToken'] = csrftoken
}

opts.headers = headers

return fetcher(graphQLParams, opts)
}

// When the query and variables string is edited, update the URL bar so
// that it can be easily shared.
function onEditQuery(newQuery) {
Expand Down

0 comments on commit 95a0642

Please sign in to comment.