Skip to content

Commit

Permalink
feat: pass router to init function so that e.g. locale can be passed …
Browse files Browse the repository at this point in the history
…to apollo
  • Loading branch information
macrozone committed Apr 18, 2021
1 parent a2b9606 commit 1853249
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ApolloClient from 'apollo-client';
import { IncomingHttpHeaders } from 'http';
import { NextPage, NextPageContext } from 'next';
import { AppContext } from 'next/app';
import { NextRouter } from 'next/dist/client/router';
import { ReactNode } from 'react';

export interface WithApolloOptions {
Expand All @@ -25,6 +26,7 @@ export interface WithApolloProps<TCache> {
export interface InitApolloOptions<TCache> {
ctx?: NextPageContext;
headers?: IncomingHttpHeaders;
router?: NextRouter;
initialState?: TCache;
}

Expand Down
7 changes: 6 additions & 1 deletion src/withApollo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,14 @@ export default function withApollo<TCache = any>(
if (getInitialProps || getDataFromTree) {
WithApollo.getInitialProps = async (pageCtx: ApolloContext) => {
const ctx = 'Component' in pageCtx ? pageCtx.ctx : pageCtx;
const router = 'Component' in pageCtx ? pageCtx.router : undefined;
const { AppTree } = pageCtx;
const headers = ctx.req ? ctx.req.headers : {};
const apollo = initApollo<TCache>(client, { ctx, headers });
const apollo = initApollo<TCache>(client, {
ctx,
headers,
router
});
const apolloState: WithApolloState<TCache> = {};

let pageProps = {};
Expand Down

0 comments on commit 1853249

Please sign in to comment.