-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathgraphiql.js
133 lines (125 loc) · 4.09 KB
/
graphiql.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Copyright (C) NIWA & British Crown (Met Office) & Contributors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Code related to GraphiQL
import { parse } from 'graphql'
import { createGraphQLUrls } from '@/graphql/index'
import { getCylcHeaders } from '@/utils/urls'
// TODO: https://github.com/apollographql/GraphiQL-Subscriptions-Fetcher/issues/16
// the functions hasSubscriptionOperation and graphQLFetcher are both from
// the graphiql-subscriptions-fetcher. Unfortunately that project is archived
// on GitHub, and is using the old API for subscription-transport-ws, which
// is a dependency of Cylc UI. As we cannot use an older version, instead we
// have the two functions here, patched as per issue to work with newer API.
/**
* Tell whether it is a query or subscription.
*
* @private
* @param {{
* query: string
* }}graphQlParams
* @returns {boolean} true if the params contain a subscription, false otherwise
*/
const hasSubscriptionOperation = function (graphQlParams) {
const queryDoc = parse(graphQlParams.query)
for (let _i = 0, _a = queryDoc.definitions; _i < _a.length; _i++) {
const definition = _a[_i]
if (definition.kind === 'OperationDefinition') {
const operation = definition.operation
if (operation === 'subscription') {
return true
}
}
}
return false
}
/**
* @typedef SubscribableComponent
* @property {?Object} subscription - GraphQL subscription
*/
/**
* The GraphQL fetcher function.
*
* @param {?Object} subscriptionsClient
* @param {function} fallbackFetcher
* @param {SubscribableComponent} component
* @returns {function}
*/
const graphQLFetcher = function (subscriptionsClient, fallbackFetcher, component) {
component.subscription = null
return function (graphQLParams) {
if (subscriptionsClient && component.subscription !== null) {
subscriptionsClient.unsubscribe(component.subscription)
}
if (subscriptionsClient && hasSubscriptionOperation(graphQLParams)) {
return {
subscribe: function (observer) {
observer.next('Your subscription data will appear here after server publication!')
const subscription = subscriptionsClient.request({
query: graphQLParams.query,
variables: graphQLParams.variables
}, function (error, result) {
if (error) {
observer.error(error)
} else {
observer.next(result)
}
})
component.subscription = subscription.subscribe((result, err) => {
if (err) {
observer.error(err)
} else {
observer.next(result)
}
})
return component.subscription
}
}
} else {
return fallbackFetcher(graphQLParams)
}
}
}
/**
* Fallback GraphQL fetcher.
*
* @param {*} graphQLParams
* @returns {Promise<any | string>}
*/
function fallbackGraphQLFetcher (graphQLParams) {
// re-using same method UI uses to create GraphQL URL's used by its client with createGraphQLUrls()
return fetch(
createGraphQLUrls().httpUrl,
{
method: 'post',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...getCylcHeaders()
},
body: JSON.stringify(graphQLParams),
credentials: 'include'
}
).then(function (response) {
return response.json().catch(function () {
return response.text()
})
})
}
export {
graphQLFetcher,
fallbackGraphQLFetcher
}