GraphQL: Accessing another resolver/field output from a sibling resolver
Asked Answered
C

3

10

need some help. Let say Im requesting the following data:

{
  parent {
    obj1    {
        value1
    }
    obj2    {
        value2
    }
  }
}

And I need the result of value2 in value1 resolver for calculation.

Thought of returning a promise in in value2 and somehow take it in value1 resolver, but what if value2 resolver didn’t run yet?

There`s any way it could be done?

City answered 11/1, 2017 at 17:30 Comment(3)
what do you need it for? It maybe an issue with the design of your API.Lethargy
I have a large schema that fetches data from a neo4j database. for each entity (let say User, Item/Product) I made a cypher query block. in each entity there are some calculated fields based on the data. for example I have ‘User’ entity, he’s resolver gets raw data from db and calculate ‘rank’ by he’s address, age and other parameters. Now I have the item/product entity that also has a calculated field - ‘price’, that uses the user rank to calculate the final price.City
Well, if something belongs to a User, from DB or computed, it should be a property on User, not it's sibling. You can always get the resolved value of the parent in inner resolves.Lethargy
P
7

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 :)

Palliasse answered 12/1, 2017 at 1:4 Comment(3)
Thanks, I like the idea... If I'm going that path I will still need to solve a case when the query will not have value2, I can call the resolver but I need somehow the invoke all the parent resolvers. Also made me think what people do to make sure some resolver promise in no hanging the response from graphql. Thanks again I will give it a tryCity
And how do you set cacheEmitter on the context? E.g. in express-graphql, the context is already set to the request object.Concerto
@mpais how i can set the cacehEmitter? is it per node?Outwardbound
T
5

According to graphql-resolvers: "GraphQL currently does not support a field to depend on the resolved result of another field". Using an EventEmiter seems like a very off-spec way of achieving this. graphql-tools offers helper functions that allow you to compose resolvers in a number of ways that should help you.

Tichonn answered 11/12, 2018 at 14:56 Comment(1)
Could you pls suggest any example to achieve the same using graphql-toolsProliferate
K
0

You can load value2 of obj2 in your obj1 resolver again, if you are caching things that should not pose any performance issues.

Kellby answered 4/3, 2021 at 1:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.