|
1 |
| -(function() { |
2 |
| - |
| 1 | +(function ( |
| 2 | + document, |
| 3 | + GRAPHENE_SETTINGS, |
| 4 | + GraphiQL, |
| 5 | + React, |
| 6 | + ReactDOM, |
| 7 | + SubscriptionsTransportWs, |
| 8 | + history, |
| 9 | + location, |
| 10 | +) { |
3 | 11 | // Parse the cookie value for a CSRF token
|
4 | 12 | var csrftoken;
|
5 | 13 | var cookies = ('; ' + document.cookie).split('; csrftoken=');
|
|
11 | 19 |
|
12 | 20 | // Collect the URL parameters
|
13 | 21 | var parameters = {};
|
14 |
| - window.location.hash.substr(1).split('&').forEach(function (entry) { |
| 22 | + location.hash.substr(1).split('&').forEach(function (entry) { |
15 | 23 | var eq = entry.indexOf('=');
|
16 | 24 | if (eq >= 0) {
|
17 | 25 | parameters[decodeURIComponent(entry.slice(0, eq))] =
|
|
41 | 49 | var fetchURL = locationQuery(otherParams);
|
42 | 50 |
|
43 | 51 | // Defines a GraphQL fetcher using the fetch API.
|
44 |
| - function graphQLFetcher(graphQLParams) { |
| 52 | + function httpClient(graphQLParams) { |
45 | 53 | var headers = {
|
46 | 54 | 'Accept': 'application/json',
|
47 | 55 | 'Content-Type': 'application/json'
|
|
64 | 72 | }
|
65 | 73 | });
|
66 | 74 | }
|
| 75 | + |
| 76 | + // Derive the subscription URL. If the SUBSCRIPTION_URL setting is specified, uses that value. Otherwise |
| 77 | + // assumes the current window location with an appropriate websocket protocol. |
| 78 | + var subscribeURL = |
| 79 | + location.origin.replace(/^http/, "ws") + |
| 80 | + (GRAPHENE_SETTINGS.subscriptionPath || location.pathname); |
| 81 | + |
| 82 | + // Create a subscription client. |
| 83 | + var subscriptionClient = new SubscriptionsTransportWs.SubscriptionClient( |
| 84 | + subscribeURL, |
| 85 | + { |
| 86 | + // Reconnect after any interruptions. |
| 87 | + reconnect: true, |
| 88 | + // Delay socket initialization until the first subscription is started. |
| 89 | + lazy: true, |
| 90 | + }, |
| 91 | + ); |
| 92 | + |
| 93 | + // Keep a reference to the currently-active subscription, if available. |
| 94 | + var activeSubscription = null; |
| 95 | + |
| 96 | + // Define a GraphQL fetcher that can intelligently route queries based on the operation type. |
| 97 | + function graphQLFetcher(graphQLParams) { |
| 98 | + var operationType = getOperationType(graphQLParams); |
| 99 | + |
| 100 | + // If we're about to execute a new operation, and we have an active subscription, |
| 101 | + // unsubscribe before continuing. |
| 102 | + if (activeSubscription) { |
| 103 | + activeSubscription.unsubscribe(); |
| 104 | + activeSubscription = null; |
| 105 | + } |
| 106 | + |
| 107 | + if (operationType === "subscription") { |
| 108 | + return { |
| 109 | + subscribe: function (observer) { |
| 110 | + subscriptionClient.request(graphQLParams).subscribe(observer); |
| 111 | + activeSubscription = subscriptionClient; |
| 112 | + }, |
| 113 | + }; |
| 114 | + } else { |
| 115 | + return httpClient(graphQLParams); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // Determine the type of operation being executed for a given set of GraphQL parameters. |
| 120 | + function getOperationType(graphQLParams) { |
| 121 | + // Run a regex against the query to determine the operation type (query, mutation, subscription). |
| 122 | + var operationRegex = new RegExp( |
| 123 | + // Look for lines that start with an operation keyword, ignoring whitespace. |
| 124 | + "^\\s*(query|mutation|subscription)\\s+" + |
| 125 | + // The operation keyword should be followed by the operationName in the GraphQL parameters. |
| 126 | + graphQLParams.operationName + |
| 127 | + // The line should eventually encounter an opening curly brace. |
| 128 | + "[^\\{]*\\{", |
| 129 | + // Enable multiline matching. |
| 130 | + "m", |
| 131 | + ); |
| 132 | + var match = operationRegex.exec(graphQLParams.query); |
| 133 | + |
| 134 | + return match[1]; |
| 135 | + } |
| 136 | + |
67 | 137 | // When the query and variables string is edited, update the URL bar so
|
68 | 138 | // that it can be easily shared.
|
69 | 139 | function onEditQuery(newQuery) {
|
|
83 | 153 | }
|
84 | 154 | var options = {
|
85 | 155 | fetcher: graphQLFetcher,
|
86 |
| - onEditQuery: onEditQuery, |
87 |
| - onEditVariables: onEditVariables, |
88 |
| - onEditOperationName: onEditOperationName, |
89 |
| - query: parameters.query, |
| 156 | + onEditQuery: onEditQuery, |
| 157 | + onEditVariables: onEditVariables, |
| 158 | + onEditOperationName: onEditOperationName, |
| 159 | + query: parameters.query, |
90 | 160 | }
|
91 | 161 | if (parameters.variables) {
|
92 | 162 | options.variables = parameters.variables;
|
|
99 | 169 | React.createElement(GraphiQL, options),
|
100 | 170 | document.getElementById("editor")
|
101 | 171 | );
|
102 |
| -})(); |
| 172 | +})( |
| 173 | + document, |
| 174 | + window.GRAPHENE_SETTINGS, |
| 175 | + window.GraphiQL, |
| 176 | + window.React, |
| 177 | + window.ReactDOM, |
| 178 | + window.SubscriptionsTransportWs, |
| 179 | + window.history, |
| 180 | + window.location, |
| 181 | +); |
0 commit comments