UNPKG

@graphql-yoga/nestjs-federation

Version:

GraphQL Yoga driver with Apollo Federation for NestJS GraphQL.

68 lines (66 loc) 2.48 kB
import { __decorate } from "./_virtual/_@oxc-project_runtime@0.110.0/helpers/decorate.mjs"; 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 } from "@nestjs/graphql"; //#region src/index.ts let YogaFederationDriver = class YogaFederationDriver extends AbstractYogaDriver { 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()], 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); //#endregion export { YogaFederationDriver, YogaGatewayDriver };