Gatsby - 对 Contentful 进行动态查询

Gatsby - making a dynamic query to Contentful

我正在开发一个基于 GatsbyContentful 的简单电子商务。 我的其中一个页面是 CartPage。 因此,我正在尝试查询 Contenful 以获取有关我的产品的必要信息,因此我想传递用户购物车中的产品 ID 列表。

这是我的组件和查询:

export interface CartPageProps {
  data: {
    products: GQL.ContentfulProductConnection;
  };
}

const CartPage = ({ data }: CartPageProps) => {

  const mockCart = [1, 2];
  console.log(data);

   return (
    <IndexLayout>
      <Page>
      //
      // Layout goes here
      //
      </Page>
    </IndexLayout>
    )
}
export default CartPage;

const pageQuery = graphql`
  query ProductsInCart($mockCart: [Int], $lang: String) {
    products: allContentfulProduct(filter: { idProduct: { in: [1, 2] }, node_locale: { eq: "en" } }) {
      edges {
        node {
          id
          idProduct
          price
          title
          quantity
          photo {
            resize(height: 200, width: 200) {
              src
            }
          }
          slug
        }
      }
    }
  }
`;

现在我在 console.log(data) 上得到的只是 undefined

如果我执行相同的查询但使用 useStaticQuery 并且所有数据都被硬编码 - 我会得到所需的数据。但是 useStaticQuery 不接受变量。

我是不是做错了什么?如何将变量传递给查询并将我的数据放入道具?

Gatsby 在构建时使用 GraphQL,而不是用于实时站点。正如所说 here 所以它不是为了在旅途中进行 GraphQL 查询。