Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested query arguments based on parent query result #95

Open
Paddy-Hamilton opened this issue Mar 23, 2018 · 7 comments
Open

Nested query arguments based on parent query result #95

Paddy-Hamilton opened this issue Mar 23, 2018 · 7 comments

Comments

@Paddy-Hamilton
Copy link

Paddy-Hamilton commented Mar 23, 2018

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?

@fbartho
Copy link
Collaborator

fbartho commented Mar 25, 2018

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.

@fbartho
Copy link
Collaborator

fbartho commented Mar 25, 2018

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

@nsams
Copy link

nsams commented Mar 26, 2018

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

@Paddy-Hamilton
Copy link
Author

Paddy-Hamilton commented Mar 26, 2018

@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.

@Paddy-Hamilton
Copy link
Author

@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?

@fbartho
Copy link
Collaborator

fbartho commented Apr 25, 2018

@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.

@nsams
Copy link

nsams commented Feb 6, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants