-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Apollo GraphQL and use it with SSG pages (fetch GraphCMS API…
… from getCommonStaticProps) and display customer label (work en/fr)
- Loading branch information
1 parent
438529e
commit d6533c4
Showing
7 changed files
with
82 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { InMemoryCache, NormalizedCacheObject } from 'apollo-cache-inmemory'; | ||
import { ApolloClient } from 'apollo-client'; | ||
import { createHttpLink } from 'apollo-link-http'; | ||
import fetch from 'isomorphic-unfetch'; | ||
|
||
const link = createHttpLink({ | ||
fetch, // Switches between unfetch & node-fetch for client & server. | ||
uri: process.env.GRAPHQL_API_ENDPOINT, | ||
|
||
// Headers applied here will be applied for all requests | ||
// See the use of the "options" when running a graphQL query to specify options per-request at https://www.apollographql.com/docs/react/api/react-hooks/#options | ||
headers: { | ||
'gcms-locale-no-default': false, | ||
'authorization': `Bearer ${process.env.GRAPHQL_API_KEY}`, | ||
}, | ||
credentials: 'same-origin', // XXX See https://www.apollographql.com/docs/react/recipes/authentication#cookie | ||
}); | ||
|
||
export const getStandaloneApolloClient = (initialState = {}): ApolloClient<NormalizedCacheObject> => { | ||
return new ApolloClient({ | ||
link: link, | ||
|
||
// XXX Very important to provide the initialState, otherwise the client will replay the query upon loading, | ||
// which is useless as the data were already fetched by the server (SSR) | ||
cache: new InMemoryCache().restore(initialState || {}), // Rehydrate the cache using the initial data passed from the server | ||
}); | ||
}; |