My immediate thought is that you could use the context to achieve something like this. I'd imagine you could have a cache like object mixed with an event emitter to solve the race condition issue.
For example, assume we had some class
class CacheEmitter extends EventEmitter {
constructor() {
super();
this.cache = {};
}
get(key) {
return new Promise((resolve, reject) => {
// If value2 resolver already ran.
if (this.cache[key]) {
return resolve(this.cache[key]);
}
// If value2 resolver has not already run.
this.on(key, (val) => {
resolve(val);
});
})
}
put(key, value) {
this.cache[key] = value;
this.emit(key, value);
}
}
Then from your resolvers you could do something like this.
value1Resolver: (parent, args, context, info) => {
return context.cacheEmitter.get('value2').then(val2 => {
doSomethingWithValue2();
});
}
value2Resolver: (parent, args, context, info) => {
return doSomethingToFetch().then(val => {
context.cacheEmitter.put('value2', val);
return val;
}
}
I haven't tried it but that seems like it may work to me! If you give it a shot, I'm curious so let me know if it works. Just for book keeping you would need to make sure you instantiate the 'CacheEmitter' class and feed it into the GraphQL context at the top level.
Hope this helps :)
User
, from DB or computed, it should be a property onUser
, not it's sibling. You can always get the resolved value of the parent in inner resolves. – Lethargy