【问题标题】:GraphQL use field value as variable for another queryGraphQL 使用字段值作为另一个查询的变量
【发布时间】:2017-12-27 18:38:03
【问题描述】:

我正在查询同一个组件中都需要的 2 个对象。问题是其中一个查询必须等待另一个查询并使用其id 字段作为另一个查询的参数。不知道如何实现。

const PlayerQuery = gql`query PlayerQuery($trackId: Int!, $duration: Int!, $language: String!) {
  subtitle(trackId: $trackId, duration: $duration) {
    id,
    lines {
      text
      time
    }
  }
  translation(trackId: $trackId, language: $language, subtitleId: ???) {
    lines {
      translation
      original
    }
  }
}`;

所以在上面的查询中translation 需要subtitleId 作为subtitle 查询返回的参数。
我在客户端和服务器上都使用 Apollo。

【问题讨论】:

  • 我刚开始使用 GraphQL,但据我了解,这是不可能的。如果 translation 是从架构中的 subtitlelines 类型引用的,则可能,因为解析器会收到 subtitle 对象。

标签: javascript reactjs graphql apollo


【解决方案1】:

这是一个很好的问题,因为它说明了 REST/RPC 样式 API 和 GraphQL 之间的显着差异。在 REST 风格的 API 中,您返回的对象仅包含有关如何获取更多数据的元数据,并且 API 使用者应该知道如何在这些表上运行 JOIN。在您的示例中,您需要使用 ID 属性加入 subtitletranslation。在 GraphQL 中,对象很少单独存在,关系编码到架构本身中。

您没有发布您的schema,但从外观上看,您创建了一个translation 对象和一个subtitle 对象,并在您的根查询中公开了它们。我的猜测是它看起来像这样:

const Translation = new GraphQLObjectType({
  name: "Translation",
  fields: {
    id: { type: GraphQLInt },
    lines: { type: Lines }
  }
});

const SubTitle = new GraphQLObjectType({
  name: "SubTitle",
  fields: {
    lines: { type: Lines }
  }
});

const RootQuery = new GraphQLObjectType({
  name: "RootQuery",
  fields: {
    subtitle: { type: SubTitle },
    translation: { type: Translation }
  }
});

module.exports = new GraphQLSchema({
  query: RootQuery
});

您应该做的是与翻译INSIDE OF这样的字幕建立关系。 GraphQL 的目标是首先在您的数据中创建一个图表或关系,然后弄清楚如何向该数据公开入口点。 GraphQL 允许您在图中选择任意子树。

const Translation = new GraphQLObjectType({
  name: "Translation",
  fields: {
    id: { type: GraphQLInt },
    lines: { type: Lines }
  }
});

const SubTitle = new GraphQLObjectType({
  name: "SubTitle",
  fields: {
    lines: { type: Lines }
    translations: {
      type: Translation,
      resolve: () => {
        // Inside this resolver you should have access to the id you need
        return { /*...*/ }
      }
    }
  }
});

const RootQuery = new GraphQLObjectType({
  name: "RootQuery",
  fields: {
    subtitle: { type: SubTitle }
  }
});

module.exports = new GraphQLSchema({
  query: RootQuery
});

注意:为清楚起见,我省略了参数字段和任何其他解析器。我相信你的代码会更复杂一点,我只是想说明这一点:)。

【讨论】:

  • 感谢您确认我的怀疑,并提供正确的答案:)
  • 如果您不介意,您可以单击复选标记以确认这是正确答案吗?你刚刚投了赞成票(我也很感激):)。
  • 我不能,因为我不是问这个问题的人 :)
  • @Baer 是的,我在想这是实现它的唯一方法,毕竟它是有道理的。我试试看!
  • 我也在寻找同样的问题,如果我们无权访问模式来修改它怎么办?我正在寻找类似查询结果的内容作为另一个查询的输入...
猜你喜欢
  • 2019-07-29
  • 1970-01-01
  • 2020-12-15
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 2016-04-11
  • 2018-12-21
  • 2017-10-24
相关资源
最近更新 更多