Apollo-link-rest: Nested query arguments based on parent query result

Created on 23 Mar 2018  ·  7Comments  ·  Source: apollographql/apollo-link-rest

I'm wondering if it is possible to perform nested queries, where the child query arguments are based on the parent result.
e.g based on the docs given example

const getLukeRestLink = gql`
  query luke {
    Luke @rest(type: "Person", path: "people/1/") {
      name
      films {
        film @rest(type: "Film", path:  filmUrl) { // the filmUrl is the url that is returned in the films field
          title
        }
      }
    }
  }
`;

Films is an array of urls.
Q Is it possible to pass in the url to the film query path argument?

question❔

Most helpful comment

This is doable @Paddy-Hamilton! Using your example code:

  query luke {
    Luke @rest(type: "Person", path: "people/1/") {
      name
      films {
        url @export(as: "url")
        film @rest(type: "Film", path: ":url") {
          title
        }
      }
    }
  }

The @export(as: …) directive lets you expose previously selected data for further nested rest-queries to use. Small complication: remember that each @rest directive is probably it's own network request, and so in this case you'll have N+1 requests where N is the number of films Luke is in.

All 7 comments

This is doable @Paddy-Hamilton! Using your example code:

  query luke {
    Luke @rest(type: "Person", path: "people/1/") {
      name
      films {
        url @export(as: "url")
        film @rest(type: "Film", path: ":url") {
          title
        }
      }
    }
  }

The @export(as: …) directive lets you expose previously selected data for further nested rest-queries to use. Small complication: remember that each @rest directive is probably it's own network request, and so in this case you'll have N+1 requests where N is the number of films Luke is in.

You may also find pathBuilder useful, documentation is in the tests/restLink.ts file for now.

Films is an array of urls, it is not an object containing an url property.

Could you modify this example?
https://codesandbox.io/s/yw2766yl4x

@fbartho thanks for the explanation and the bonus tip re network requests.

I think the need for using this nested request method in this example is due to the structure of the api, so I'd have to just accept multiple network requests if i wanted to get this data. But thanks, will be helpful in structuring my own database and api.

@fbartho

I have been looking for some more documentation around @export directive but have only really found this example, which @peggyrayzis mentions in this issue. Peggy also mentions that it's still in experimental phase and not officially released. I am looking to use it in a project I am working on, but would feel more comfortable if it was an official supported feature, what's the road map for @match, is it going to be released soon?

@Paddy-Hamilton we definitely support the @export(as: directive within nested @rest( queries. Mixed apollo-link-rest & apollo-link-state interactions is a really cool feature, but I think needs to be discussed at the apollo-link level. /cc @peggyrayzis do you have any insight here?

I'm unfamiliar for the @match directive so I'd love any pointers as to where that was discussed.

This issue is not solved for me, please take a look at https://codesandbox.io/s/6yv8y2z9v3

It will load a single url https://swapi.co/api/https://swapi.co/api/films/2/,https://swapi.co/api/films/6/,https://swapi.co/api/films/3/,https://swapi.co/api/films/1/,https://swapi.co/api/films/7/

problem is that films is an array. I don't think this can be solved by pathBuilder.

Was this page helpful?
0 / 5 - 0 ratings