I have this resolvers:
const resolvers = {
Query: {
getCurrencies: async (parent, args, context, info) => {
//complex operation
//const currencies = ...
return currencies;
},
getUser: async (parent, args, context, info) => {
const currencies = await resolvers.Query.getCurrencies(parent, args, context, info);
// some code to get User
//const user = ...
return {
...user,
currency: currencies.find(c => c.id === user.currencyId)
}
},
getCountry: async (parent, args, context, info) => {
const currencies = await resolvers.Query.getCurrencies(parent, args, context, info);
// some code to get Country
// const country = ...
return {
...country,
currency: currencies.find(c => c.id === country.currencyId)
}
}
}
}
After in Apollo client I’m using both:
const user = useQuery(GET_USER)
const country = useQuery(GET_COUNTRY)
It’s possible to cache in some way getCurrencies?
A logical option would be pass currencies via client variables:
const currencies = useQuery(GET_USER)
const user = useQuery(GET_USER, {
variables: currencies
})
const country = useQuery(GET_COUNTRY,{
variables: currencies
})
But that would add a lot of complexity to my code…