apollographql / apollographql/apollo-server

Plugin method types shouldn't force users to return a promise

Open
#8,146 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
14k
Forks
2k
Avg merge
2d 14h
Merged PRs (30d)
2

Description

Back in v3, most plugin methods (`requestDidStart` etc.) were unified to always return a promise. This means that instead of
```ts
class LoggingPlugin implements ApolloServerPlugin {
requestDidStart() {
const start = hrtime.bigint()
return {
willSendResponse () {
console.log('completed in', (hrtime.bigint() - start) / BigInt(1000000), 'ms')
}
}
}
}
```
users have to do
```ts
class LoggingPlugin implements ApolloServerPlugin {
async requestDidStart() {
const start = hrtime.bigint()
return {
async willSendResponse () {
console.log('completed in', (hrtime.bigint() - start) / BigInt(1000000), 'ms')
}
}
}
}
```

This has the following disadvantages:
1. In a larger example, using `async` for code that will never `await` anything can be confusing. "Why does this code need to be async, is there a bug, has it changed?"
2. It probably has a performance impact.
3. You can write plugins without `async` in JavaScript and they'll work fine, which makes porting them to TypeScript confusing.
4. If JavaScript plugins work fine with non-promise returns but the TS types don't reflect this, there's a risk of breaking existing plugins if future behaviour comes to rely on the return type being a promise.

I promise I actually wrote this feature request and it's not some LLM slop. I just finished porting some of our plugins to TS at work, and I found this API confusing. It's not a huge deal but it would make the ergonomics of the library nicer.

Contributor guide

Open the contributing guide

Research direction

Start by locating the ApolloServerPlugin type definitions and the requestDidStart and willSendResponse entry points. Update the types so synchronous returns are accepted, then verify that the synchronous LoggingPlugin example type-checks without requiring async; the issue does not name a specific test or file to run.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, developer-experience
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.