16

I'm querying for 2 objects which are both needed in the same component. The problem is that one of the queries have to wait on the other and use its id field as an argument for the other. Not sure how to implement this.

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
    }
  }
}`;

So in the query above translation needs subtitleId as an argument which is returned by the subtitle query.
I'm using Apollo both on the client and on the server.

Nick Dima
  • 1,587
  • 2
  • 18
  • 36
  • I only just started with GraphQL, but from what I understand, that's not possible. If `translation` was referenced from the `subtitle` or `lines` types in the schema, it probably _would_ be possible, because the resolver would receive the `subtitle` object. – robertklep Jul 21 '17 at 16:37

1 Answers1

17

That's a great question because it illustrates a significant difference between REST/RPC style APIs and GraphQL. In REST style APIs the objects that you return only contain metadata about how to fetch more data, and the API consumer is expected to know how to run the JOINs over those tables. In your example, you have a subtitle and a translation that you need to JOIN using the ID property. In GraphQL, objects rarely exists in isolation and the relationships encoded into the schema itself.

You didn't post your schema but from the looks of it, you created a translation object and a subtitle object and exposed them both in your root query. My guess is that it looks something like this:

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
});

What you should do instead, is to make a relationship to translations INSIDE OF subtitle like this. The goal of GraphQL is to first create a graph or relationships in your data, then to figure out how to expose entry points to that data. GraphQL lets you select arbitrary sub-trees in a graph.

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
});

Note: For clarity, I left out the arguments fields and any additional resolvers. I'm sure your code will be a bit more sophisticated, I just wanted to illustrate the point :).

Baer
  • 3,690
  • 25
  • 24
  • Thanks for confirming my suspicion, and providing a proper answer :) – robertklep Jul 21 '17 at 18:00
  • If you wouldn't mind, can you click the checkmark to confirm that this is a correct answer? You just upvoted it (which I also appreciate) :). – Baer Jul 21 '17 at 19:03
  • I can't as I wasn't the one asking the question :) – robertklep Jul 21 '17 at 20:10
  • 1
    @Baer yeah, I was thinking that this is the only way to pull it off, and after all it makes sense. I'll give it a try! – Nick Dima Jul 21 '17 at 21:56
  • 7
    I was looking for this same question, what if we don't have access to the schema to modify it? I'm looking for something like result of query as input to another query... – Eladian Aug 30 '18 at 23:09