【问题标题】:Query variables not being passed down from vue component in apollo查询变量未从 apollo 中的 vue 组件传递
【发布时间】:2020-02-28 10:43:50
【问题描述】:

我有一个简单的查询,它接受一个 ID 参数,但它不起作用。它说 "TypeError: Cannot read property 'taskId' of undefined" 。所以我认为它由于某种原因无法识别“this”关键字。

请看一下:

来自前端组件的 Apollo 查询:

    getCommentsByTask: {
      query: GET_COMMENTS_BY_TASK,
      variables: {
        taskId: this.taskId
      },
      result({ data }) {
        this.getComments = data;
        console.log("data", data);
      }
    }

在前端定义查询:

query GET_COMMENTS_BY_TASK($taskId: ID!) {
  getCommentsByTask(taskId: $taskId) {
    id
    parentId
    ownerId
    text
  }
}

服务器中的解析器:

async getCommentsByTask (_, {taskId}, context) {
      const userId = getUserId(context)
      const user = await User.findById(userId)

      if (!user) return

      const comments = await Comment.findById(taskId)

      return comments
    }

架构:

type Query {
  getCommentsByTask(taskId: ID!): [Comment]
}

【问题讨论】:

    标签: graphql apollo-client vue-apollo


    【解决方案1】:

    假设这是一个智能查询,variables 应该是一个(常规,非箭头)函数,如果您需要访问它。

    【讨论】:

      猜你喜欢
      • 2018-06-27
      • 2019-03-21
      • 2019-01-26
      • 2018-01-27
      • 2020-11-12
      • 2023-03-15
      • 2018-09-20
      • 2017-04-22
      • 2019-01-25
      相关资源
      最近更新 更多