我有一个需要查询数据的 React 组件。我想将参数/变量传递给查询。但我不知道该怎么做。
例如。我有这个博客项目轮播,它想要查询 x 数量的项目和 x 数量的偏移量(跳过第一个 x 数字)。
这大概是我现在所拥有的。但我不知道如何在 GraphQL 查询中传递变量
import React from "react"
import {graphql, useStaticQuery} from "gatsby";
import BlockCarousel from "../blockCarousel/blockCarousel";
const BlockCarouselBlog = ({block,data}) => {
let queryArgs = {
limit: 10,
skip: 0
};
block.blocks = GetBlogItems(queryArgs);
return (
<BlockCarousel block={block} data={data} />
)
};
export default BlockCarouselBlog
const GetBlogItems = (args) => {
let data = useStaticQuery(graphql`
query {
allSanityBlog(
sort: { fields: [_createdAt], order: DESC }
limit: 10 // this needs the variable
skip: 0 // this needs the variable
) {
... rest of the data
}
}
`)
if (data.allSanityBlog.edges)
return data.allSanityBlog.edges
return null;
}
问题
如何将参数传递给组件的 Gatsby GraphQL 查询?