GraphQL 查询错误——变量从未在操作中使用

GraphQL query error -- variable is never used in operation

我正在从 Apollo Server/Express 后端为个人 post 执行请求。

在 Apollo GraphQL 沙箱中,查询有效并检索到正确的 post,但是,查询有一个红色波浪线标识错误,显示为 -

Variable "$getPostId" is never used in operation "Query".

查询如下-

query Query($getPostId: ID!) {
  getPost(id:"20c9b3ac-afe6-4faa-a3f9-e00ef1b38ccf") {
    title
    author
    id
  }
}

架构如下-

module.exports = gql`
  type Post {
    id: ID!
    title: String!
    author: String!
  }
  type Query {
    getPosts: [Post]!
    getPost(id: ID!): Post
  }
  ...
`

最接近的 post 似乎解决了我能找到的类似问题是 。但是,我无法将解决方案转化为我的问题。

为什么会显示错误(尤其是当查询成功运行时)?需要做什么才能阻止错误显示?

非常感谢!

听起来像

query Query($getPostId: ID!) {
  getPost(id:"20c9b3ac-afe6-4faa-a3f9-e00ef1b38ccf") {
    title
    author
    id
  }
}

应该是

query Query($getPostId: ID!) {
  getPost(id: $getPostId) {
    title
    author
    id
  }
}

或者,如果您的查询实际上是为了对 ID 进行硬编码,那么您需要


query Query {
  getPost(id:"20c9b3ac-afe6-4faa-a3f9-e00ef1b38ccf") {
    title
    author
    id
  }
}