Search code examples
vue.jsgraphqlstrapivue-apollo

GraphQL query - Query by ID


I have installed the strapi-starter-blog locally and I'm trying to understand how I can query article by ID (or slug). When I open the GraphQL Playground, I can get all the article using:

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

The response is:

{
  "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"
        }
      }
    ]
  }
}

But when I try to get the article using the ID with variable, like here github code in the GraphQL Playground with the following

Query:

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

Variables:

{
  "id": 1
}

I get an error:

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

What is the difference and why can't I get the data like in the example of the Github repo.

Thanks for your help.


Solution

  • It's the difference between articles and article as the query. If you use the singular one you can use the ID as argument