forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Examples] Move with-typescript-graphql to SSG (vercel#13854)
Related to vercel#11014
- Loading branch information
Showing
12 changed files
with
152 additions
and
253 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,35 @@ | ||
.next | ||
node_modules | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# next.js | ||
/.next/ | ||
/out/ | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
# Graphql | ||
*.graphql.d.ts | ||
*.graphqls.d.ts |
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,59 @@ | ||
import { IncomingMessage, ServerResponse } from 'http' | ||
import { useMemo } from 'react' | ||
import { ApolloClient } from 'apollo-client' | ||
import { InMemoryCache, NormalizedCacheObject } from 'apollo-cache-inmemory' | ||
|
||
let apolloClient: ApolloClient<NormalizedCacheObject> | undefined | ||
|
||
export type ResolverContext = { | ||
req?: IncomingMessage | ||
res?: ServerResponse | ||
} | ||
|
||
function createIsomorphLink(context: ResolverContext = {}) { | ||
if (typeof window === 'undefined') { | ||
const { SchemaLink } = require('apollo-link-schema') | ||
const { schema } = require('./schema') | ||
return new SchemaLink({ schema, context }) | ||
} else { | ||
const { HttpLink } = require('apollo-link-http') | ||
return new HttpLink({ | ||
uri: '/api/graphql', | ||
credentials: 'same-origin', | ||
}) | ||
} | ||
} | ||
|
||
function createApolloClient(context?: ResolverContext) { | ||
return new ApolloClient({ | ||
ssrMode: typeof window === 'undefined', | ||
link: createIsomorphLink(context), | ||
cache: new InMemoryCache(), | ||
}) | ||
} | ||
|
||
export function initializeApollo( | ||
initialState: any = null, | ||
// Pages with Next.js data fetching methods, like `getStaticProps`, can send | ||
// a custom context which will be used by `SchemaLink` to server render pages | ||
context?: ResolverContext | ||
) { | ||
const _apolloClient = apolloClient ?? createApolloClient(context) | ||
|
||
// If your page has Next.js data fetching methods that use Apollo Client, the initial state | ||
// get hydrated here | ||
if (initialState) { | ||
_apolloClient.cache.restore(initialState) | ||
} | ||
// For SSG and SSR always create a new Apollo Client | ||
if (typeof window === 'undefined') return _apolloClient | ||
// Create the Apollo Client once in the client | ||
if (!apolloClient) apolloClient = _apolloClient | ||
|
||
return _apolloClient | ||
} | ||
|
||
export function useApollo(initialState: any) { | ||
const store = useMemo(() => initializeApollo(initialState), [initialState]) | ||
return store | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.