【发布时间】:2020-10-11 13:58:25
【问题描述】:
我有一个 .ts(不是 .tsx) 文件,它只导出一个 json 对象,例如
const obj = {
img1: gql_img1,
img2: gql_img2
}
我希望 gq1_img1 和 gq1_img2 成为 graphql 查询的结果
我找到了solution which uses Apollo Client,,但看起来他们没有使用 Gatsby,而且我认为 Gatsby 没有使用客户端。
使用useStaticQuery的问题在于它是一个钩子,如果我尝试像下面的sn-p那样使用它,我会得到"Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:"
const gql = () => {
const gql = useStaticQuery(graphql
`query adQuery {
invoiceNinja300x250: file(
extension: {regex: "/(jpg)|(jpeg)|(png)/"},
name: {eq: "IN-300x250-2"}
){
childImageSharp {
fluid(maxWidth: 250) {
...GatsbyImageSharpFluid_withWebp_noBase64
}
}
},
invoiceNinja600x300: file(
extension: {regex: "/(jpg)|(jpeg)|(png)/"},
name: {eq: "IN-600x300-2"}
){
childImageSharp {
fluid(maxWidth: 250) {
...GatsbyImageSharpFluid_withWebp_noBase64
}
}
}
}`
)
return gql
}
const GQL = gql()
【问题讨论】:
标签: reactjs typescript graphql gatsby