【问题标题】:GraphQL query - Query by IDGraphQL 查询 - 按 ID 查询
【发布时间】:2020-11-16 16:57:54
【问题描述】:

我已经在本地安装了strapi-starter-blog,我正在尝试了解如何通过 ID(或 slug)查询文章。当我打开 GraphQL Playground 时,我可以使用以下方式获取所有文章:

query Articles {
  articles {
    id
    title
    content
    image {
      url
    }
    category {
      name
    }
  }
}

回复是:

{
  "data": {
    "articles": [
      {
        "id": "1",
        "title": "Thanks for giving this Starter a try!",
        "content": "\n# Thanks\n\nWe hope that this starter will make you want to discover Strapi in more details.\n\n## Features\n\n- 2 Content types: Article, Category\n- Permissions set to 'true' for article and category\n- 2 Created Articles\n- 3 Created categories\n- Responsive design using UIkit\n\n## Pages\n\n- \"/\" display every articles\n- \"/article/:id\" display one article\n- \"/category/:id\" display articles depending on the category",
        "image": {
          "url": "/uploads/blog_header_network_7858ad4701.jpg"
        },
        "category": {
          "name": "news"
        }
      },
      {
        "id": "2",
        "title": "Enjoy!",
        "content": "Have fun!",
        "image": {
          "url": "/uploads/blog_header_balloon_32675098cf.jpg"
        },
        "category": {
          "name": "trends"
        }
      }
    ]
  }
}

但是当我尝试使用带有变量的 ID 来获取文章时,例如在 GraphQL Playground 中的github code 中使用以下内容

查询:

query Articles($id: ID!) {
  articles(id: $id) {
    id
    title
    content
    image {
      url
    }
    category {
      name
    }
  }
}

变量:

{
  "id": 1
}

我收到一个错误:

...
"message": "Unknown argument \"id\" on field \"articles\" of type \"Query\"."
...

有什么区别,为什么我不能像 Github repo 的例子那样得到数据。

感谢您的帮助。

【问题讨论】:

  • 这是articlesarticle作为查询的区别。如果您使用单数,则可以使用 ID 作为参数
  • 很好,它正在工作。非常感谢!

标签: vue.js graphql strapi vue-apollo


【解决方案1】:

这是articlesarticle 之间的区别作为查询。如果你使用单数,你可以使用 ID 作为参数

【讨论】:

    猜你喜欢
    • 2018-06-13
    • 2019-10-30
    • 2021-12-17
    • 2021-08-14
    • 2019-06-27
    • 2020-05-01
    • 2020-03-31
    • 2018-10-31
    • 2018-02-10
    相关资源
    最近更新 更多