It's common and very useful to offer two kind of queries for every type you have:
a query to fetch a single node with an id
or other unique fields, that's in your case Product
(you already have this).
a query to fetch many nodes depending on different filter conditions, let's call it allProducts
.
Then you have two options to fetch multiple products in one query.
First, you can use the Product
query multiple times and use GraphQL Aliases to avoid a name clash in the response data:
query ProductInCartQuery($firstId: ID!, $secondId: ID!){
firstProduct: Product(id: $firstId) {
id
... ProductInfo
}
secondProduct: Product(id: $secondId) {
id
... ProductInfo
}
fragment ProductInfo on Product {
name
price
}
}
You could build this query string dynamically depending on the ids you want to query. However, it's probably best to use the allProducts
query with the necessary filter setup if the number of differents ids is dynamic:
query filteredProducts($ids: [ID!]!) {
allProducts(filter: {
id_in: $ids
}) {
... ProductInfo
}
}
fragment ProductInfo on Product {
name
price
}
You can try it out yourself in this GraphQL Playground I prepared for you. More background information can be found in this article.