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