graphql - 不变违规 : Expecting a parsed GraphQL document

标签 graphql nuxt.js apollo vue-apollo

尽管突变包含在 gql 标记中,但我还是收到以下错误:

Invariant Violation: Expecting a parsed GraphQL document. Perhaps you need to wrap the query string in a "gql" tag? http://docs.apollostack.com/apollo-client/core.html#gql

此问题仅由下面的突变代码引起,我有一个有效的查询。

代码:

<script>
import gql from 'graphql-tag'

export default {
  apollo: {
    createTask: {
      mutation: gql`
        mutation Task(
          $taskName: String!
          $taskDesc: String!
        ) {
          setSession(
            taskName: $taskName
            taskDesc: $taskDesc
          ) {
            id
            taskName
          }
        }
      `,
      variables() {
        return {
          taskName: this.res.data.task_name,
          taskDesc: this.res.data.task_description,
        }
      },
    },
  },
  data() {
    return {
      createTask: '',
    }
  },
}
<script>

最佳答案

Smart queries使用 apollo 对象声明并在组件安装时运行。如果您希望以这种方式运行查询,那么您的代码将如下所示:

export default {
  apollo: {
    someQuery: gql`...`,
  }
}

但是,在处理突变时,您通常不希望在挂载时运行它们。相反,您希望执行突变以响应某些用户操作,例如单击按钮。所以上面的语法不适用于突变。相反,您需要直接在您的方法之一内调用 this.$apollo.mutate:

export default {
  methods: {
    createTask() {
      this.$apollo.mutate({
        mutation: gql`...`,
        variables: {...},
        // other options
      }).then(result => {
        // do something with the result
      })
    }
  }
}

关于graphql - 不变违规 : Expecting a parsed GraphQL document,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62520976/

相关文章:

javascript - Nuxtjs错误渲染函数或模板未在组件中定义: carousel

graphql - 在 Graphql 中解析类型

reactjs - redux-form 值得在 apollo-react-redux 应用程序中使用吗?

node.js - graphql 解析器中的授权作为中间件

graphql - 将变量传递给 `mutation` apollographql 服务器

ruby-on-rails - 如何在 graphql ruby​​ 中获取连接类型中的字段参数

javascript - 无法在 nuxt 中使用 axios 插件

vue.js - 什么触发了 "triggerScroll"事件?

graphql - GraphQL 的多语言支持

api - REST 与数据库作为 GraphQL API 数据源?