James Peters, It depends on where and when do you want to fetch the product list. But you can actually do the same in both components of losing and checkout using the react-appolo library as you are calling it directly from GraphQL.
These steps may be helpful,
- First define the GraphQL query. Here is an example of fetching tags,
const ALL_TAGS = gql`
query {
allTags {
data {
_id
name
description
}
}
}
`
- Then use the
useQuery hook from the apollo react lib.
const { loading: tagLoading, data: tagData } = useQuery(ALL_TAGS)
- Now you can use the tagData and that has the fetched data.
Note, the lib has the hooks for both fetching and mutating data,
import { useQuery, useMutation } from "@apollo/react-hooks"
James Peters
Great guide man, thanks for writing it! It works great when you manually add the products through products.json, but I am currently trying to use this is on a website where I am getting the product data from Contentful / graphql. How would I go about adding the products from the graphql query to products.json or the checkout function? Thanks again and sorry if this is a stupid question but I'm fairly new to web development.