It is generally accepted and very useful to offer two types of queries for each type that you have:
a request to select one node with id or other unique fields that are Product in your case (you already have this).
a request to retrieve many nodes depending on different filter conditions, call him allProducts .
Then you have two options for extracting multiple products in one query.
First, you can use the Product query several times and use GraphQL Aliases to avoid name collisions 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 can build this query string dynamically depending on the identifiers you want to query. However, it is best to use the allProducts query with the necessary filter setting if the number of differents identifiers is dynamic:
query filteredProducts($ids: [ID!]!) { allProducts(filter: { id_in: $ids }) { ... ProductInfo } } fragment ProductInfo on Product { name price }
You can try yourself in this GraphQL playground. I am prepared for you. More information can be found in this article .
source share