|
77 | 77 | integrity="sha384-ArTEHLNWIe9TuoDpFEtD/NeztNdWn3SdmWwMiAuZaSJeOaYypEGzeQoBxuPO+ORM"
|
78 | 78 | crossorigin="anonymous"
|
79 | 79 | ></script>
|
| 80 | + <script |
| 81 | + src= "https://unpkg.com/[email protected]/umd/graphql-ws.min.js" |
| 82 | + integrity="sha384-oEPbisbEBMo7iCrbQcKx244HXUjGnF1jyS8hkVZ3oCwnw9c9oLfY70c1RKeKj3+i" |
| 83 | + crossorigin="anonymous" |
| 84 | + ></script> |
80 | 85 |
|
81 | 86 | </head>
|
82 | 87 | <body>
|
|
188 | 193 | // if location is absolute (e.g. "/api") then prepend host only
|
189 | 194 | return (window.location.protocol === "http:" ? "ws://" : "wss://") + window.location.host + subscriptionsEndPoint;
|
190 | 195 | }
|
| 196 | + const subscriptionEndPoint = getSubscriptionsEndPoint(); |
191 | 197 |
|
192 | 198 | // Enable Subscriptions via WebSocket
|
193 |
| - var subscriptionsClient = new window.SubscriptionsTransportWs.SubscriptionClient(getSubscriptionsEndPoint(), { reconnect: true }); |
194 |
| - function subscriptionsFetcher(graphQLParams, fetcherOpts = { headers: {} }) { |
| 199 | + let subscriptionsClient = null; |
| 200 | + function subscriptionsTransportWsFetcher(graphQLParams, fetcherOpts = { headers: {} }) { |
| 201 | + if (!subscriptionsClient) |
| 202 | + subscriptionsClient = new window.SubscriptionsTransportWs.SubscriptionClient(subscriptionEndPoint, { reconnect: true }); |
195 | 203 | return window.GraphiQLSubscriptionsFetcher.graphQLFetcher(subscriptionsClient, function (_graphQLParams) {
|
196 | 204 | return graphQLFetcher(_graphQLParams, fetcherOpts);
|
197 | 205 | })(graphQLParams);
|
198 | 206 | }
|
| 207 | + |
| 208 | + function isSubscription(operationName, documentAST) { |
| 209 | + if (!documentAST.definitions || !documentAST.definitions.length || !documentAST.definitions.filter) return false; |
| 210 | + const definitions = documentAST.definitions.filter(function (def) { return def.kind === 'OperationDefinition'; }); |
| 211 | + if (operationName) definitions = definitions.filter(function (def) { return def.name && def.name.value === operationName; }); |
| 212 | + if (definitions.length === 0) return false; |
| 213 | + return definitions[0].operation === 'subscription'; |
| 214 | + } |
| 215 | +
|
| 216 | + let wsClient = null; |
| 217 | + function graphQLWsFetcher(payload, fetcherOpts) { |
| 218 | + console.log('graphQLWsFetcher', payload, fetcherOpts, !fetcherOpts || !fetcherOpts.documentAST || !isSubscription(payload.operationName, fetcherOpts.documentAST)); |
| 219 | + if (!fetcherOpts || !fetcherOpts.documentAST || !isSubscription(payload.operationName, fetcherOpts.documentAST)) |
| 220 | + return graphQLFetcher(payload, fetcherOpts); |
| 221 | + if (!wsClient) { |
| 222 | + wsClient = graphqlWs.createClient({ url: subscriptionEndPoint }); |
| 223 | + } |
| 224 | + let deferred = null; |
| 225 | + const pending = []; |
| 226 | + let throwMe = null, |
| 227 | + done = false; |
| 228 | + const dispose = wsClient.subscribe(payload, { |
| 229 | + next: (data) => { |
| 230 | + pending.push(data); |
| 231 | + if (deferred) deferred.resolve(false); |
| 232 | + }, |
| 233 | + error: (err) => { |
| 234 | + if (err instanceof Error) { |
| 235 | + throwMe = err; |
| 236 | + } else if (err instanceof CloseEvent) { |
| 237 | + throwMe = new Error( |
| 238 | + `Socket closed with event ${err.code} ${ |
| 239 | + err.reason || "" |
| 240 | + }`.trim() |
| 241 | + ); |
| 242 | + } else { |
| 243 | + // GraphQLError[] |
| 244 | + throwMe = new Error(err.map(({ message }) => message).join(", ")); |
| 245 | + } |
| 246 | + if (deferred) deferred.reject(throwMe); |
| 247 | + }, |
| 248 | + complete: () => { |
| 249 | + done = true; |
| 250 | + if (deferred) deferred.resolve(true); |
| 251 | + }, |
| 252 | + }); |
| 253 | +
|
| 254 | + return { |
| 255 | + [Symbol.asyncIterator]: function() { |
| 256 | + return this; |
| 257 | + }, |
| 258 | + next: function() { |
| 259 | + if (done) return Promise.resolve({ done: true, value: undefined }); |
| 260 | + if (throwMe) return Promise.reject(throwMe); |
| 261 | + if (pending.length) return Promise.resolve({ value: pending.shift() }); |
| 262 | + return new Promise(function(resolve, reject) { |
| 263 | + deferred = { resolve, reject }; |
| 264 | + }).then(function(result) { |
| 265 | + if (result) { |
| 266 | + return { done: true, value: undefined }; |
| 267 | + } else { |
| 268 | + return { value: pending.shift() }; |
| 269 | + } |
| 270 | + }); |
| 271 | + }, |
| 272 | + return: function() { |
| 273 | + dispose(); |
| 274 | + return Promise.resolve({ done: true, value: undefined }); |
| 275 | + } |
| 276 | + }; |
| 277 | + } |
| 278 | +
|
| 279 | + const subscriptionFetcher = (@Model.GraphQLWs) ? graphQLWsFetcher : subscriptionsTransportWsFetcher; |
199 | 280 |
|
200 | 281 | // Render <GraphiQL /> into the body.
|
201 | 282 | // See the README in the top level of this module to learn more about
|
202 | 283 | // how you can customize GraphiQL by providing different values or
|
203 | 284 | // additional child elements.
|
204 | 285 | ReactDOM.render(
|
205 | 286 | React.createElement(@Model.GraphiQLElement, {
|
206 |
| - fetcher: subscriptionsFetcher, |
| 287 | + fetcher: subscriptionFetcher, |
207 | 288 | query: parameters.query,
|
208 | 289 | variables: parameters.variables,
|
209 | 290 | operationName: parameters.operationName,
|
|
0 commit comments