Skip to content
New issue

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

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

Already on GitHub? # to your account

Best Approach for Prefetching Multiple Queries in Apollo with Next.js? #371

Closed
naimulemon opened this issue Oct 9, 2024 · 2 comments
Closed

Comments

@naimulemon
Copy link

naimulemon commented Oct 9, 2024

I’m working with Apollo Client in a Next.js 14 project and trying to prefetch multiple GraphQL queries using PreloadQuery. I want to ensure that this is the correct approach for prefetching multiple queries and handling the data efficiently within the component.

const IndexPage: React.FC = () => {
  return (
    <>
      <ApolloWrapper>
        <PreloadQuery query={ProductDocument}>
          <PreloadQuery
            query={gql(`query ProductDiscount($productId: Int!) {
  productDiscount(productId: $productId) {
    id
    discount
    offerTitle
  }
}`)}
          >
            <Suspense fallback={<div>Loading...</div>}>
              <ProductComponent />
            </Suspense>
          </PreloadQuery>
        </PreloadQuery>
      </ApolloWrapper>
    </>
  );
};

Is this a recommended way to prefetch multiple queries using PreloadQuery?
Are there more efficient or cleaner alternatives for handling multiple queries in this scenario, particularly with regards to code readability and performance?

@phryneas
Copy link
Member

The general advice would be to use fragment colocation to compose all your data needs into one query, but if you are using more than one query per router, you're already doing it right.

Copy link

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

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

No branches or pull requests

2 participants