I’m using resolver chaining to fetch book specific data from different APIs and return as an array of books. In a child resolver, I want to use an API to get the data.
Example schema:
const typeDefs = gql`
type Review {
id: String!
rating: String!
comments: [String]
}
type Book {
id: String!
name: String!
review: Review
}
type Query {
getBooks: [Book]
}
`;
Resolvers:
{
Query: {
getBooks: () => {
return [{
id: '1',
name: 'Abc'
}, {
id: '2',
name: 'Def'
}];
},
},
Book: {
review: async () => {
// Make API call to get the review data.
return {
id: '1',
rating: '4',
comments: ['Great book', 'A decent read.']
};
}
},
}
If the result contains 10 books, the review API will be called 10 times to get the corresponding data. This is something I’m trying to avoid as the API supports receiving a list of IDs in the request and returning a list of review objects back.
Is there a way to make resolver call the API only once to get the data for all of the books? We are going to use Apollo Federation, and the above logic will be part of a subgraph.
Any help or pointing in the right direction will be appreciated.
Thanks.