Skip to content

Commit

Permalink
feat: support subscriptions for nestjs federation (#2966)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasily-polonsky authored Aug 22, 2023
1 parent fd83f18 commit 5740159
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-dancers-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-yoga/nestjs-federation': minor
---

support subscriptions for nestjs federation
32 changes: 28 additions & 4 deletions packages/nestjs-federation/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
} from '@graphql-yoga/nestjs';
import { useApolloInlineTrace } from '@graphql-yoga/plugin-apollo-inline-trace';
import { Injectable, Type } from '@nestjs/common';
import { GraphQLFederationFactory } from '@nestjs/graphql';
import {
GqlSubscriptionService,
GraphQLFederationFactory,
SubscriptionConfig,
} from '@nestjs/graphql';

export type YogaFederationDriverConfig<Platform extends YogaDriverPlatform = 'express'> =
YogaDriverConfig<Platform>;
Expand All @@ -19,6 +23,8 @@ export type YogaFederationDriverConfig<Platform extends YogaDriverPlatform = 'ex
export class YogaFederationDriver<
Platform extends YogaDriverPlatform = 'express',
> extends AbstractYogaDriver<Platform> {
private subscriptionService?: GqlSubscriptionService;

constructor(private readonly graphqlFederationFactory: GraphQLFederationFactory) {
super();
}
Expand All @@ -40,11 +46,29 @@ export class YogaFederationDriver<
plugins: [...(options?.plugins || []), useApolloInlineTrace()],
});

if (options.subscriptions) {
// See more: https://github.com/apollographql/apollo-server/issues/2776
throw new Error('No support for subscriptions when using Apollo Federation');
if (options.subscriptions && options.schema) {
const config: SubscriptionConfig =
options.subscriptions === true
? {
'graphql-ws': true,
}
: options.subscriptions;

this.subscriptionService = new GqlSubscriptionService(
{
schema: options.schema,
path: options.path,
context: options.context,
...config,
},
this.httpAdapterHost.httpAdapter?.getHttpServer(),
);
}
}

public async stop(): Promise<void> {
await this.subscriptionService?.stop();
}
}

export interface YogaGatewayDriverConfig<Platform extends YogaDriverPlatform = 'express'> {
Expand Down

0 comments on commit 5740159

Please sign in to comment.