Skip to content

Commit 95a0642

Browse files
kiendangfiraskafri
authored andcommitted
fix: fix graphiql request failure
1 parent a8ceca7 commit 95a0642

File tree

1 file changed

+14
-27
lines changed

1 file changed

+14
-27
lines changed

graphene_django/static/graphene_django/graphiql.js

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -60,40 +60,27 @@
6060

6161
function trueLambda() { return true; };
6262

63-
var fetcher = GraphiQL.createFetcher({
63+
var headers = {};
64+
var cookies = ("; " + document.cookie).split("; csrftoken=");
65+
if (cookies.length == 2) {
66+
csrftoken = cookies.pop().split(";").shift();
67+
} else {
68+
csrftoken = document.querySelector("[name=csrfmiddlewaretoken]").value;
69+
}
70+
if (csrftoken) {
71+
headers['X-CSRFToken'] = csrftoken
72+
}
73+
74+
var graphQLFetcher = GraphiQL.createFetcher({
6475
url: fetchURL,
6576
wsClient: graphqlWs.createClient({
6677
url: subscribeURL,
6778
shouldRetry: trueLambda,
6879
lazy: true,
69-
})
80+
}),
81+
headers: headers
7082
})
7183

72-
function graphQLFetcher(graphQLParams, opts) {
73-
if (typeof opts === 'undefined') {
74-
opts = {};
75-
}
76-
var headers = opts.headers || {};
77-
headers['Accept'] = headers['Accept'] || 'application/json';
78-
headers['Content-Type'] = headers['Content-Type'] || 'application/json';
79-
80-
// Parse the cookie value for a CSRF token
81-
var csrftoken;
82-
var cookies = ("; " + document.cookie).split("; csrftoken=");
83-
if (cookies.length == 2) {
84-
csrftoken = cookies.pop().split(";").shift();
85-
} else {
86-
csrftoken = document.querySelector("[name=csrfmiddlewaretoken]").value;
87-
}
88-
if (csrftoken) {
89-
headers['X-CSRFToken'] = csrftoken
90-
}
91-
92-
opts.headers = headers
93-
94-
return fetcher(graphQLParams, opts)
95-
}
96-
9784
// When the query and variables string is edited, update the URL bar so
9885
// that it can be easily shared.
9986
function onEditQuery(newQuery) {

0 commit comments

Comments
 (0)