UNPKG

10 kBJavaScriptView Raw
1import * as i0 from '@angular/core';
2import { InjectionToken, Injectable, NgZone, Optional, Inject } from '@angular/core';
3import { ApolloClient } from '@apollo/client/core';
4export { gql, gql as graphql } from '@apollo/client/core';
5import { Observable, queueScheduler, observable, from } from 'rxjs';
6import { observeOn, startWith } from 'rxjs/operators';
7
8function 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}
23class 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}
32function fixObservable(obs) {
33 obs[observable] = () => obs;
34 return obs;
35}
36function wrapWithZone(obs, ngZone) {
37 return obs.pipe(observeOn(new ZoneScheduler(ngZone)));
38}
39function pickFlag(flags, flag, defaultValue) {
40 return flags && typeof flags[flag] !== 'undefined'
41 ? flags[flag]
42 : defaultValue;
43}
44
45class 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 // ObservableQuery's methods
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 // XXX: there's a bug in apollo-client typings
78 // it should not inherit types from ObservableQuery
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
98const APOLLO_FLAGS = new InjectionToken('APOLLO_FLAGS');
99const APOLLO_OPTIONS = new InjectionToken('APOLLO_OPTIONS');
100const APOLLO_NAMED_OPTIONS = new InjectionToken('APOLLO_NAMED_OPTIONS');
101
102class 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 * Get an access to an instance of ApolloClient
126 * @deprecated use `apollo.client` instead
127 */
128 getClient() {
129 return this.client;
130 }
131 /**
132 * Set a new instance of ApolloClient
133 * Remember to clean up the store before setting a new client.
134 * @deprecated use `apollo.client = client` instead
135 *
136 * @param client ApolloClient instance
137 */
138 setClient(client) {
139 this.client = client;
140 }
141 /**
142 * Get an access to an instance of ApolloClient
143 */
144 get client() {
145 return this._client;
146 }
147 /**
148 * Set a new instance of ApolloClient
149 * Remember to clean up the store before setting a new client.
150 *
151 * @param client ApolloClient instance
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}
169class 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 * Create an instance of ApolloClient
188 * @param options Options required to create ApolloClient
189 * @param name client's name
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 * Use a default ApolloClient
201 */
202 default() {
203 return this;
204 }
205 /**
206 * Use a named ApolloClient
207 * @param name client's name
208 */
209 use(name) {
210 if (isDefault(name)) {
211 return this.default();
212 }
213 return this.map.get(name);
214 }
215 /**
216 * Create a default ApolloClient, same as `apollo.create(options)`
217 * @param options ApolloClient's options
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 * Create a named ApolloClient, same as `apollo.create(options, name)`
227 * @param name client's name
228 * @param options ApolloClient's options
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 * Remember to clean up the store before removing a client
238 * @param name client's name
239 */
240 removeClient(name) {
241 if (isDefault(name)) {
242 this._client = undefined;
243 }
244 else {
245 this.map.delete(name);
246 }
247 }
248}
249Apollo.ɵ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" });
250Apollo.decorators = [
251 { type: Injectable, args: [{
252 providedIn: 'root',
253 },] }
254];
255Apollo.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];
261function isDefault(name) {
262 return !name || name === 'default';
263}
264
265class 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}
277Query.decorators = [
278 { type: Injectable }
279];
280Query.ctorParameters = () => [
281 { type: Apollo }
282];
283
284class 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}
293Mutation.decorators = [
294 { type: Injectable }
295];
296Mutation.ctorParameters = () => [
297 { type: Apollo }
298];
299
300class 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}
309Subscription.decorators = [
310 { type: Injectable }
311];
312Subscription.ctorParameters = () => [
313 { type: Apollo }
314];
315
316/**
317 * Generated bundle index. Do not edit.
318 */
319
320export { APOLLO_FLAGS, APOLLO_NAMED_OPTIONS, APOLLO_OPTIONS, Apollo, ApolloBase, Mutation, Query, QueryRef, Subscription };
321//# sourceMappingURL=ngApollo.js.map