Last active
May 2, 2019 17:33
-
-
Save sebas5384/65189cd38dbd97bd823f4c95c45ba9b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fetch from "isomorphic-fetch" | |
import { createHttpLink } from "apollo-link-http" | |
import { ApolloClient } from "apollo-client" | |
import { InMemoryCache } from "apollo-cache-inmemory" | |
import { isDevelopment, isClient } from "app/lib/func" | |
import { extractCacheTags } from "app/lib/cacheTagsAfterware" | |
const createLink = context => { | |
const cacheTagExtractor = extractCacheTags(context) // Create an extractor with the `context` from nextjs. | |
const httpLinkGET = createHttpLink({ | |
uri: GRAPHQL_HOST, | |
fetch: cacheTagExtractor(fetch) // Compose fetch to extract cache tags at each request. | |
}) | |
} | |
/** | |
* Creates a new ApolloClient instance. | |
*/ | |
const create = ({ context, initialState = {}, cacheOptions = {} }) => | |
new ApolloClient({ | |
connectToDevTools: isClient() && isDevelopment(), | |
ssrMode: isServer(), // Disables forceFetch on the server (so queries are only run once) | |
link: createLink(context), // Create apollo-link with `context` fron getInitialProps() on nextjs. | |
cache: new InMemoryCache({ | |
...defaultCacheOptions, | |
...cacheOptions | |
}).restore(initialState) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment