next-with-gqless-example
This app is an example of how to use gqless with Next.js. It mirrors the with-apollo Next.js example app. Apollo examples are included here for comparison.
The Gist
- Copy boilerplate code from ./lib/gqless.js (including
withGqless&useGqless) into your project- Copy the file
- Install the required
react-ssr-prepasspackage (actually@zen_flow/react-ssr-prepassuntil this patch is released). - Add the required
getClientfunction togqless/client.ts.export const createClient = (queryFetcher = fetchQuery) => new Client<Query>(schema.Query, queryFetcher);
- Use the
withGqlessHOC on the Next.js pages you want SSR'd. This will add a React context provider for the gqless client and wrapgetInitialPropsto prefetch the necessary data before displaying the page. - Use the
useGqlesshook to access the gqlessclient(andquery) from context (in components that will be SSR'd). We cannot use the singletonclient(orquery) exported fromClient.tsfor SSR because on the server we want to have one client per request. - Use
SsrCompatibleSuspenseinstead of the regular ReactSuspensecomponent. This will stop ReactDOMServer from complaining and crashing when<Suspense/>is encountered during SSR. The caveat here is that React will log warnings when rehydrating browser-side, since what's rendered on browser is supposed to match what was rendered on the server. (Issue #2)