@graphql-yoga/nestjs-federation
Version:
GraphQL Yoga driver with Apollo Federation for NestJS GraphQL.
79 lines (78 loc) • 3.27 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.YogaGatewayDriver = exports.YogaFederationDriver = void 0;
const tslib_1 = require("tslib");
const graphql_1 = require("graphql");
const gateway_1 = require("@apollo/gateway");
const subgraph_1 = require("@apollo/subgraph");
const apollo_federation_1 = require("@envelop/apollo-federation");
const nestjs_1 = require("@graphql-yoga/nestjs");
const plugin_apollo_inline_trace_1 = require("@graphql-yoga/plugin-apollo-inline-trace");
const common_1 = require("@nestjs/common");
const graphql_2 = require("@nestjs/graphql");
let YogaFederationDriver = exports.YogaFederationDriver = class YogaFederationDriver extends nestjs_1.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((0, subgraph_1.printSubgraphSchema)(options.schema), options);
}
await super.start({
...options,
plugins: [...(options?.plugins || []), (0, plugin_apollo_inline_trace_1.useApolloInlineTrace)()],
});
if (options.subscriptions && options.schema) {
const config = options.subscriptions === true
? {
'graphql-ws': true,
}
: options.subscriptions;
this.subscriptionService = new graphql_2.GqlSubscriptionService({
schema: options.schema,
path: options.path,
context: options.context,
...config,
}, this.httpAdapterHost.httpAdapter?.getHttpServer());
}
}
async stop() {
await this.subscriptionService?.stop();
}
};
exports.YogaFederationDriver = YogaFederationDriver = tslib_1.__decorate([
(0, common_1.Injectable)(),
tslib_1.__metadata("design:paramtypes", [graphql_2.GraphQLFederationFactory])
], YogaFederationDriver);
let YogaGatewayDriver = exports.YogaGatewayDriver = class YogaGatewayDriver extends nestjs_1.AbstractYogaDriver {
async generateSchema(_options) {
return new graphql_1.GraphQLSchema({});
}
async start(options) {
const { server: serverOpts = {}, gateway: gatewayOpts = {} } = options;
const gateway = new gateway_1.ApolloGateway(gatewayOpts);
await gateway.load();
await super.start({
...serverOpts,
plugins: [...(serverOpts.plugins || []), (0, apollo_federation_1.useApolloFederation)({ gateway })],
});
}
async mergeDefaultOptions(options) {
return {
...options,
server: await super.mergeDefaultOptions(options?.server ?? {}),
};
}
};
exports.YogaGatewayDriver = YogaGatewayDriver = tslib_1.__decorate([
(0, common_1.Injectable)()
], YogaGatewayDriver);
;