Skip to content

Commit

Permalink
graphql query offline cache
Browse files Browse the repository at this point in the history
closes #35
  • Loading branch information
Murky committed Feb 18, 2020
1 parent fa5935d commit 452948f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
15 changes: 13 additions & 2 deletions client/graphql-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -14,7 +15,7 @@ const defaultOptions = {
errorPolicy: 'ignore'
},
query: {
fetchPolicy: 'no-cache', //'network-only'
fetchPolicy: 'network-first', //'network-only'
errorPolicy: 'all'
},
mutate: {
Expand Down Expand Up @@ -62,7 +63,8 @@ const ERROR_HANDLER = ({ graphQLErrors, networkError }) => {
}

const cache = new InMemoryCache({
addTypename: false
addTypename: false,
dataIdFromObject: object => object.key
})

const httpOptions = {
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 452948f

Please sign in to comment.