From 853ec004668fb3bc795ba7d68e676a752b1303a1 Mon Sep 17 00:00:00 2001 From: Pavel910 Date: Mon, 3 Jun 2019 13:07:09 +0200 Subject: [PATCH] fix: update apollo configs to always use cache. --- packages/webiny-cli/package.json | 2 +- .../packages/admin/src/config/apollo.js | 50 +++++-------------- .../packages/site/src/config/apollo.js | 48 +++++------------- 3 files changed, 25 insertions(+), 75 deletions(-) diff --git a/packages/webiny-cli/package.json b/packages/webiny-cli/package.json index a5ade89fbfc..9ff5698d2c0 100644 --- a/packages/webiny-cli/package.json +++ b/packages/webiny-cli/package.json @@ -1,6 +1,6 @@ { "name": "webiny-cli", - "version": "2.2.3", + "version": "2.2.4", "main": "index.js", "bin": { "webiny": "./lib/index.js", diff --git a/packages/webiny-cli/src/init/template/packages/admin/src/config/apollo.js b/packages/webiny-cli/src/init/template/packages/admin/src/config/apollo.js index 4a3a53f0d5d..14f58a4d303 100644 --- a/packages/webiny-cli/src/init/template/packages/admin/src/config/apollo.js +++ b/packages/webiny-cli/src/init/template/packages/admin/src/config/apollo.js @@ -5,43 +5,17 @@ import { InMemoryCache } from "apollo-cache-inmemory"; import { createAuthLink } from "webiny-app-security/components"; import { createOmitTypenameLink } from "webiny-app/graphql"; -let config; +const isProduction = process.env.NODE_ENV === "production"; +const uriPrefix = isProduction ? "" : process.env.REACT_APP_FUNCTIONS_HOST; -const cache = new InMemoryCache({ - addTypename: true, - dataIdFromObject: obj => obj.id || null +export default new ApolloClient({ + link: ApolloLink.from([ + createOmitTypenameLink(), + createAuthLink(), + new BatchHttpLink({ uri: uriPrefix + "/function/api" }) + ]), + cache: new InMemoryCache({ + addTypename: true, + dataIdFromObject: obj => obj.id || null + }) }); - -if (process.env.NODE_ENV === "production") { - config = { - link: ApolloLink.from([ - createOmitTypenameLink(), - createAuthLink(), - new BatchHttpLink({ uri: "/function/api" }) - ]), - cache - }; -} - -if (process.env.NODE_ENV === "development") { - config = { - link: ApolloLink.from([ - createOmitTypenameLink(), - createAuthLink(), - new BatchHttpLink({ uri: process.env.REACT_APP_FUNCTIONS_HOST + "/function/api" }) - ]), - cache, - defaultOptions: { - watchQuery: { - fetchPolicy: "network-only", - errorPolicy: "all" - }, - query: { - fetchPolicy: "network-only", - errorPolicy: "all" - } - } - }; -} - -export default new ApolloClient(config); diff --git a/packages/webiny-cli/src/init/template/packages/site/src/config/apollo.js b/packages/webiny-cli/src/init/template/packages/site/src/config/apollo.js index 20e6387acdf..a0c0e577f29 100644 --- a/packages/webiny-cli/src/init/template/packages/site/src/config/apollo.js +++ b/packages/webiny-cli/src/init/template/packages/site/src/config/apollo.js @@ -5,48 +5,24 @@ import { InMemoryCache } from "apollo-cache-inmemory"; import { createAuthLink } from "webiny-app-security/components"; import { createOmitTypenameLink } from "webiny-app/graphql"; -let config; +const isProduction = process.env.NODE_ENV === "production"; +const uriPrefix = isProduction ? "" : process.env.REACT_APP_FUNCTIONS_HOST; const cache = new InMemoryCache({ addTypename: true, dataIdFromObject: obj => obj.id || null }); -if (process.env.NODE_ENV === "production") { +if (isProduction && process.env.REACT_APP_ENV === "browser") { // Production build of this app will be rendered using SSR so we need to restore cache from pre-rendered state. - if (process.env.REACT_APP_ENV === "browser") { - cache.restore(window.__APOLLO_STATE__); - } - - config = { - link: ApolloLink.from([ - createOmitTypenameLink(), - createAuthLink(), - new BatchHttpLink({ uri: "/function/api" }) - ]), - cache - }; -} - -if (process.env.NODE_ENV === "development") { - config = { - link: ApolloLink.from([ - createOmitTypenameLink(), - createAuthLink(), - new BatchHttpLink({ uri: process.env.REACT_APP_FUNCTIONS_HOST + "/function/api" }) - ]), - cache, - defaultOptions: { - watchQuery: { - fetchPolicy: "network-only", - errorPolicy: "all" - }, - query: { - fetchPolicy: "network-only", - errorPolicy: "all" - } - } - }; + cache.restore(window.__APOLLO_STATE__); } -export default new ApolloClient(config); +export default new ApolloClient({ + link: ApolloLink.from([ + createOmitTypenameLink(), + createAuthLink(), + new BatchHttpLink({ uri: uriPrefix + "/function/api" }) + ]), + cache +});