Gergő Horváth
Gergő Horváth

Reputation: 3705

How to let GraphQL know if I want to resolve a field from another rootQuery?

Let me explain:

type SecondType {
  id: String
}

type FirstType {
  id: String
  secondTypes: [SecondType]
}

type Query {
  firstTypes: [FirstType]
  secondTypes: [SecondType]
}
//
const resolver = {
  Query: {
    firstTypes: fetchFirstTypes,
    secondTypes: fetchSecondTypes
  }
};

The idea is that if there's a parent-child relationship between firstType and secondType, for example secondType contains it's parent (firstType) id, through the result of the previous resolver, secondTypes inside query firstTypes can fetch secondTypes related to it's parent firstType. What I've experienced is that the resolver of firstType is looking for a key named secondTypes from the result of fetchFirstTypes. But I want to let GraphQL know that it needs to resolve secondTypes from the resolver of Query.secondTypes. How can I do that? Maybe is there a way to write "fallback reducers" of a field? So if it doesn't find a key from the result, it can look for a resolver?

Upvotes: 0

Views: 277

Answers (1)

Indragith
Indragith

Reputation: 1300

Based on my understanding of your question, you need a resolver for the key secondTypes which is present in FirstType. For this, you can make the resolver as follows:

const resolver = {
  Query: {
    firstTypes: fetchFirstTypes,
    secondTypes: fetchSecondTypes
  },
  FirstType: {
      secondTypes: (parent, args, context, info) => {
          // Here you will get the parent id(FirstType) parent.id
          // From Here resolve the secondtypes data
      }
  }
};

Upvotes: 1

Related Questions