|
5 | 5 | GraphiQL,
|
6 | 6 | React,
|
7 | 7 | ReactDOM,
|
8 |
| - SubscriptionsTransportWs, |
| 8 | + graphqlWs, |
9 | 9 | fetch,
|
10 | 10 | history,
|
11 | 11 | location,
|
|
52 | 52 |
|
53 | 53 | var fetchURL = locationQuery(otherParams);
|
54 | 54 |
|
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) { |
57 | 73 | if (typeof opts === 'undefined') {
|
58 | 74 | opts = {};
|
59 | 75 | }
|
|
73 | 89 | headers['X-CSRFToken'] = csrftoken
|
74 | 90 | }
|
75 | 91 |
|
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 |
154 | 93 |
|
155 |
| - return match[1]; |
| 94 | + return fetcher(graphQLParams, opts) |
156 | 95 | }
|
157 | 96 |
|
158 | 97 | // When the query and variables string is edited, update the URL bar so
|
|
177 | 116 | onEditQuery: onEditQuery,
|
178 | 117 | onEditVariables: onEditVariables,
|
179 | 118 | onEditOperationName: onEditOperationName,
|
180 |
| - headerEditorEnabled: GRAPHENE_SETTINGS.graphiqlHeaderEditorEnabled, |
| 119 | + isHeadersEditorEnabled: GRAPHENE_SETTINGS.graphiqlHeaderEditorEnabled, |
181 | 120 | shouldPersistHeaders: GRAPHENE_SETTINGS.graphiqlShouldPersistHeaders,
|
182 | 121 | query: parameters.query,
|
183 | 122 | };
|
|
199 | 138 | window.GraphiQL,
|
200 | 139 | window.React,
|
201 | 140 | window.ReactDOM,
|
202 |
| - window.SubscriptionsTransportWs, |
| 141 | + window.graphqlWs, |
203 | 142 | window.fetch,
|
204 | 143 | window.history,
|
205 | 144 | window.location,
|
|
0 commit comments