UNPKG

@graphql-yoga/nestjs-federation

Version:

GraphQL Yoga driver with Apollo Federation for NestJS GraphQL.

67 lines (66 loc) 2.55 kB
import { __decorate, __metadata } from "tslib"; import { ApolloGateway } from '@apollo/gateway'; import { printSubgraphSchema } from '@apollo/subgraph'; import { useApolloFederation } from '@envelop/apollo-federation'; import { Injectable } from '@nestjs/common'; import { GraphQLFederationFactory } from '@nestjs/graphql'; import { AbstractYogaDriver, } from '@graphql-yoga/nestjs'; import { useApolloInlineTrace } from '@graphql-yoga/plugin-apollo-inline-trace'; import { GraphQLSchema } from 'graphql'; let YogaFederationDriver = class YogaFederationDriver extends 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(printSubgraphSchema(options.schema), options); } await super.start({ ...options, 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'); } } }; YogaFederationDriver = __decorate([ Injectable(), __metadata("design:paramtypes", [GraphQLFederationFactory]) ], YogaFederationDriver); export { YogaFederationDriver }; 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); export { YogaGatewayDriver };