Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to return multiple fetch responses on getServerSideProps or getStaticProps #159

Open
vitorcamachoo opened this issue Apr 27, 2021 · 2 comments

Comments

@vitorcamachoo
Copy link

Hiii,

I having some trouble to understand how to use this library when I want to make multiple fetch on getStaticProps (example) and return that data. I think Im not getting the correct result, because when I enter the page, the request appears on the XHR twice (one canceled, and the other with success), but it should not appear as I know.

Thanks

@correttojs
Copy link
Owner

hi,
this module is not designed to run multiple queries on getStaticProps, as it integrates together the query and the cache extraction. Nevertheless, if you pass the same apollo instance to the 2 fetch requests the cache should be populated. I suggest you to debug what is returned by the normalized cache

@WillSquire
Copy link

Tried getting onboard with this too, but ended up doing this instead:

import { InferGetServerSidePropsType } from 'next'
import { client } from '../config'
import {
  ArticlesDocument,
  ArticlesQuery,
  ArticlesQueryVariables,
} from '../middleware'

export const getServerSideProps = async () => {
  const articlesQueryResults = await client.query<
    ArticlesQuery,
    ArticlesQueryVariables
  >({
    query: ArticlesDocument,
  })

  return {
    props: {
      articlesQueryResults,
    },
  }
}

export default function Projects({
  articlesQueryResults,
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
  const { data, error } = articlesQueryResults

  // do stuff with data or error, etc...
}

To be honest a module that exports a typed client.query would be really handy to me, as I need the flexibility more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants