简体   繁体   English

使用 GraphQL (Nuxt) 访问嵌套节点

[英]Access nested nodes with GraphQL (Nuxt)

i'm experiencing some issues with Apollo, GraphQL and Nuxt.我在 Apollo、GraphQL 和 Nuxt 方面遇到了一些问题。 i don't know if it's especially related to Nuxt though, or with vue.我不知道它是否与 Nuxt 特别相关,或者与 vue.js 相关。

i'm trying to use WordPress as headless CMS via WP-GraphQL plugin.我正在尝试通过 WP-GraphQL 插件将 WordPress 用作无头 CMS。 here's my query这是我的查询

WP-GraphQL interface WP-GraphQL 接口

i basically created a graphql folder with a posts.js file inside, that contains my query我基本上创建了一个带有 posts.js 文件的 graphql 文件夹,其中包含我的查询

import gql from 'graphql-tag'
export const myQuery = gql`
  query myQuery {
    posts {
      nodes {
        id
        title
        date
        slug
        author {
          node {
            name
          }
        }
        featuredImage {
          node {
            uri
            sourceUrl
            srcSet
          }
        }
      }
    }
  }
`

then, all i need to do is to print my data in the template.然后,我需要做的就是在模板中打印我的数据。 here's the script part first.首先是脚本部分。

<script>
import { myQuery } from '~/graphql/posts'

export default {
  data() {
    return {
      posts: [],
    }
  },

  apollo: {
    posts: {
      prefetch: true,
      query: myQuery,
    },
  },

  watch: {
    async $route() {
      await this.$nuxt.refresh()
      window.scrollTo(0, 0)
    },
  },
  transition: 'home',

  async mounted() {
    this.posts = await this.$apollo.query({ query: myQuery })
    this.posts = this.posts.data.posts.nodes
    this.loading = false
}
</script>

and then comes the template :然后是模板:

<template>
    <section class="featured-projects">
      <div class="featured-projects__wrapper">
        <article v-for="post in posts" :key="post.id">
          <p>{{ post.id }}</p>
          <h2>{{ post.title }}</h2>
          <span>{{ post.date }}</span>
        </article>
      </div>
    </section>
  </section>
</template>

everything just works!一切正常!

now, i would like to print post author name as well.现在,我也想打印帖子作者姓名。 i first tried this :我第一次尝试这个:

<span>{{ post.author }}</span>

and this actually prints this :这实际上打印了这个:

{
    "node": {
        "name": "max max",
        "__typename": "User"
    },
    "__typename": "NodeWithAuthorToUserConnectionEdge"
}

it totally makes sense, as author is an object with nested items in it.这完全有道理,因为作者是一个包含嵌套项目的对象。 so according to what i'm being returned and following GraphQL API structure, to display post author name, think i should do something like this instead :因此,根据我返回的内容并遵循 GraphQL API 结构,要显示帖子作者姓名,我认为我应该这样做:

<span>{{ post.author.node.name }}</span>

and here's the error i get, and i don't know what to do to access what i want : Cannot read property 'node' of undefined.这是我得到的错误,我不知道如何访问我想要的内容:无法读取未定义的属性“节点”。

your problem arises from reading the data before it is loaded.您的问题来自在加载之前读取数据。

depending on your js settings you should be able to use one of the following:根据您的 js 设置,您应该能够使用以下选项之一:

<span>{{ post?.author.node.name }}</span>

or <span>{{ post ? post.author.node.name : '' }}</span><span>{{ post ? post.author.node.name : '' }}</span> <span>{{ post ? post.author.node.name : '' }}</span>


according to the Vue Apollo documentation it could also be a problem with the duplication of the query根据 Vue Apollo 文档,它也可能是查询重复的问题

<script>
import { myQuery } from '~/graphql/posts'

export default {
  data() {
    return {
      posts: [], // initialization
    }
  },

  apollo: {
    posts: {
      prefetch: false, // to prevent SSR
      query: myQuery,
      update: data => {
        console.log('overwrite posts with new data', data.posts)
        return data.posts
      }
    },
  }
}
</script>

further as there seem to be cases in which the author has more than one entry (perhaps co authors?) I would try to update the author rendering to the following:此外,似乎有些情况下作者有多个条目(也许是共同作者?),我会尝试将作者渲染更新为以下内容:

<template>
    <section class="featured-projects">
      <div class="featured-projects__wrapper">
        <article v-for="post in posts" :key="post.id">
          <p>{{ post.id }}</p>
          <div v-if="Array.isArray(post.author)">
            first author is: {{ post.author[0].node.name }}
          </div>
          <div v-else-if="post.author">
           author is: {{ post.author.node.name }}
          </div>
          <div v-else="post.author">
           no author
          </div>
        </article>
      </div>
    </section>
  </section>
</template>

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

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