Skip to content

Commit

Permalink
Implement callback subscription protocol in new plugin `ApolloServerP…
Browse files Browse the repository at this point in the history
…luginSubscriptionCallback` (apollographql#7617)

This PR creates a new plugin for Apollo Server which implements the new
[subscription callback
protocol](https://www.apollographql.com/docs/router/executing-operations/subscription-callback-protocol/).

While the plugin approach works, it does have a couple notable pitfalls.
I see this as a good first step towards enabling people to use it today.
The nature of this implementation is to skip execution in Apollo Server
altogether, which means the execution internals are bypassed. This means
that some plugin hooks are skipped - notably `executionDidStart/End` and
`willResolveField` (for field-level instrumentation) when resolving
these types of operations.

Fortunately, most of the logic is captured in the `SubscriptionManager`
class. The plugin mostly just wraps it, and Apollo Server should be able
to integrate it when we choose to.
  • Loading branch information
trevor-scheer authored Jul 27, 2023
1 parent 98eb29e commit 4ff81ca
Show file tree
Hide file tree
Showing 7 changed files with 2,602 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .changeset/cuddly-lemons-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@apollo/server': minor
---

Introduce new `ApolloServerPluginSubscriptionCallback` plugin. This plugin implements the [subscription callback protocol](https://github.com/apollographql/router/blob/dev/dev-docs/callback_protocol.md) which is used by Apollo Router. This feature implements subscriptions over HTTP via a callback URL which Apollo Router registers with Apollo Server. This feature is currently in preview and is subject to change.

You can enable callback subscriptions like so:
```ts
import { ApolloServerPluginSubscriptionCallback } from '@apollo/server/plugin/subscriptionCallback';
import { ApolloServer } from '@apollo/server';

const server = new ApolloServer({
// ...
plugins: [
ApolloServerPluginSubscriptionCallback(),
],
});
```

Note that there is currently no tracing or metrics mechanism in place for callback subscriptions. Additionally, this plugin "intercepts" callback subscription requests and bypasses some of Apollo Server's internals. The result of this is that certain plugin hooks (notably `executionDidStart` and `willResolveField`) will not be called when handling callback subscription requests or when sending subscription events.

For more information on the subscription callback protocol, visit the docs:
https://www.apollographql.com/docs/router/executing-operations/subscription-callback-protocol/
34 changes: 34 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"eslint-plugin-import": "2.27.5",
"express": "4.18.2",
"graphql": "16.7.1",
"graphql-subscriptions": "2.0.0",
"graphql-tag": "2.12.6",
"jest": "29.6.1",
"jest-config": "29.6.1",
Expand Down
8 changes: 8 additions & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@
"import": "./dist/esm/plugin/schemaReporting/index.js",
"require": "./dist/cjs/plugin/schemaReporting/index.js"
},
"./plugin/subscriptionCallback": {
"types": {
"require": "./dist/cjs/plugin/subscriptionCallback/index.d.ts",
"default": "./dist/esm/plugin/subscriptionCallback/index.d.ts"
},
"import": "./dist/esm/plugin/subscriptionCallback/index.js",
"require": "./dist/cjs/plugin/subscriptionCallback/index.js"
},
"./plugin/usageReporting": {
"types": {
"require": "./dist/cjs/plugin/usageReporting/index.d.ts",
Expand Down
8 changes: 8 additions & 0 deletions packages/server/plugin/subscriptionCallback/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@apollo/server/plugin/subscriptionCallback",
"type": "module",
"main": "../../dist/cjs/plugin/subscriptionCallback/index.js",
"module": "../../dist/esm/plugin/subscriptionCallback/index.js",
"types": "../../dist/esm/plugin/subscriptionCallback/index.d.ts",
"sideEffects": false
}
Loading

0 comments on commit 4ff81ca

Please sign in to comment.