apollographql / router

A configurable, high-performance routing runtime for Apollo Federation 🚀
https://www.apollographql.com/docs/router/
Other
809 stars 272 forks source link

How to get description of schema in router plugins? #4349

Open bitterteasweetorange opened 11 months ago

bitterteasweetorange commented 11 months ago

schema.graphql:

type Query
  @join__type(graph: ACCOUNTS)
  @join__type(graph: INVENTORY)
  @join__type(graph: PRODUCTS)
  @join__type(graph: REVIEWS) {
  """
  desc: xxxxxx  
  """
  me: User @join__field(graph: ACCOUNTS)
}

plugin.rs

    let body = request.supergraph_request.body();

    if let Some(query) = &body.query {
        let doc = ExecutableDocument::parse(&schema, query, "query.graphql");         // get document
        let operation = doc
            .get_operation(Some(query))
            .expect("query does not exist");                                    // panic!
    }
goto-bus-stop commented 11 months ago

hi, I moved the issue to the router repository as it's more of a router usage question than an apollo-rs one :)

in the future plugins wil get access to the parsed schema, but not right now as the schema API isn't fully stable. You do get access to the supergraph schema source text, so you have to parse that to use it. The supergraph_sdl example in this repository shows how you can access the SDL in your plugin. https://github.com/apollographql/router/blob/f81203fa3e8d7efc98e124aca1e8944bc0c05a2d/examples/supergraph_sdl/src/supergraph_sdl.rs

That example is still using an older version of apollo-rs, with the 1.0 betas instead of using ApolloCompiler you'd do something like this:

fn supergraph_service(&self, service: supergraph::BoxService) -> supergraph::BoxService {
    let schema = apollo_compiler::Schema::parse_and_validate(self.supergraph_sdl).expect("probably replace this with error handling");
    ServiceBuilder::new().map_request(move |req| {
        let Some(query) = req.supergraph_request.body().query else { todo!() };
        ExecutableDocument::parse(&schema, &query, "query.graphql");
        todo!()
    })
}
garypen commented 10 months ago

Awaiting apollo-compiler 1.0 release.