graphile / graphile/crystal

Support Route level hooks for grafserv/fastify/v4

Open
#1,999 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
12.9k
Forks
625
Avg merge
5h 23m
Merged PRs (30d)
24

Description

### Feature description

In the [fastify grafserv server](https://github.com/graphile/crystal/blob/main/grafast/grafserv/src/servers/fastify/v4/index.ts), there are several calls to `app.route()` to set up the relevant routes with fastify. I think it would be useful to be able to register fastify hooks to individual routes.

To do this, I think you'd need some server-specific settings in Grafserv options? I'm not sure if that's already a pattern somewhere, or whether that adds coupling where we want grafserv and the specific servers to remain decoupled.

I'm thinking the grafserv config would include something like

```ts
{
// ... other grafserv config properties
fastifyV4: {
routeHooks: {
'/graphql': {
preHandler: () => {}
}
}
}
}
```

### Motivating example

I'm trying to find the cleanest way to add a hook to my postgraphile + fastify application that validates the access token present in request cookies and returns a 401 before the request starts executing grafast/postgraphile-related things.

Because grafserv's calls to app.route() are inaccessible to my code, I think my best option without this feature request would be to create a server-wide hook and check the path within the hook to determine whether the call is to /graphql or some other route. If it's a call to /graphql, then I do the validation.

### Supporting development

I:

- [ ] am interested in building this feature myself
- [x] am interested in collaborating on building this feature
- [x] am willing to help testing this feature before it's released
- [x] am willing to write a test-driven test suite for this feature (before it exists)
- [x] am a [Graphile sponsor](https://www.graphile.org/sponsor/) ❤️
- [ ] have an active [support or consultancy contract](https://www.graphile.org/support/) with Graphile

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.