繁体   English   中英

GraphQL使用字段值作为另一个查询的变量

[英]GraphQL use field value as variable for another query

我正在查询同一组件中需要的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。

这是一个很好的问题,因为它说明了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
});

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM