如何让GraphQL知道我是否要解析另一个rootQuery的字段?

让我解释一下:

type SecondType {
  id: String
}

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

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

这个想法是,如果firstTypesecondType之间存在父子关系,例如secondType包含它的父(firstType)id,结果是查询secondTypes中的先前解析器firstTypes可以获取与其父secondTypes相关的firstType。我所经历的是firstType的解析器正在从secondTypes的结果中寻找一个名为fetchFirstTypes的密钥。但是我想让GraphQL知道它需要从secondTypes的解析器解析Query.secondTypes。我怎样才能做到这一点?也许有一种方法可以写出一个字段的“后备还原器”?因此,如果没有从结果中找到键,它可以寻找解析器?

hebeipl 回答:如何让GraphQL知道我是否要解析另一个rootQuery的字段?

根据我对您问题的理解,您需要一个用于FirstType中存在的键secondTypes的解析器。为了这, 您可以按如下方式制作解析器:

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
      }
  }
};
本文链接:https://www.f2er.com/3167203.html

大家都在问