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