@graphql-yoga/nestjs-federation
Version:
GraphQL Yoga driver with Apollo Federation for NestJS GraphQL.
76 lines (75 loc) • 2.9 kB
JavaScript
import { __decorate, __metadata } from "tslib";
import { GraphQLSchema } from 'graphql';
import { ApolloGateway } from '@apollo/gateway';
import { printSubgraphSchema } from '@apollo/subgraph';
import { useApolloFederation } from '@envelop/apollo-federation';
import { AbstractYogaDriver, } from '@graphql-yoga/nestjs';
import { useApolloInlineTrace } from '@graphql-yoga/plugin-apollo-inline-trace';
import { Injectable } from '@nestjs/common';
import { GqlSubscriptionService, GraphQLFederationFactory, } from '@nestjs/graphql';
export let YogaFederationDriver = class YogaFederationDriver extends AbstractYogaDriver {
graphqlFederationFactory;
subscriptionService;
constructor(graphqlFederationFactory) {
super();
this.graphqlFederationFactory = graphqlFederationFactory;
}
async generateSchema(options) {
return await this.graphqlFederationFactory.generateSchema(options);
}
async start(options) {
if (options.definitions?.path) {
if (!options.schema) {
throw new Error('Schema is required when providing definitions path');
}
await this.graphQlFactory.generateDefinitions(printSubgraphSchema(options.schema), options);
}
await super.start({
...options,
plugins: [...(options?.plugins || []), useApolloInlineTrace()],
});
if (options.subscriptions && options.schema) {
const config = 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());
}
}
async stop() {
await this.subscriptionService?.stop();
}
};
YogaFederationDriver = __decorate([
Injectable(),
__metadata("design:paramtypes", [GraphQLFederationFactory])
], YogaFederationDriver);
export let YogaGatewayDriver = class YogaGatewayDriver extends AbstractYogaDriver {
async generateSchema(_options) {
return new GraphQLSchema({});
}
async start(options) {
const { server: serverOpts = {}, gateway: gatewayOpts = {} } = options;
const gateway = new ApolloGateway(gatewayOpts);
await gateway.load();
await super.start({
...serverOpts,
plugins: [...(serverOpts.plugins || []), useApolloFederation({ gateway })],
});
}
async mergeDefaultOptions(options) {
return {
...options,
server: await super.mergeDefaultOptions(options?.server ?? {}),
};
}
};
YogaGatewayDriver = __decorate([
Injectable()
], YogaGatewayDriver);