1 | import * as i0 from '@angular/core';
|
2 | import { InjectionToken, Injectable, NgZone, Optional, Inject } from '@angular/core';
|
3 | import { ApolloClient } from '@apollo/client/core';
|
4 | export { gql, gql as graphql } from '@apollo/client/core';
|
5 | import { Observable, queueScheduler, observable, from } from 'rxjs';
|
6 | import { observeOn, startWith } from 'rxjs/operators';
|
7 |
|
8 | function fromPromise(promiseFn) {
|
9 | return new Observable((subscriber) => {
|
10 | promiseFn().then((result) => {
|
11 | if (!subscriber.closed) {
|
12 | subscriber.next(result);
|
13 | subscriber.complete();
|
14 | }
|
15 | }, (error) => {
|
16 | if (!subscriber.closed) {
|
17 | subscriber.error(error);
|
18 | }
|
19 | });
|
20 | return () => subscriber.unsubscribe();
|
21 | });
|
22 | }
|
23 | class ZoneScheduler {
|
24 | constructor(zone) {
|
25 | this.zone = zone;
|
26 | this.now = Date.now ? Date.now : () => +new Date();
|
27 | }
|
28 | schedule(work, delay = 0, state) {
|
29 | return this.zone.run(() => queueScheduler.schedule(work, delay, state));
|
30 | }
|
31 | }
|
32 | function fixObservable(obs) {
|
33 | obs[observable] = () => obs;
|
34 | return obs;
|
35 | }
|
36 | function wrapWithZone(obs, ngZone) {
|
37 | return obs.pipe(observeOn(new ZoneScheduler(ngZone)));
|
38 | }
|
39 | function pickFlag(flags, flag, defaultValue) {
|
40 | return flags && typeof flags[flag] !== 'undefined'
|
41 | ? flags[flag]
|
42 | : defaultValue;
|
43 | }
|
44 |
|
45 | class QueryRef {
|
46 | constructor(obsQuery, ngZone, options) {
|
47 | this.obsQuery = obsQuery;
|
48 | const wrapped = wrapWithZone(from(fixObservable(this.obsQuery)), ngZone);
|
49 | this.valueChanges = options.useInitialLoading
|
50 | ? wrapped.pipe(startWith(Object.assign(Object.assign({}, this.obsQuery.getCurrentResult(false)), { error: undefined, partial: undefined, stale: true })))
|
51 | : wrapped;
|
52 | this.queryId = this.obsQuery.queryId;
|
53 | }
|
54 |
|
55 | result() {
|
56 | return this.obsQuery.result();
|
57 | }
|
58 | getCurrentResult() {
|
59 | return this.obsQuery.getCurrentResult();
|
60 | }
|
61 | getLastResult() {
|
62 | return this.obsQuery.getLastResult();
|
63 | }
|
64 | getLastError() {
|
65 | return this.obsQuery.getLastError();
|
66 | }
|
67 | resetLastResults() {
|
68 | return this.obsQuery.resetLastResults();
|
69 | }
|
70 | refetch(variables) {
|
71 | return this.obsQuery.refetch(variables);
|
72 | }
|
73 | fetchMore(fetchMoreOptions) {
|
74 | return this.obsQuery.fetchMore(fetchMoreOptions);
|
75 | }
|
76 | subscribeToMore(options) {
|
77 |
|
78 |
|
79 | return this.obsQuery.subscribeToMore(options);
|
80 | }
|
81 | updateQuery(mapFn) {
|
82 | return this.obsQuery.updateQuery(mapFn);
|
83 | }
|
84 | stopPolling() {
|
85 | return this.obsQuery.stopPolling();
|
86 | }
|
87 | startPolling(pollInterval) {
|
88 | return this.obsQuery.startPolling(pollInterval);
|
89 | }
|
90 | setOptions(opts) {
|
91 | return this.obsQuery.setOptions(opts);
|
92 | }
|
93 | setVariables(variables) {
|
94 | return this.obsQuery.setVariables(variables);
|
95 | }
|
96 | }
|
97 |
|
98 | const APOLLO_FLAGS = new InjectionToken('APOLLO_FLAGS');
|
99 | const APOLLO_OPTIONS = new InjectionToken('APOLLO_OPTIONS');
|
100 | const APOLLO_NAMED_OPTIONS = new InjectionToken('APOLLO_NAMED_OPTIONS');
|
101 |
|
102 | class ApolloBase {
|
103 | constructor(ngZone, flags, _client) {
|
104 | this.ngZone = ngZone;
|
105 | this.flags = flags;
|
106 | this._client = _client;
|
107 | this.useInitialLoading = pickFlag(flags, 'useInitialLoading', false);
|
108 | }
|
109 | watchQuery(options) {
|
110 | return new QueryRef(this.ensureClient().watchQuery(Object.assign({}, options)), this.ngZone, Object.assign({ useInitialLoading: this.useInitialLoading }, options));
|
111 | }
|
112 | query(options) {
|
113 | return fromPromise(() => this.ensureClient().query(Object.assign({}, options)));
|
114 | }
|
115 | mutate(options) {
|
116 | return fromPromise(() => this.ensureClient().mutate(Object.assign({}, options)));
|
117 | }
|
118 | subscribe(options, extra) {
|
119 | const obs = from(fixObservable(this.ensureClient().subscribe(Object.assign({}, options))));
|
120 | return extra && extra.useZone !== true
|
121 | ? obs
|
122 | : wrapWithZone(obs, this.ngZone);
|
123 | }
|
124 | |
125 |
|
126 |
|
127 |
|
128 | getClient() {
|
129 | return this.client;
|
130 | }
|
131 | |
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | setClient(client) {
|
139 | this.client = client;
|
140 | }
|
141 | |
142 |
|
143 |
|
144 | get client() {
|
145 | return this._client;
|
146 | }
|
147 | |
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | set client(client) {
|
154 | if (this._client) {
|
155 | throw new Error('Client has been already defined');
|
156 | }
|
157 | this._client = client;
|
158 | }
|
159 | ensureClient() {
|
160 | this.checkInstance();
|
161 | return this._client;
|
162 | }
|
163 | checkInstance() {
|
164 | if (!this._client) {
|
165 | throw new Error('Client has not been defined yet');
|
166 | }
|
167 | }
|
168 | }
|
169 | class Apollo extends ApolloBase {
|
170 | constructor(_ngZone, apolloOptions, apolloNamedOptions, flags) {
|
171 | super(_ngZone, flags);
|
172 | this._ngZone = _ngZone;
|
173 | this.map = new Map();
|
174 | if (apolloOptions) {
|
175 | this.createDefault(apolloOptions);
|
176 | }
|
177 | if (apolloNamedOptions && typeof apolloNamedOptions === 'object') {
|
178 | for (let name in apolloNamedOptions) {
|
179 | if (apolloNamedOptions.hasOwnProperty(name)) {
|
180 | const options = apolloNamedOptions[name];
|
181 | this.createNamed(name, options);
|
182 | }
|
183 | }
|
184 | }
|
185 | }
|
186 | |
187 |
|
188 |
|
189 |
|
190 |
|
191 | create(options, name) {
|
192 | if (isDefault(name)) {
|
193 | this.createDefault(options);
|
194 | }
|
195 | else {
|
196 | this.createNamed(name, options);
|
197 | }
|
198 | }
|
199 | |
200 |
|
201 |
|
202 | default() {
|
203 | return this;
|
204 | }
|
205 | |
206 |
|
207 |
|
208 |
|
209 | use(name) {
|
210 | if (isDefault(name)) {
|
211 | return this.default();
|
212 | }
|
213 | return this.map.get(name);
|
214 | }
|
215 | |
216 |
|
217 |
|
218 |
|
219 | createDefault(options) {
|
220 | if (this.getClient()) {
|
221 | throw new Error('Apollo has been already created.');
|
222 | }
|
223 | return this.setClient(new ApolloClient(options));
|
224 | }
|
225 | |
226 |
|
227 |
|
228 |
|
229 |
|
230 | createNamed(name, options) {
|
231 | if (this.map.has(name)) {
|
232 | throw new Error(`Client ${name} has been already created`);
|
233 | }
|
234 | this.map.set(name, new ApolloBase(this._ngZone, this.flags, new ApolloClient(options)));
|
235 | }
|
236 | |
237 |
|
238 |
|
239 |
|
240 | removeClient(name) {
|
241 | if (isDefault(name)) {
|
242 | this._client = undefined;
|
243 | }
|
244 | else {
|
245 | this.map.delete(name);
|
246 | }
|
247 | }
|
248 | }
|
249 | Apollo.ɵprov = i0.ɵɵdefineInjectable({ factory: function Apollo_Factory() { return new Apollo(i0.ɵɵinject(i0.NgZone), i0.ɵɵinject(APOLLO_OPTIONS, 8), i0.ɵɵinject(APOLLO_NAMED_OPTIONS, 8), i0.ɵɵinject(APOLLO_FLAGS, 8)); }, token: Apollo, providedIn: "root" });
|
250 | Apollo.decorators = [
|
251 | { type: Injectable, args: [{
|
252 | providedIn: 'root',
|
253 | },] }
|
254 | ];
|
255 | Apollo.ctorParameters = () => [
|
256 | { type: NgZone },
|
257 | { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [APOLLO_OPTIONS,] }] },
|
258 | { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [APOLLO_NAMED_OPTIONS,] }] },
|
259 | { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [APOLLO_FLAGS,] }] }
|
260 | ];
|
261 | function isDefault(name) {
|
262 | return !name || name === 'default';
|
263 | }
|
264 |
|
265 | class Query {
|
266 | constructor(apollo) {
|
267 | this.apollo = apollo;
|
268 | this.client = 'default';
|
269 | }
|
270 | watch(variables, options) {
|
271 | return this.apollo.use(this.client).watchQuery(Object.assign(Object.assign({}, options), { variables, query: this.document }));
|
272 | }
|
273 | fetch(variables, options) {
|
274 | return this.apollo.use(this.client).query(Object.assign(Object.assign({}, options), { variables, query: this.document }));
|
275 | }
|
276 | }
|
277 | Query.decorators = [
|
278 | { type: Injectable }
|
279 | ];
|
280 | Query.ctorParameters = () => [
|
281 | { type: Apollo }
|
282 | ];
|
283 |
|
284 | class Mutation {
|
285 | constructor(apollo) {
|
286 | this.apollo = apollo;
|
287 | this.client = 'default';
|
288 | }
|
289 | mutate(variables, options) {
|
290 | return this.apollo.use(this.client).mutate(Object.assign(Object.assign({}, options), { variables, mutation: this.document }));
|
291 | }
|
292 | }
|
293 | Mutation.decorators = [
|
294 | { type: Injectable }
|
295 | ];
|
296 | Mutation.ctorParameters = () => [
|
297 | { type: Apollo }
|
298 | ];
|
299 |
|
300 | class Subscription {
|
301 | constructor(apollo) {
|
302 | this.apollo = apollo;
|
303 | this.client = 'default';
|
304 | }
|
305 | subscribe(variables, options, extra) {
|
306 | return this.apollo.use(this.client).subscribe(Object.assign(Object.assign({}, options), { variables, query: this.document }), extra);
|
307 | }
|
308 | }
|
309 | Subscription.decorators = [
|
310 | { type: Injectable }
|
311 | ];
|
312 | Subscription.ctorParameters = () => [
|
313 | { type: Apollo }
|
314 | ];
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 | export { APOLLO_FLAGS, APOLLO_NAMED_OPTIONS, APOLLO_OPTIONS, Apollo, ApolloBase, Mutation, Query, QueryRef, Subscription };
|
321 |
|