【发布时间】:2022-01-09 07:41:30
【问题描述】:
我是 graphql 的新手。我有一个前端项目(nextjs)和后端(strapi)。
这是我的代码
import { ApolloClient, InMemoryCache, gql } from '@apollo/client'
export default function Blog({ posts }) {
console.log('posts', posts)
return (
<div>
{posts.map(post => {
return (
<div>
<p>{posts.heading}</p>
</div>
)
})}
</div>
)
}
export async function getStaticProps() {
const client = new ApolloClient({
url: 'http://localhost:1337/graphql/',
cache: new InMemoryCache(),
})
const { data } = await client.query({
query: gql`
query {
posts {
data {
attributes {
heading
}
}
}
}
`,
})
return {
props: {
posts: data.posts,
},
}
}
除此之外,我还收到此消息“无法解构中间值的属性”。有谁知道为什么,我确定代码是正确的。
【问题讨论】:
-
const response = await client.query ...记录响应,稍后解构...使用邮递员/游乐场测试查询? -
你能给我一个工作的例子吗?
标签: graphql next.js apollo-client strapi graphql-js