Skip to content

Commit 3283d0b

Browse files
kiendangfiraskafri
authored andcommitted
Update GraphiQL to 2.4.1
1 parent 1d814c5 commit 3283d0b

File tree

3 files changed

+29
-90
lines changed

3 files changed

+29
-90
lines changed

graphene_django/static/graphene_django/graphiql.js

Lines changed: 23 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
GraphiQL,
66
React,
77
ReactDOM,
8-
SubscriptionsTransportWs,
8+
graphqlWs,
99
fetch,
1010
history,
1111
location,
@@ -52,8 +52,24 @@
5252

5353
var fetchURL = locationQuery(otherParams);
5454

55-
// Defines a GraphQL fetcher using the fetch API.
56-
function httpClient(graphQLParams, opts) {
55+
// Derive the subscription URL. If the SUBSCRIPTION_URL setting is specified, uses that value. Otherwise
56+
// assumes the current window location with an appropriate websocket protocol.
57+
var subscribeURL =
58+
location.origin.replace(/^http/, "ws") +
59+
(GRAPHENE_SETTINGS.subscriptionPath || location.pathname);
60+
61+
function trueLambda() { return true; };
62+
63+
var fetcher = GraphiQL.createFetcher({
64+
url: fetchURL,
65+
wsClient: graphqlWs.createClient({
66+
url: subscribeURL,
67+
shouldRetry: trueLambda,
68+
lazy: true,
69+
})
70+
})
71+
72+
function graphQLFetcher(graphQLParams, opts) {
5773
if (typeof opts === 'undefined') {
5874
opts = {};
5975
}
@@ -73,86 +89,9 @@
7389
headers['X-CSRFToken'] = csrftoken
7490
}
7591

76-
return fetch(fetchURL, {
77-
method: "post",
78-
headers: headers,
79-
body: JSON.stringify(graphQLParams),
80-
credentials: "include",
81-
})
82-
.then(function (response) {
83-
return response.text();
84-
})
85-
.then(function (responseBody) {
86-
try {
87-
return JSON.parse(responseBody);
88-
} catch (error) {
89-
return responseBody;
90-
}
91-
});
92-
}
93-
94-
// Derive the subscription URL. If the SUBSCRIPTION_URL setting is specified, uses that value. Otherwise
95-
// assumes the current window location with an appropriate websocket protocol.
96-
var subscribeURL =
97-
location.origin.replace(/^http/, "ws") +
98-
(GRAPHENE_SETTINGS.subscriptionPath || location.pathname);
99-
100-
// Create a subscription client.
101-
var subscriptionClient = new SubscriptionsTransportWs.SubscriptionClient(
102-
subscribeURL,
103-
{
104-
// Reconnect after any interruptions.
105-
reconnect: true,
106-
// Delay socket initialization until the first subscription is started.
107-
lazy: true,
108-
},
109-
);
110-
111-
// Keep a reference to the currently-active subscription, if available.
112-
var activeSubscription = null;
113-
114-
// Define a GraphQL fetcher that can intelligently route queries based on the operation type.
115-
function graphQLFetcher(graphQLParams, opts) {
116-
var operationType = getOperationType(graphQLParams);
117-
118-
// If we're about to execute a new operation, and we have an active subscription,
119-
// unsubscribe before continuing.
120-
if (activeSubscription) {
121-
activeSubscription.unsubscribe();
122-
activeSubscription = null;
123-
}
124-
125-
if (operationType === "subscription") {
126-
return {
127-
subscribe: function (observer) {
128-
activeSubscription = subscriptionClient;
129-
return subscriptionClient.request(graphQLParams, opts).subscribe(observer);
130-
},
131-
};
132-
} else {
133-
return httpClient(graphQLParams, opts);
134-
}
135-
}
136-
137-
// Determine the type of operation being executed for a given set of GraphQL parameters.
138-
function getOperationType(graphQLParams) {
139-
// Run a regex against the query to determine the operation type (query, mutation, subscription).
140-
var operationRegex = new RegExp(
141-
// Look for lines that start with an operation keyword, ignoring whitespace.
142-
"^\\s*(query|mutation|subscription)\\s*" +
143-
// The operation keyword should be followed by whitespace and the operationName in the GraphQL parameters (if available).
144-
(graphQLParams.operationName ? ("\\s+" + graphQLParams.operationName) : "") +
145-
// The line should eventually encounter an opening curly brace.
146-
"[^\\{]*\\{",
147-
// Enable multiline matching.
148-
"m",
149-
);
150-
var match = operationRegex.exec(graphQLParams.query);
151-
if (!match) {
152-
return "query";
153-
}
92+
opts.headers = headers
15493

155-
return match[1];
94+
return fetcher(graphQLParams, opts)
15695
}
15796

15897
// When the query and variables string is edited, update the URL bar so
@@ -177,7 +116,7 @@
177116
onEditQuery: onEditQuery,
178117
onEditVariables: onEditVariables,
179118
onEditOperationName: onEditOperationName,
180-
headerEditorEnabled: GRAPHENE_SETTINGS.graphiqlHeaderEditorEnabled,
119+
isHeadersEditorEnabled: GRAPHENE_SETTINGS.graphiqlHeaderEditorEnabled,
181120
shouldPersistHeaders: GRAPHENE_SETTINGS.graphiqlShouldPersistHeaders,
182121
query: parameters.query,
183122
};
@@ -199,7 +138,7 @@
199138
window.GraphiQL,
200139
window.React,
201140
window.ReactDOM,
202-
window.SubscriptionsTransportWs,
141+
window.graphqlWs,
203142
window.fetch,
204143
window.history,
205144
window.location,

graphene_django/templates/graphene/graphiql.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<script src="https://cdn.jsdelivr.net/npm/graphiql@{{graphiql_version}}/graphiql.min.js"
3434
integrity="{{graphiql_sri}}"
3535
crossorigin="anonymous"></script>
36-
<script src="https://cdn.jsdelivr.net/npm/subscriptions-transport-ws@{{subscriptions_transport_ws_version}}/browser/client.js"
36+
<script src="https://cdn.jsdelivr.net/npm/graphql-ws@{{subscriptions_transport_ws_version}}/umd/graphql-ws.min.js"
3737
integrity="{{subscriptions_transport_ws_sri}}"
3838
crossorigin="anonymous"></script>
3939
</head>

graphene_django/views.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ class GraphQLView(View):
6666
react_dom_sri = "sha256-nbMykgB6tsOFJ7OdVmPpdqMFVk4ZsqWocT6issAPUF0="
6767

6868
# The GraphiQL React app.
69-
graphiql_version = "1.4.7" # "1.0.3"
70-
graphiql_sri = "sha256-cpZ8w9D/i6XdEbY/Eu7yAXeYzReVw0mxYd7OU3gUcsc=" # "sha256-VR4buIDY9ZXSyCNFHFNik6uSe0MhigCzgN4u7moCOTk="
71-
graphiql_css_sri = "sha256-HADQowUuFum02+Ckkv5Yu5ygRoLllHZqg0TFZXY7NHI=" # "sha256-LwqxjyZgqXDYbpxQJ5zLQeNcf7WVNSJ+r8yp2rnWE/E="
69+
graphiql_version = "2.4.1" # "1.0.3"
70+
graphiql_sri = "sha256-s+f7CFAPSUIygFnRC2nfoiEKd3liCUy+snSdYFAoLUc=" # "sha256-VR4buIDY9ZXSyCNFHFNik6uSe0MhigCzgN4u7moCOTk="
71+
graphiql_css_sri = "sha256-88yn8FJMyGboGs4Bj+Pbb3kWOWXo7jmb+XCRHE+282k=" # "sha256-LwqxjyZgqXDYbpxQJ5zLQeNcf7WVNSJ+r8yp2rnWE/E="
7272

7373
# The websocket transport library for subscriptions.
74-
subscriptions_transport_ws_version = "0.9.18"
74+
subscriptions_transport_ws_version = "5.12.1"
7575
subscriptions_transport_ws_sri = (
76-
"sha256-i0hAXd4PdJ/cHX3/8tIy/Q/qKiWr5WSTxMFuL9tACkw="
76+
"sha256-EZhvg6ANJrBsgLvLAa0uuHNLepLJVCFYS+xlb5U/bqw="
7777
)
7878

7979
schema = None

0 commit comments

Comments
 (0)