Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally pass context to client #83

Merged
merged 1 commit into from
Oct 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/withApollo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ type ContextWithApolloOptions = AppContext & {
} & NextPageContext &
WithApolloOptions;

type ApolloClientParam = ApolloClient<NormalizedCacheObject>
| ((ctx?: NextPageContext) => ApolloClient<NormalizedCacheObject>)

/**
* Installs the Apollo Client on NextPageContext
* or NextAppContext. Useful if you want to use apolloClient
* inside getStaticProps, getStaticPaths or getServerSideProps
* @param {NextPageContext | AppContext} ctx
*/
export const initOnContext = (
ac: ApolloClient<NormalizedCacheObject>,
acp: ApolloClientParam,
ctx: ContextWithApolloOptions
) => {
const ac = typeof acp === 'function' ? acp(ctx) : acp as ApolloClient<NormalizedCacheObject>;
const inAppContext = Boolean(ctx.ctx);

// We consider installing `withApollo({ ssr: true })` on global App level
Expand Down Expand Up @@ -76,10 +80,12 @@ export const initOnContext = (
* @param {NextPageContext} ctx
*/
const initApolloClient = (
apolloClient: ApolloClient<NormalizedCacheObject>,
acp: ApolloClientParam,
initialState: NormalizedCacheObject,
ctx: NextPageContext | undefined
) => {
const apolloClient = typeof acp === 'function' ? acp(ctx) : acp as ApolloClient<NormalizedCacheObject>;

// Make sure to create a new client for every server-side request so that data
// isn't shared between connections (which would be bad)
if (typeof window === "undefined") {
Expand All @@ -98,11 +104,11 @@ const initApolloClient = (
* Creates a withApollo HOC
* that provides the apolloContext
* to a next.js Page or AppTree.
* @param {Object} withApolloOptions
* @param {Object} ac
* @param {Boolean} [withApolloOptions.ssr=false]
* @returns {(PageComponent: NextPage) => ComponentClass<P> | FunctionComponent<P>}
*/
export default (ac: ApolloClient<NormalizedCacheObject>) => {
export default (ac: ApolloClientParam) => {
return ({ ssr = false } = {}) => (PageComponent: NextPage) => {
const WithApollo = ({
apolloClient,
Expand Down Expand Up @@ -211,10 +217,11 @@ export default (ac: ApolloClient<NormalizedCacheObject>) => {
};

const createApolloClient = (
apolloClient: ApolloClient<NormalizedCacheObject>,
acp: ApolloClientParam,
initialState: NormalizedCacheObject,
ctx: NextPageContext | undefined
) => {
) => {
const apolloClient = typeof acp === 'function' ? acp(ctx) : acp as ApolloClient<NormalizedCacheObject>;
// The `ctx` (NextPageContext) will only be present on the server.
// use it to extract auth headers (ctx.req) or similar.
(apolloClient as ApolloClient<NormalizedCacheObject> & {
Expand Down