Skip to content

Instantly share code, notes, and snippets.

@kylemocode
Last active July 13, 2022 07:55
Show Gist options
  • Save kylemocode/56fe3ab0536dfa7026601cbe6a94163d to your computer and use it in GitHub Desktop.
Save kylemocode/56fe3ab0536dfa7026601cbe6a94163d to your computer and use it in GitHub Desktop.
let mswEnabled = false;
const customFetch = async (uri: string, options: RequestInit) => {
if (typeof window !== 'undefined' && !mswEnabled) {
const { worker } = await import('@/mocks/worker');
await worker.start();
mswEnabled = true;
}
return fetch(uri, options);
};
const httpLinkOption = {
uri: URL_YOU_WANT_TO_MOCK,
...(process.env.NEXT_PUBLIC_USE_MOCK && { fetch: customFetch }),
};
const apiLink: ApolloLink = new HttpLink(httpLinkOption);
...
...
return new ApolloClient({
link: apiLink,
cache,
...
...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment