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

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

Open
yanns opened this issue Feb 22, 2023 · 1 comment
Open

Comments

@yanns
Copy link
Contributor

yanns commented Feb 22, 2023

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:

  • (1) check which feature tags a request can use, and put them into the context
  • (2) implement such callbacks:
    • before each field, check if the field can be accessed or not.
    • before each field, if the query is like __type(name: "User"), override the resolution to return None if the query should not access the User type.
    • after each field, if it was an introspection result (like { __schema { queryType { name, fields { name } } } }), remove all types that should not be seen

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

Describe the solution you'd like

  • Having middleware that can add callbacks before and after each field resolution. Middlewares could then run some logic before or after a field is resolved to override the default implementation, to add errors, or to manipulate the result.
  • Plugins can register middlewares.

Additional context

@smyrick
Copy link
Member

smyrick commented Mar 14, 2024

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"]])
}

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

2 participants