Skip to content
This repository was archived by the owner on Feb 24, 2021. It is now read-only.

Commit eaaa9e3

Browse files
committed
Add optional linkState argument to pass along a withClientState link for Apollo Link State
1 parent 1c10cf1 commit eaaa9e3

File tree

1 file changed

+10
-3
lines changed
  • packages/ooth-client-react-next-apollo/src

1 file changed

+10
-3
lines changed

packages/ooth-client-react-next-apollo/src/index.tsx

+10-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const getServerClient = (
3838
opts: Partial<ApolloClientOptions<any>>,
3939
cacheOpts?: ApolloReducerConfig,
4040
initialData?: any,
41+
linkState?: any,
4142
) => {
4243
let link: ApolloLink = new HttpLink({
4344
uri,
@@ -61,6 +62,10 @@ const getServerClient = (
6162
cache.restore(initialData);
6263
}
6364

65+
if (linkState) {
66+
link = ApolloLink.from([link, linkState]);
67+
}
68+
6469
return new ApolloClient({
6570
link,
6671
cache,
@@ -75,10 +80,11 @@ const getClient = (
7580
opts: Partial<ApolloClientOptions<any>>,
7681
cacheOpts?: ApolloReducerConfig,
7782
initialData?: any,
83+
linkState?: any,
7884
) => {
7985
if (!(process as any).browser) {
8086
// on server, create a new client for each request
81-
return getServerClient(uri, cookies, opts, cacheOpts, initialData);
87+
return getServerClient(uri, cookies, opts, cacheOpts, initialData, linkState);
8288
}
8389
// on client, create singleton
8490
if (!client) {
@@ -91,11 +97,12 @@ export type Options = {
9197
url: string;
9298
opts?: ApolloReducerConfig;
9399
apolloOpts?: Partial<ApolloClientOptions<any>>;
100+
linkState?: any;
94101
};
95102

96103
type Props = { initialData?: any; childProps?: any };
97104

98-
export default ({ url, opts, apolloOpts }: Options) => {
105+
export default ({ url, opts, apolloOpts, linkState }: Options) => {
99106
// tslint:disable-next-line variable-name
100107
return (Component: React.ComponentType<any> & { getInitialProps?: (ctx: any) => Promise<any> }) =>
101108
class extends React.Component<Props> {
@@ -109,7 +116,7 @@ export default ({ url, opts, apolloOpts }: Options) => {
109116

110117
public static async getInitialProps(ctx: any): Promise<{ initialData: any; childProps: any }> {
111118
const cookies = ctx.req && ctx.req.cookies;
112-
const client = getClient(url, cookies, apolloOpts || {}, opts);
119+
const client = getClient(url, cookies, apolloOpts, linkState || {}, opts);
113120

114121
const childProps = Component.getInitialProps ? await Component.getInitialProps(ctx) : {};
115122

0 commit comments

Comments
 (0)