We are implementing GraphQL service, which stands in front of several backend microservices.
For example, we have a Product
and each product has a list of history orders. Our backend server provides two REST APIs, one for product detail data, the other returns the history order list of a product.
Our client app has two pages: one is the product detail page, the other is the history order list of a product.
So, in the product detail page, we can only retrieve the detail data of the product, while in the order list page, we only need the list data.
The GraphQL schema like below:
type ProductOrder {
createAt: Date!
userName: String!
count: Int
}
type Product {
productId: ID
name: String
orders: [ProductOrder!]!
}
Query {
product(productId: ID): Product
}
and resolvers are like this
const resolvers = {
Query: {
product(_, { productId}){
// fetch detail data from backend API
return await someService.getProductDetail(productId);
}
},
Product: {
orders(product){
// fetch order list from another API
return await someService.getProductOrders(product.productId);
}
}
};
But we find a potential over-request using the above code.
When we request the order list data from the order list page, we have to request the product detail API first, after then we can request the order list API. But we ONLY need the order list data, no product data at all. In this case, we think the product detail request is useless, how can we eliminate this request?
It could be better if we can send only one request to retrieve the order list data.
Type
with too many fields. – Remission