Repository URL to install this package:
|
Version:
1.3.2 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @file @todo https://www.apollographql.com/docs/react/features/performance.html#cache-redirects
* @todo @see https://www.apollographql.com/docs/react/advanced/boost-migration
*/
const apollo_boost_1 = require("apollo-boost");
const apollo_cache_persist_1 = require("apollo-cache-persist");
const apollo_link_error_1 = require("apollo-link-error");
const persistence_1 = require("@skava/persistence");
const exotic_1 = require("exotic");
const IS_BROWSER = typeof window === 'object';
const proxyUrl = process.env.API_URL === '"' ? '' : process.env.API_URL || '';
const apiBaseUrl = proxyUrl
? proxyUrl
: IS_BROWSER
? ''
: process.env.SERVER_URL;
/**
* @note we add `credentials` to send cookies via fetch
* @todo @see https://github.com/bitinn/node-fetch/issues/49
* @see https://github.com/apollographql/apollo-link/issues/83
* @see https://github.com/github/fetch#sending-cookies
*/
const cookieFetch = (url, options) => {
const credentials = process.env.SUBAPP_GRAPHQL
? 'same-origin'
: 'include';
const fetchOptions = Object.assign({}, options, { credentials });
return fetch(url, fetchOptions);
};
const uri = `${apiBaseUrl}/graphql`;
exports.uri = uri;
const httpLink = new apollo_boost_1.HttpLink({ uri, fetch: cookieFetch });
exports.httpLink = httpLink;
/**
* @api https://www.apollographql.com/docs/react/features/cache-updates.html#normalization
* @todo freeze @@perf
*/
const inMemoryCacheConfig = {};
const cache = new apollo_boost_1.InMemoryCache(inMemoryCacheConfig).restore(IS_BROWSER && window.__APOLLO_STATE__);
exports.cache = cache;
/**
* Apollo Middlewares
*/
const logError = (error) => {
const { message, locations, path } = error;
console.error(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`);
};
const errorLink = apollo_link_error_1.onError(namedErrorResponseParams => {
const { graphQLErrors, networkError, response, operation, } = namedErrorResponseParams;
if (exotic_1.isObj(graphQLErrors)) {
const list = graphQLErrors;
list.forEach(logError);
}
if (!exotic_1.isEmpty(networkError)) {
console.error(`[Network error]: ${networkError}`);
}
const hasError = exotic_1.isObj(graphQLErrors) || !exotic_1.isEmpty(networkError);
const isValidResponse = exotic_1.isObj(response);
if (hasError && isValidResponse) {
// ignore... for now
response.errors = null;
}
});
/**
* @see https://github.com/apollographql/apollo-link/tree/master/packages/apollo-link-error
* @todo https://www.apollographql.com/docs/link/index.html#graphql-tools custom fetcher?
*/
const consoleLink = new apollo_boost_1.ApolloLink((operation, forward) => {
console.info(`starting request for ${operation.operationName}`);
return forward(operation).map(data => {
console.info(`ending request for ${operation.operationName}`);
return data;
});
});
const link = apollo_boost_1.ApolloLink.from([errorLink, consoleLink, httpLink]);
/**
* @see https://github.com/apollographql/apollo-cache-persist/blob/0bc569308379b4cc59b2c7f5f4823428b208ada0/src/types/index.ts
*/
if (process.env.USE_APOLLO_CACHE) {
const cacheConfig = {
cache,
// @todo this isn't working for types
storage: persistence_1.oneStorage,
key: 'graphql-client-cache',
};
apollo_cache_persist_1.persistCache(cacheConfig);
}
/**
* @todo
* @requires https://github.com/apollographql/apollo-client/blob/master/docs/source/recipes/server-side-rendering.md#store-rehydration
*
* @see https://github.com/apollographql/apollo-client/blob/82a846c9591bcff975cc28d3786105b80a49b4ba/src/queries/queryTransform.ts#L30
* @see https://github.com/apollographql/apollo-client/issues/1913#issuecomment-348359030
*/
const clientConfig = {
link,
cache,
ssrMode: true,
};
exports.clientConfig = clientConfig;
if (process.env.DISABLE_SSR && IS_BROWSER === true) {
clientConfig.ssrMode = false;
}
if (!process.env.DISABLE_CACHE) {
clientConfig.cache = cache;
}
if (IS_BROWSER === false) {
// @see https://github.com/apollographql/apollo-client/issues/1419
// clientConfig.cache = undefined
}
const client = new apollo_boost_1.ApolloClient(clientConfig);
exports.client = client;
// important
exports.default = client;
//# sourceMappingURL=apolloClient.js.map