GraphQL响应返回[Object]而不是数据



GraphQL新手,并使用它从Wordpress API返回数据。在graphhiql Wordpress IDE中创建查询时,它返回正确的数据,但是当我在NEXTJS应用程序中实现查询并执行console.log时,我看到[Object]而不是响应中的数据。我很困惑为什么在使用IDE创建查询时正确出现,但在实现时没有按预期呈现数据。

我的设置:

//apollo-client.js
import { ApolloClient, InMemoryCache } from '@apollo/client';
const client = new ApolloClient({
uri: `${process.env.NEXT_PUBLIC_GRAPH_QL}`,
cache: new InMemoryCache({
addTypename: false,
}),
});
export default client;
//index.ts
import { gql } from '@apollo/client';
import client from '../../apollo-client';
export async function getStaticProps() {
const { data } = await client.query({
query: gql`
query getPosts {
posts(first: 20) {
nodes {
title
featuredImage {
node {
sourceUrl
} 
}
}
}
}
`,
});
return {
props: {
posts: data
},
};

console.log(posts):

{
posts: {
nodes: [
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object]
]
}
}

如果您真的想查看console.log中显示的实际数据,您可以使用

console.log(JSON.stringify(posts)):

也可以使用

来访问特定的对象
console.log(data.posts.nodes):

最新更新