GraphQL 查询 - 按 ID 查询
GraphQL query - Query by ID
我已经在本地安装了 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 获取文章时,就像这里 github code 在 GraphQL Playground 中使用以下
查询:
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 存储库示例中那样获取数据。
感谢您的帮助。
作为查询的articles
和article
的区别。如果你使用单数,你可以使用 ID 作为参数
我已经在本地安装了 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 获取文章时,就像这里 github code 在 GraphQL Playground 中使用以下
查询:
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 存储库示例中那样获取数据。
感谢您的帮助。
作为查询的articles
和article
的区别。如果你使用单数,你可以使用 ID 作为参数