简体   繁体   English

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

[英]GraphQL response returns [Object] instead of data

New to GraphQL and using it to return data from Wordpress API. GraphQL 新手并使用它从 Wordpress API 返回数据。 When creating a query in GraphiQL Wordpress IDE it returns the correct data, but when I implemented the query in my NEXTJS app and did a console.log I see [Object] instead of the data in the response.在 GraphiQL Wordpress IDE 中创建查询时,它返回正确的数据,但是当我在 NEXTJS 应用程序中实现查询并执行console.log时,我看到[Object]而不是响应中的数据。 I am confused as to why it appears correctly when creating the query with the IDE but when implemented does not render the data as expected.我很困惑为什么在使用 IDE 创建查询时它会正确显示,但在实现时不会按预期呈现数据。

My setup:我的设置:

//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) : 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]
    ]
  }
}

If you really want to see what's the actual data present within the console.log, you can use如果您真的想查看 console.log 中存在的实际数据是什么,您可以使用

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

also you can access specific objects by using,您还可以通过使用访问特定对象,

console.log(data.posts.nodes):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM