-
Notifications
You must be signed in to change notification settings - Fork 5
/
graphql-subscription.html
318 lines (280 loc) · 8.33 KB
/
graphql-subscription.html
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<!--<link rel="import" href="graphql-client.html"/>-->
<link rel="import" href="../matryoshka-loader/matryoshka-loader-mixin.html">
<link rel="import" href="graphql-client.html">
<!--
## GraphQL subscription
An easy interface to create GraphQL subscription in your system.
_Note: to use `<graphql-subscription>` you should import `build/apollo-client-subscription.js`
instead of `build/apollo-client.js`:_
```html
<script src="bower_components/polymer-apollo-client/build/apollo-client-subscription.js"></script>
```
## Building a subscription
```html
<link rel="import" src="bower_components/polymer-apollo-client/graphql-subscription.html">
```
```html
<graphql-subscription variables='{"identifier": "home_hero"}' result="{{result}}">
subscription ($identifier: String!) {
block: Block(identifier: $identifier) {
id
title
content
}
}
</graphql-subscription>
```
When the subscription resolves the resulting data will be placed in the `result` property.
## Using the result:
```html
<h1>[[result.block.title]]</h1>
<div class="content">[[result.block.content]]</div>
```
Changing the variables or the query will automatically re-fetch all the information.
## Detecting loading states
The query element implements the `MatryoshkaLoaderMixin` and thus propagates the loading state of the query throughout the system.
@group Apollo Client
@polymer
@customElement
@demo demo/full-demo.html Full demo
-->
<script>
class GraphQLSubscription extends MatryoshkaLoaderMixin(Polymer.Element) {
static get is() {
return 'graphql-subscription';
}
static get properties() {
return {
/**
* Copy of the query provided in `this.innerText`, if you want to programmatically change the query use
* `this.innerText`.
* @private
*/
query: {
type: Object
},
/**
* JSON Object of the variables passed with the query.
*/
variables: {
type: Object,
value: {}
},
/**
* This allows you to defer the query until a later moment using `Polymer.Async.idlePeriod`. This solves an issue
* with rendering critical data first and deferring non-critical information to a later moment.
*/
defer: {
type: Boolean,
value: false
},
/**
* When this is set to `true` it will not execute the query when the properties `query`+`variables`+`defer` have a
* value. To run the query set `hold` to `false` or run `execute()`.
*
* You might want to set `hostLoading` to `false` when you do this.
*/
hold: {
type: Boolean,
value: false
},
/**
* It is used to halt the execution of the query when not all the variables are provided yet.
* @private
*/
requiredVariables: {
type: Object,
computed: '_computeRequiredVariables(query)'
},
/**
* Object of the resulting data of the subscription.
* [Apollo Client Docs](http://dev.apollodata.com/react/queries.html#default-result-props)
*/
result: {
type: Object,
notify: true
},
/**
* Sets the default value of `hostLoading` to `true`, this means this element will always propagate that it is
* loading.
* @todo, when `defer` is set, is this properly handled?
* @protected
*/
hostLoading: {
value: true
},
/**
* Connect to a different client.
*/
clientName: {
value: CLIENT_NAME_DEFAULT
},
/**
* Last error, if any.
*/
lastError: {
value: null,
readOnly: true,
notify: true
}
};
}
static get observers() {
return [
'_onRunQuery(defer, hold, query, variables)'
]
}
/**
* Since the query should never be shown, we always need to hide it.
*/
constructor() {
super();
this.style.display = 'none';
}
/**
*
*/
connectedCallback() {
super.connectedCallback();
var observer = new MutationObserver(this._updateQuery);
observer.observe(this, { subtree: true, characterData: true });
this._updateQuery();
}
/**
* Actual method to fetch all the data. This is called when one of the properties: `query`, `variables`, `defer` or `hold` is changed.
* @protected
*/
_onRunQuery(defer, hold) {
if (hold) {
return;
}
if (this.validate().error) {
return;
}
if (defer) {
Polymer.Async.idlePeriod.run(this.execute.bind(this));
} else {
this.execute();
}
}
/**
* Validate if all the required properties are properly filled in and return the error if there is something wrong.
* @return {Object}
*/
validate(params = {}) {
const variables = params.variables || this.variables;
if (this.query === undefined) {
return {
error: true,
msg: 'Query not yet defined',
};
}
if (variables == null) {
return {
error: true,
// eslint-disable-next-line max-len
msg: 'Variables are undefined should be an empty object if you don not want to send anything',
};
}
if (this.defer === undefined) {
return {
error: true,
// eslint-disable-next-line max-len
msg: 'Defer is undefined, accidentally set it to undefined should be true or false?',
};
}
if (this.requiredVariables.length &&
Object.keys(variables).length <= 0) {
let emptyVariables = this.requiredVariables.filter((variable) => {
return variables[variable] === undefined;
});
return {
error: true,
msg: 'Not all required variables are submitted',
variables: emptyVariables,
};
}
return {
error: false,
};
}
/**
* Execute the query/mutation directly (used in combination with `hold` or with `<graphql-mutation>`).
*
* @return {ObservableQuery}
*/
execute(params) {
let validationResult = this.validate(params);
if (validationResult.error) {
window.console.error(validationResult.msg, this);
return;
}
this.hostLoading = true;
const {
fetchPolicy,
fetchResults,
notifyOnNetworkStatusChange,
pollInterval,
query,
variables,
} = this;
const client = this._getClient();
if (!client) {
throw new Error(
'There is no GraphQL client available. ' +
'Initialize one on window.Apollo.client'
);
}
let observableQuery = client.subscribe({
fetchPolicy,
fetchResults,
notifyOnNetworkStatusChange,
pollInterval,
query: this.query,
variables: this.variables
}).subscribe({
next: (result) => {
if (result.data) {
this.hostLoading = result.loading;
this.result = result.data;
} else if (result.errors) {
console.error(result.errors.message);
this._handlerError(result.errors.message);
}
}
});
return observableQuery;
}
_computeRequiredVariables(query) {
var requiredVariables = [];
query.definitions.forEach(function (definition) {
if (definition.variableDefinitions) {
definition.variableDefinitions.forEach(function (variable) {
if (variable.type.kind === 'NonNullType') {
requiredVariables.push(variable.variable.name.value)
}
});
}
});
return requiredVariables;
}
_updateQuery() {
const query = this.textContent;
if (!query) return;
this.query = window.Apollo.gql([query]);
}
_getClient() {
return window.Apollo.namedClient[this.clientName]
}
/**
* Fired when an error is received.
*
* @event error
*/
_handleError(error) {
this.lastError = error;
this.dispatchEvent(new CustomEvent('error', { bubbles: true, composed: true, detail: error }))
}
}
customElements.define(GraphQLSubscription.is, GraphQLSubscription);
</script>