apollographql / router

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

Callbacks before / after field resolution to override default implementation #2661

Open yanns opened 1 year ago

yanns commented 1 year ago

Is your feature request related to a problem? Please describe. It's complicated in plugins to override the default implementation of the resolution of each field.

Example 1: checking permissions

Let's consider the supergraph SDL:

type Query {
  products: [Product!]! @scope(name: "view_products")
  orders: [Order!]! @scope(name: "view_orders")
}

A OAuth plugin could:

Example 2: feature toggles

Let's consider the supergraph SDL:

type Query {
    reviews: [Review!]!
    users: [User!]! @feature(name: "review-user")
}
type User @feature(name: "review-user") {
    name: String!
}

The field users (and the type User) is only enabled for customers that have access to the feature tag review-user.

A feature toggle plugin could:

We have implemented such a middleware in Scala with sangria, and it's working great for us.

Describe the solution you'd like

Additional context

smyrick commented 8 months ago

This is now supported in the Router with the new directives

https://www.apollographql.com/docs/router/configuration/authorization

type Query {
  product: Product @authenticated
}

type Product @key(fields: "id") {
  id: ID! @policy(policy: [["feature-a"]])
  inStock: Boolean! @requiresScopes(scopes: [["read:product"]])
}