UNPKG

5.34 kBJavaScriptView Raw
1import { __extends } from 'tslib';
2import ApolloClient__default from 'apollo-client';
3export * from 'apollo-client';
4import { ApolloLink, Observable } from 'apollo-link';
5export * from 'apollo-link';
6import { InMemoryCache } from 'apollo-cache-inmemory';
7export * from 'apollo-cache-inmemory';
8import { HttpLink } from 'apollo-link-http';
9export { HttpLink } from 'apollo-link-http';
10import { onError } from 'apollo-link-error';
11export { default as gql } from 'graphql-tag';
12import { invariant } from 'ts-invariant';
13
14var PRESET_CONFIG_KEYS = [
15 'request',
16 'uri',
17 'credentials',
18 'headers',
19 'fetch',
20 'fetchOptions',
21 'clientState',
22 'onError',
23 'cacheRedirects',
24 'cache',
25 'name',
26 'version',
27 'resolvers',
28 'typeDefs',
29 'fragmentMatcher',
30];
31var DefaultClient = (function (_super) {
32 __extends(DefaultClient, _super);
33 function DefaultClient(config) {
34 if (config === void 0) { config = {}; }
35 var _this = this;
36 if (config) {
37 var diff = Object.keys(config).filter(function (key) { return PRESET_CONFIG_KEYS.indexOf(key) === -1; });
38 if (diff.length > 0) {
39 process.env.NODE_ENV === "production" || invariant.warn('ApolloBoost was initialized with unsupported options: ' +
40 ("" + diff.join(' ')));
41 }
42 }
43 var request = config.request, uri = config.uri, credentials = config.credentials, headers = config.headers, fetch = config.fetch, fetchOptions = config.fetchOptions, clientState = config.clientState, cacheRedirects = config.cacheRedirects, errorCallback = config.onError, name = config.name, version = config.version, resolvers = config.resolvers, typeDefs = config.typeDefs, fragmentMatcher = config.fragmentMatcher;
44 var cache = config.cache;
45 process.env.NODE_ENV === "production" ? invariant(!cache || !cacheRedirects) : invariant(!cache || !cacheRedirects, 'Incompatible cache configuration. If providing `cache` then ' +
46 'configure the provided instance with `cacheRedirects` instead.');
47 if (!cache) {
48 cache = cacheRedirects
49 ? new InMemoryCache({ cacheRedirects: cacheRedirects })
50 : new InMemoryCache();
51 }
52 var errorLink = errorCallback
53 ? onError(errorCallback)
54 : onError(function (_a) {
55 var graphQLErrors = _a.graphQLErrors, networkError = _a.networkError;
56 if (graphQLErrors) {
57 graphQLErrors.map(function (_a) {
58 var message = _a.message, locations = _a.locations, path = _a.path;
59 return process.env.NODE_ENV === "production" || invariant.warn("[GraphQL error]: Message: " + message + ", Location: " +
60 (locations + ", Path: " + path));
61 });
62 }
63 if (networkError) {
64 process.env.NODE_ENV === "production" || invariant.warn("[Network error]: " + networkError);
65 }
66 });
67 var requestHandler = request
68 ? new ApolloLink(function (operation, forward) {
69 return new Observable(function (observer) {
70 var handle;
71 Promise.resolve(operation)
72 .then(function (oper) { return request(oper); })
73 .then(function () {
74 handle = forward(operation).subscribe({
75 next: observer.next.bind(observer),
76 error: observer.error.bind(observer),
77 complete: observer.complete.bind(observer),
78 });
79 })
80 .catch(observer.error.bind(observer));
81 return function () {
82 if (handle) {
83 handle.unsubscribe();
84 }
85 };
86 });
87 })
88 : false;
89 var httpLink = new HttpLink({
90 uri: uri || '/graphql',
91 fetch: fetch,
92 fetchOptions: fetchOptions || {},
93 credentials: credentials || 'same-origin',
94 headers: headers || {},
95 });
96 var link = ApolloLink.from([errorLink, requestHandler, httpLink].filter(function (x) { return !!x; }));
97 var activeResolvers = resolvers;
98 var activeTypeDefs = typeDefs;
99 var activeFragmentMatcher = fragmentMatcher;
100 if (clientState) {
101 if (clientState.defaults) {
102 cache.writeData({
103 data: clientState.defaults,
104 });
105 }
106 activeResolvers = clientState.resolvers;
107 activeTypeDefs = clientState.typeDefs;
108 activeFragmentMatcher = clientState.fragmentMatcher;
109 }
110 _this = _super.call(this, {
111 cache: cache,
112 link: link,
113 name: name,
114 version: version,
115 resolvers: activeResolvers,
116 typeDefs: activeTypeDefs,
117 fragmentMatcher: activeFragmentMatcher,
118 }) || this;
119 return _this;
120 }
121 return DefaultClient;
122}(ApolloClient__default));
123
124export default DefaultClient;
125//# sourceMappingURL=bundle.esm.js.map