Skip to content

Commit 7f99ddc

Browse files
authored
Optionally pass context to client (#83)
1 parent f9c8af9 commit 7f99ddc

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/withApollo.tsx

+13-6
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@ type ContextWithApolloOptions = AppContext & {
2222
} & NextPageContext &
2323
WithApolloOptions;
2424

25+
type ApolloClientParam = ApolloClient<NormalizedCacheObject>
26+
| ((ctx?: NextPageContext) => ApolloClient<NormalizedCacheObject>)
27+
2528
/**
2629
* Installs the Apollo Client on NextPageContext
2730
* or NextAppContext. Useful if you want to use apolloClient
2831
* inside getStaticProps, getStaticPaths or getServerSideProps
2932
* @param {NextPageContext | AppContext} ctx
3033
*/
3134
export const initOnContext = (
32-
ac: ApolloClient<NormalizedCacheObject>,
35+
acp: ApolloClientParam,
3336
ctx: ContextWithApolloOptions
3437
) => {
38+
const ac = typeof acp === 'function' ? acp(ctx) : acp as ApolloClient<NormalizedCacheObject>;
3539
const inAppContext = Boolean(ctx.ctx);
3640

3741
// We consider installing `withApollo({ ssr: true })` on global App level
@@ -76,10 +80,12 @@ export const initOnContext = (
7680
* @param {NextPageContext} ctx
7781
*/
7882
const initApolloClient = (
79-
apolloClient: ApolloClient<NormalizedCacheObject>,
83+
acp: ApolloClientParam,
8084
initialState: NormalizedCacheObject,
8185
ctx: NextPageContext | undefined
8286
) => {
87+
const apolloClient = typeof acp === 'function' ? acp(ctx) : acp as ApolloClient<NormalizedCacheObject>;
88+
8389
// Make sure to create a new client for every server-side request so that data
8490
// isn't shared between connections (which would be bad)
8591
if (typeof window === "undefined") {
@@ -98,11 +104,11 @@ const initApolloClient = (
98104
* Creates a withApollo HOC
99105
* that provides the apolloContext
100106
* to a next.js Page or AppTree.
101-
* @param {Object} withApolloOptions
107+
* @param {Object} ac
102108
* @param {Boolean} [withApolloOptions.ssr=false]
103109
* @returns {(PageComponent: NextPage) => ComponentClass<P> | FunctionComponent<P>}
104110
*/
105-
export default (ac: ApolloClient<NormalizedCacheObject>) => {
111+
export default (ac: ApolloClientParam) => {
106112
return ({ ssr = false } = {}) => (PageComponent: NextPage) => {
107113
const WithApollo = ({
108114
apolloClient,
@@ -211,10 +217,11 @@ export default (ac: ApolloClient<NormalizedCacheObject>) => {
211217
};
212218

213219
const createApolloClient = (
214-
apolloClient: ApolloClient<NormalizedCacheObject>,
220+
acp: ApolloClientParam,
215221
initialState: NormalizedCacheObject,
216222
ctx: NextPageContext | undefined
217-
) => {
223+
) => {
224+
const apolloClient = typeof acp === 'function' ? acp(ctx) : acp as ApolloClient<NormalizedCacheObject>;
218225
// The `ctx` (NextPageContext) will only be present on the server.
219226
// use it to extract auth headers (ctx.req) or similar.
220227
(apolloClient as ApolloClient<NormalizedCacheObject> & {

0 commit comments

Comments
 (0)