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