UNPKG

4.39 kBJavaScriptView Raw
1import * as tslib_1 from "tslib";
2export * from 'apollo-client';
3export * from 'apollo-link';
4export * from 'apollo-cache-inmemory';
5import { ApolloLink, Observable } from 'apollo-link';
6import { HttpLink } from 'apollo-link-http';
7import { withClientState } from 'apollo-link-state';
8import { onError } from 'apollo-link-error';
9import { InMemoryCache } from 'apollo-cache-inmemory';
10import gql from 'graphql-tag';
11import ApolloClient from 'apollo-client';
12export { gql, HttpLink };
13var PRESET_CONFIG_KEYS = [
14 'request',
15 'uri',
16 'credentials',
17 'headers',
18 'fetch',
19 'fetchOptions',
20 'clientState',
21 'onError',
22 'cacheRedirects',
23 'cache',
24];
25var DefaultClient = (function (_super) {
26 tslib_1.__extends(DefaultClient, _super);
27 function DefaultClient(config) {
28 if (config === void 0) { config = {}; }
29 var _this = this;
30 if (config) {
31 var diff = Object.keys(config).filter(function (key) { return PRESET_CONFIG_KEYS.indexOf(key) === -1; });
32 if (diff.length > 0) {
33 console.warn('ApolloBoost was initialized with unsupported options: ' +
34 ("" + diff.join(' ')));
35 }
36 }
37 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;
38 var cache = config.cache;
39 if (cache && cacheRedirects) {
40 throw new Error('Incompatible cache configuration. If providing `cache` then ' +
41 'configure the provided instance with `cacheRedirects` instead.');
42 }
43 if (!cache) {
44 cache = cacheRedirects
45 ? new InMemoryCache({ cacheRedirects: cacheRedirects })
46 : new InMemoryCache();
47 }
48 var stateLink = clientState
49 ? withClientState(tslib_1.__assign({}, clientState, { cache: cache }))
50 : false;
51 var errorLink = errorCallback
52 ? onError(errorCallback)
53 : onError(function (_a) {
54 var graphQLErrors = _a.graphQLErrors, networkError = _a.networkError;
55 if (graphQLErrors) {
56 graphQLErrors.map(function (_a) {
57 var message = _a.message, locations = _a.locations, path = _a.path;
58 return console.log("[GraphQL error]: Message: " + message + ", Location: " +
59 (locations + ", Path: " + path));
60 });
61 }
62 if (networkError) {
63 console.log("[Network error]: " + networkError);
64 }
65 });
66 var requestHandler = request
67 ? new ApolloLink(function (operation, forward) {
68 return new Observable(function (observer) {
69 var handle;
70 Promise.resolve(operation)
71 .then(function (oper) { return request(oper); })
72 .then(function () {
73 handle = forward(operation).subscribe({
74 next: observer.next.bind(observer),
75 error: observer.error.bind(observer),
76 complete: observer.complete.bind(observer),
77 });
78 })
79 .catch(observer.error.bind(observer));
80 return function () {
81 if (handle) {
82 handle.unsubscribe();
83 }
84 };
85 });
86 })
87 : false;
88 var httpLink = new HttpLink({
89 uri: uri || '/graphql',
90 fetch: fetch,
91 fetchOptions: fetchOptions || {},
92 credentials: credentials || 'same-origin',
93 headers: headers || {},
94 });
95 var link = ApolloLink.from([
96 errorLink,
97 requestHandler,
98 stateLink,
99 httpLink,
100 ].filter(function (x) { return !!x; }));
101 _this = _super.call(this, { cache: cache, link: link }) || this;
102 return _this;
103 }
104 return DefaultClient;
105}(ApolloClient));
106export default DefaultClient;
107//# sourceMappingURL=index.js.map
\No newline at end of file