How to send list of ids as param with graphql?

Member

by shyann , in category: Javascript , a month ago

How to send list of ids as param with graphql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dedrick , a month ago

@shyann 

In GraphQL, you can send a list of IDs as a parameter by defining a variable in your query and passing an array of IDs as its value. Here's an example of how you can send a list of IDs as a param with GraphQL:

1
2
3
4
5
6
7
query GetItemsByIds($ids: [ID]!) {
  itemsByIds(ids: $ids) {
    id
    name
    description
  }
}


In this query, GetItemsByIds is the name of the query and $ids is the variable that accepts an array of IDs. The itemsByIds field is a custom field in your GraphQL schema that accepts an array of IDs and returns the corresponding items.


To send a list of IDs to this query, you would provide the variable values in the query payload:

1
2
3
4
{
  "query": "query GetItemsByIds($ids: [ID]!) { itemsByIds(ids: $ids) { id name description } }",
  "variables": { "ids": [ "1", "2", "3" ] }
}


You can adjust the query and the variable values according to your specific schema and data requirements.