Skip to content

Instantly share code, notes, and snippets.

@ziedHamdi
Last active August 27, 2021 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ziedHamdi/eed7dff4f99fbf13216e33b988797df2 to your computer and use it in GitHub Desktop.
Save ziedHamdi/eed7dff4f99fbf13216e33b988797df2 to your computer and use it in GitHub Desktop.
Ssr with Apollo and Next.js: code in page
import {gql, useQuery} from '@apollo/client'
import {Grid} from "@material-ui/core";
import {serverSideTranslations} from 'next-i18next/serverSideTranslations';
import {addApolloState, initializeApollo} from '../apollo/client'
import {getDataFromTree} from "@apollo/client/react/ssr";
export async function getServerSideProps(context) {
const client = initializeApollo({})
// await getDataFromTree(SsrPage) (not working: see bug report)
const query1 = await client.query({query: ComplaintsQuery})
const query2 = await client.query({query: TrendingComplaintsQuery})
const {locale} = context
const documentProps = addApolloState(client, {
props: {...(await serverSideTranslations(locale, ['header', 'complaintList', 'footer']))},
})
// Will be passed to the page component as props
return {props: documentProps.props}
}
export default function SsrPage(props) {
//your react standard code here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment