From 452948f200c7242fb4abc22e073f24bf89ce8a85 Mon Sep 17 00:00:00 2001 From: Murky Date: Tue, 18 Feb 2020 11:43:38 +0900 Subject: [PATCH] graphql query offline cache closes #35 --- client/graphql-client.js | 15 +++++++++++++-- package.json | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/client/graphql-client.js b/client/graphql-client.js index 3c94348..5ee37bb 100644 --- a/client/graphql-client.js +++ b/client/graphql-client.js @@ -5,6 +5,7 @@ import { HttpLink } from 'apollo-link-http' import { onError } from 'apollo-link-error' import { createUploadLink } from 'apollo-upload-client' import { BatchHttpLink } from 'apollo-link-batch-http' +import { persistCache } from 'apollo-cache-persist' const GRAPHQL_URI = '/graphql' @@ -14,7 +15,7 @@ const defaultOptions = { errorPolicy: 'ignore' }, query: { - fetchPolicy: 'no-cache', //'network-only' + fetchPolicy: 'network-first', //'network-only' errorPolicy: 'all' }, mutate: { @@ -62,7 +63,8 @@ const ERROR_HANDLER = ({ graphQLErrors, networkError }) => { } const cache = new InMemoryCache({ - addTypename: false + addTypename: false, + dataIdFromObject: object => object.key }) const httpOptions = { @@ -76,6 +78,15 @@ const httpLink = ApolloLink.split( new BatchHttpLink(httpOptions) ) +const initPersistCache = async () => { + persistCache({ + cache, + storage: window.localStorage + }) +} + +initPersistCache() + export const client = new ApolloClient({ defaultOptions, cache, diff --git a/package.json b/package.json index f723e84..405eed0 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "@types/node": "^12.11.2", "@webcomponents/webcomponentsjs": "^2.2.7", "apollo-cache-inmemory": "^1.6.1", + "apollo-cache-persist": "^0.1.1", "apollo-client": "^2.6.1", "apollo-link-batch-http": "^1.2.13", "apollo-link-error": "^1.1.10",