@graphql-yoga/nestjs-federation
Version:
GraphQL Yoga driver with Apollo Federation for NestJS GraphQL.
70 lines (69 loc) • 2.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.YogaGatewayDriver = exports.YogaFederationDriver = void 0;
const tslib_1 = require("tslib");
const gateway_1 = require("@apollo/gateway");
const subgraph_1 = require("@apollo/subgraph");
const apollo_federation_1 = require("@envelop/apollo-federation");
const common_1 = require("@nestjs/common");
const graphql_1 = require("@nestjs/graphql");
const nestjs_1 = require("@graphql-yoga/nestjs");
const plugin_apollo_inline_trace_1 = require("@graphql-yoga/plugin-apollo-inline-trace");
const graphql_2 = require("graphql");
let YogaFederationDriver = class YogaFederationDriver extends nestjs_1.AbstractYogaDriver {
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) {
// See more: https://github.com/apollographql/apollo-server/issues/2776
throw new Error('No support for subscriptions when using Apollo Federation');
}
}
};
YogaFederationDriver = tslib_1.__decorate([
(0, common_1.Injectable)(),
tslib_1.__metadata("design:paramtypes", [graphql_1.GraphQLFederationFactory])
], YogaFederationDriver);
exports.YogaFederationDriver = YogaFederationDriver;
let YogaGatewayDriver = class YogaGatewayDriver extends nestjs_1.AbstractYogaDriver {
async generateSchema(_options) {
return new graphql_2.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 ?? {}),
};
}
};
YogaGatewayDriver = tslib_1.__decorate([
(0, common_1.Injectable)()
], YogaGatewayDriver);
exports.YogaGatewayDriver = YogaGatewayDriver;