I defined a type that relies on two different API endpoints. Let's call them A and B. For the query getObjects I want to return an array with objects of type SomeType
type SomeType{
id: String // this comes from A
url: String // this comes from B
}
type Query{
getObjects: [SomeType]
}
I have a resolver for getObjects that retrieves data from endpoint A and feeds in SomeType to return an array. But I need to resolve the url separately, because the url comes from a endpoint B, right? Thus I write a resolver for the field url. But I don't want to make a request inside of resolver for url as it would mean that I make a request for each object from endpoint A (in my case ~ 18k). endpoint B returns an object with key-value pairs. The key is the id I get from endpoint A, and the value has the url.
const resolvers = {
SomeType: {
url: (parent, {}, context) => {
console.log(parent.id); // parent.id is needed to index the
return someurl;
}
},
Query: {
getObjects: async () => {
const mainResponse = await fetch("endpoint A");
const responseForLogo = await fetch("endpoint B")
const mainData = await mainResponse.json();
const secondaryData = await responseForLogo.json();
return mainData['data'];
},
}
}
But how can I efficiently get the Url for each object? One way I imagined was to make the request inside the getObjects resolver and store the response object and access it in the resolver for url.
Hope someone can help me with that.