UNPKG

14.2 kBJavaScriptView Raw
1import * as i0 from '@angular/core';
2import { InjectionToken, Injectable, Optional, Inject, NgModule } from '@angular/core';
3import { NetworkStatus, ApolloClient, gql as gql$1 } from '@apollo/client/core';
4import { Observable, queueScheduler, observable, from } from 'rxjs';
5import { map, startWith, observeOn } from 'rxjs/operators';
6
7function fromPromise(promiseFn) {
8 return new Observable((subscriber) => {
9 promiseFn().then((result) => {
10 if (!subscriber.closed) {
11 subscriber.next(result);
12 subscriber.complete();
13 }
14 }, (error) => {
15 if (!subscriber.closed) {
16 subscriber.error(error);
17 }
18 });
19 return () => subscriber.unsubscribe();
20 });
21}
22function useMutationLoading(source, enabled) {
23 if (!enabled) {
24 return source.pipe(map((result) => ({
25 ...result,
26 loading: false,
27 })));
28 }
29 return source.pipe(startWith({
30 loading: true,
31 }), map((result) => ({
32 ...result,
33 loading: !!result.loading,
34 })));
35}
36class ZoneScheduler {
37 constructor(zone) {
38 this.zone = zone;
39 this.now = Date.now ? Date.now : () => +new Date();
40 }
41 schedule(work, delay = 0, state) {
42 return this.zone.run(() => queueScheduler.schedule(work, delay, state));
43 }
44}
45function fixObservable(obs) {
46 obs[observable] = () => obs;
47 return obs;
48}
49function wrapWithZone(obs, ngZone) {
50 return obs.pipe(observeOn(new ZoneScheduler(ngZone)));
51}
52function pickFlag(flags, flag, defaultValue) {
53 return flags && typeof flags[flag] !== 'undefined'
54 ? flags[flag]
55 : defaultValue;
56}
57
58function useInitialLoading(obsQuery) {
59 return function useInitialLoadingOperator(source) {
60 return new Observable(function useInitialLoadingSubscription(subscriber) {
61 const currentResult = obsQuery.getCurrentResult();
62 const { loading, errors, error, partial, data } = currentResult;
63 const { partialRefetch, fetchPolicy } = obsQuery.options;
64 const hasError = errors || error;
65 if (partialRefetch &&
66 partial &&
67 (!data || Object.keys(data).length === 0) &&
68 fetchPolicy !== 'cache-only' &&
69 !loading &&
70 !hasError) {
71 subscriber.next({
72 ...currentResult,
73 loading: true,
74 networkStatus: NetworkStatus.loading,
75 });
76 }
77 return source.subscribe(subscriber);
78 });
79 };
80}
81class QueryRef {
82 constructor(obsQuery, ngZone, options) {
83 this.obsQuery = obsQuery;
84 const wrapped = wrapWithZone(from(fixObservable(this.obsQuery)), ngZone);
85 this.valueChanges = options.useInitialLoading
86 ? wrapped.pipe(useInitialLoading(this.obsQuery))
87 : wrapped;
88 this.queryId = this.obsQuery.queryId;
89 }
90 // ObservableQuery's methods
91 get options() {
92 return this.obsQuery.options;
93 }
94 get variables() {
95 return this.obsQuery.variables;
96 }
97 result() {
98 return this.obsQuery.result();
99 }
100 getCurrentResult() {
101 return this.obsQuery.getCurrentResult();
102 }
103 getLastResult() {
104 return this.obsQuery.getLastResult();
105 }
106 getLastError() {
107 return this.obsQuery.getLastError();
108 }
109 resetLastResults() {
110 return this.obsQuery.resetLastResults();
111 }
112 refetch(variables) {
113 return this.obsQuery.refetch(variables);
114 }
115 fetchMore(fetchMoreOptions) {
116 return this.obsQuery.fetchMore(fetchMoreOptions);
117 }
118 subscribeToMore(options) {
119 // XXX: there's a bug in apollo-client typings
120 // it should not inherit types from ObservableQuery
121 return this.obsQuery.subscribeToMore(options);
122 }
123 updateQuery(mapFn) {
124 return this.obsQuery.updateQuery(mapFn);
125 }
126 stopPolling() {
127 return this.obsQuery.stopPolling();
128 }
129 startPolling(pollInterval) {
130 return this.obsQuery.startPolling(pollInterval);
131 }
132 setOptions(opts) {
133 return this.obsQuery.setOptions(opts);
134 }
135 setVariables(variables) {
136 return this.obsQuery.setVariables(variables);
137 }
138}
139
140const APOLLO_FLAGS = new InjectionToken('APOLLO_FLAGS');
141const APOLLO_OPTIONS = new InjectionToken('APOLLO_OPTIONS');
142const APOLLO_NAMED_OPTIONS = new InjectionToken('APOLLO_NAMED_OPTIONS');
143
144class ApolloBase {
145 constructor(ngZone, flags, _client) {
146 this.ngZone = ngZone;
147 this.flags = flags;
148 this._client = _client;
149 this.useInitialLoading = pickFlag(flags, 'useInitialLoading', false);
150 this.useMutationLoading = pickFlag(flags, 'useMutationLoading', false);
151 }
152 watchQuery(options) {
153 return new QueryRef(this.ensureClient().watchQuery({
154 ...options,
155 }), this.ngZone, {
156 useInitialLoading: this.useInitialLoading,
157 ...options,
158 });
159 }
160 query(options) {
161 return fromPromise(() => this.ensureClient().query({ ...options }));
162 }
163 mutate(options) {
164 return useMutationLoading(fromPromise(() => this.ensureClient().mutate({ ...options })), options.useMutationLoading ?? this.useMutationLoading);
165 }
166 subscribe(options, extra) {
167 const obs = from(fixObservable(this.ensureClient().subscribe({ ...options })));
168 return extra && extra.useZone !== true
169 ? obs
170 : wrapWithZone(obs, this.ngZone);
171 }
172 /**
173 * Get an access to an instance of ApolloClient
174 * @deprecated use `apollo.client` instead
175 */
176 getClient() {
177 return this.client;
178 }
179 /**
180 * Set a new instance of ApolloClient
181 * Remember to clean up the store before setting a new client.
182 * @deprecated use `apollo.client = client` instead
183 *
184 * @param client ApolloClient instance
185 */
186 setClient(client) {
187 this.client = client;
188 }
189 /**
190 * Get an access to an instance of ApolloClient
191 */
192 get client() {
193 return this._client;
194 }
195 /**
196 * Set a new instance of ApolloClient
197 * Remember to clean up the store before setting a new client.
198 *
199 * @param client ApolloClient instance
200 */
201 set client(client) {
202 if (this._client) {
203 throw new Error('Client has been already defined');
204 }
205 this._client = client;
206 }
207 ensureClient() {
208 this.checkInstance();
209 return this._client;
210 }
211 checkInstance() {
212 if (!this._client) {
213 throw new Error('Client has not been defined yet');
214 }
215 }
216}
217class Apollo extends ApolloBase {
218 constructor(_ngZone, apolloOptions, apolloNamedOptions, flags) {
219 super(_ngZone, flags);
220 this._ngZone = _ngZone;
221 this.map = new Map();
222 if (apolloOptions) {
223 this.createDefault(apolloOptions);
224 }
225 if (apolloNamedOptions && typeof apolloNamedOptions === 'object') {
226 for (let name in apolloNamedOptions) {
227 if (apolloNamedOptions.hasOwnProperty(name)) {
228 const options = apolloNamedOptions[name];
229 this.createNamed(name, options);
230 }
231 }
232 }
233 }
234 /**
235 * Create an instance of ApolloClient
236 * @param options Options required to create ApolloClient
237 * @param name client's name
238 */
239 create(options, name) {
240 if (isDefault(name)) {
241 this.createDefault(options);
242 }
243 else {
244 this.createNamed(name, options);
245 }
246 }
247 /**
248 * Use a default ApolloClient
249 */
250 default() {
251 return this;
252 }
253 /**
254 * Use a named ApolloClient
255 * @param name client's name
256 */
257 use(name) {
258 if (isDefault(name)) {
259 return this.default();
260 }
261 return this.map.get(name);
262 }
263 /**
264 * Create a default ApolloClient, same as `apollo.create(options)`
265 * @param options ApolloClient's options
266 */
267 createDefault(options) {
268 if (this.getClient()) {
269 throw new Error('Apollo has been already created.');
270 }
271 return this.setClient(new ApolloClient(options));
272 }
273 /**
274 * Create a named ApolloClient, same as `apollo.create(options, name)`
275 * @param name client's name
276 * @param options ApolloClient's options
277 */
278 createNamed(name, options) {
279 if (this.map.has(name)) {
280 throw new Error(`Client ${name} has been already created`);
281 }
282 this.map.set(name, new ApolloBase(this._ngZone, this.flags, new ApolloClient(options)));
283 }
284 /**
285 * Remember to clean up the store before removing a client
286 * @param name client's name
287 */
288 removeClient(name) {
289 if (isDefault(name)) {
290 this._client = undefined;
291 }
292 else {
293 this.map.delete(name);
294 }
295 }
296}
297Apollo.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.6", ngImport: i0, type: Apollo, deps: [{ token: i0.NgZone }, { token: APOLLO_OPTIONS, optional: true }, { token: APOLLO_NAMED_OPTIONS, optional: true }, { token: APOLLO_FLAGS, optional: true }], target: i0.ɵɵFactoryTarget.Injectable });
298Apollo.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.0.6", ngImport: i0, type: Apollo });
299i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.6", ngImport: i0, type: Apollo, decorators: [{
300 type: Injectable
301 }], ctorParameters: function () { return [{ type: i0.NgZone }, { type: undefined, decorators: [{
302 type: Optional
303 }, {
304 type: Inject,
305 args: [APOLLO_OPTIONS]
306 }] }, { type: undefined, decorators: [{
307 type: Optional
308 }, {
309 type: Inject,
310 args: [APOLLO_NAMED_OPTIONS]
311 }] }, { type: undefined, decorators: [{
312 type: Optional
313 }, {
314 type: Inject,
315 args: [APOLLO_FLAGS]
316 }] }]; } });
317function isDefault(name) {
318 return !name || name === 'default';
319}
320
321const PROVIDERS = [Apollo];
322class ApolloModule {
323}
324ApolloModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.6", ngImport: i0, type: ApolloModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
325ApolloModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.0.6", ngImport: i0, type: ApolloModule });
326ApolloModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.0.6", ngImport: i0, type: ApolloModule, providers: PROVIDERS });
327i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.6", ngImport: i0, type: ApolloModule, decorators: [{
328 type: NgModule,
329 args: [{
330 providers: PROVIDERS,
331 }]
332 }] });
333
334class Query {
335 constructor(apollo) {
336 this.apollo = apollo;
337 this.client = 'default';
338 }
339 watch(variables, options) {
340 return this.apollo.use(this.client).watchQuery({
341 ...options,
342 variables,
343 query: this.document,
344 });
345 }
346 fetch(variables, options) {
347 return this.apollo.use(this.client).query({
348 ...options,
349 variables,
350 query: this.document,
351 });
352 }
353}
354Query.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.6", ngImport: i0, type: Query, deps: [{ token: Apollo }], target: i0.ɵɵFactoryTarget.Injectable });
355Query.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.0.6", ngImport: i0, type: Query });
356i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.6", ngImport: i0, type: Query, decorators: [{
357 type: Injectable
358 }], ctorParameters: function () { return [{ type: Apollo }]; } });
359
360class Mutation {
361 constructor(apollo) {
362 this.apollo = apollo;
363 this.client = 'default';
364 }
365 mutate(variables, options) {
366 return this.apollo.use(this.client).mutate({
367 ...options,
368 variables,
369 mutation: this.document,
370 });
371 }
372}
373Mutation.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.6", ngImport: i0, type: Mutation, deps: [{ token: Apollo }], target: i0.ɵɵFactoryTarget.Injectable });
374Mutation.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.0.6", ngImport: i0, type: Mutation });
375i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.6", ngImport: i0, type: Mutation, decorators: [{
376 type: Injectable
377 }], ctorParameters: function () { return [{ type: Apollo }]; } });
378
379class Subscription {
380 constructor(apollo) {
381 this.apollo = apollo;
382 this.client = 'default';
383 }
384 subscribe(variables, options, extra) {
385 return this.apollo.use(this.client).subscribe({
386 ...options,
387 variables,
388 query: this.document,
389 }, extra);
390 }
391}
392Subscription.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.6", ngImport: i0, type: Subscription, deps: [{ token: Apollo }], target: i0.ɵɵFactoryTarget.Injectable });
393Subscription.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.0.6", ngImport: i0, type: Subscription });
394i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.6", ngImport: i0, type: Subscription, decorators: [{
395 type: Injectable
396 }], ctorParameters: function () { return [{ type: Apollo }]; } });
397
398function typedGQLTag(literals, ...placeholders) {
399 return gql$1(literals, ...placeholders);
400}
401const gql = typedGQLTag;
402const graphql = typedGQLTag;
403
404/**
405 * Generated bundle index. Do not edit.
406 */
407
408export { APOLLO_FLAGS, APOLLO_NAMED_OPTIONS, APOLLO_OPTIONS, Apollo, ApolloBase, ApolloModule, Mutation, Query, QueryRef, Subscription, gql, graphql };
409//# sourceMappingURL=ngApollo.mjs.map