|
| 1 | +import { Injectable } from '@nestjs/common'; |
| 2 | +import { loadPackage } from '@nestjs/common/utils/load-package.util'; |
| 3 | +import { gql } from 'apollo-server-core'; |
| 4 | +import { GraphQLSchema } from 'graphql'; |
| 5 | +import { mergeSchemas } from 'graphql-tools'; |
| 6 | +import { isEmpty } from 'lodash'; |
| 7 | +import { GraphQLSchemaBuilder } from './graphql-schema-builder'; |
| 8 | +import { GqlModuleOptions } from './interfaces'; |
| 9 | +import { |
| 10 | + DelegatesExplorerService, |
| 11 | + ResolversExplorerService, |
| 12 | + ScalarsExplorerService, |
| 13 | +} from './services'; |
| 14 | +import { extend } from './utils'; |
| 15 | + |
| 16 | +@Injectable() |
| 17 | +export class GraphQLFederationFactory { |
| 18 | + constructor( |
| 19 | + private readonly resolversExplorerService: ResolversExplorerService, |
| 20 | + private readonly delegatesExplorerService: DelegatesExplorerService, |
| 21 | + private readonly scalarsExplorerService: ScalarsExplorerService, |
| 22 | + private readonly gqlSchemaBuilder: GraphQLSchemaBuilder, |
| 23 | + ) {} |
| 24 | + |
| 25 | + async mergeOptions( |
| 26 | + options: GqlModuleOptions = {}, |
| 27 | + ): Promise<GqlModuleOptions> { |
| 28 | + const transformSchema = async schema => |
| 29 | + options.transformSchema ? options.transformSchema(schema) : schema; |
| 30 | + |
| 31 | + let schema: GraphQLSchema; |
| 32 | + if (options.autoSchemaFile) { |
| 33 | + // Enable support when Directive support in type-graphql goes stable |
| 34 | + throw new Error('Code-first approach is not supported yet'); |
| 35 | + // schema = await this.generateSchema(options); |
| 36 | + } else if (isEmpty(options.typeDefs)) { |
| 37 | + schema = options.schema; |
| 38 | + } else { |
| 39 | + schema = this.buildSchemaFromTypeDefs(options); |
| 40 | + } |
| 41 | + |
| 42 | + return { |
| 43 | + ...options, |
| 44 | + schema: await transformSchema(schema), |
| 45 | + typeDefs: undefined, |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + private buildSchemaFromTypeDefs(options: GqlModuleOptions) { |
| 50 | + const { buildFederatedSchema } = loadPackage( |
| 51 | + '@apollo/federation', |
| 52 | + 'ApolloFederation', |
| 53 | + () => require('@apollo/federation'), |
| 54 | + ); |
| 55 | + |
| 56 | + return buildFederatedSchema([ |
| 57 | + { |
| 58 | + typeDefs: gql` |
| 59 | + ${options.typeDefs} |
| 60 | + `, |
| 61 | + resolvers: this.getResolvers(options.resolvers), |
| 62 | + }, |
| 63 | + ]); |
| 64 | + } |
| 65 | + |
| 66 | + private async generateSchema( |
| 67 | + options: GqlModuleOptions, |
| 68 | + ): Promise<GraphQLSchema> { |
| 69 | + const { |
| 70 | + buildFederatedSchema, |
| 71 | + printSchema, |
| 72 | + } = loadPackage('@apollo/federation', 'ApolloFederation', () => |
| 73 | + require('@apollo/federation'), |
| 74 | + ); |
| 75 | + |
| 76 | + const autoGeneratedSchema: GraphQLSchema = await this.gqlSchemaBuilder.buildFederatedSchema( |
| 77 | + options.autoSchemaFile, |
| 78 | + options.buildSchemaOptions, |
| 79 | + this.resolversExplorerService.getAllCtors(), |
| 80 | + ); |
| 81 | + const executableSchema = buildFederatedSchema({ |
| 82 | + typeDefs: gql(printSchema(autoGeneratedSchema)), |
| 83 | + resolvers: this.getResolvers(options.resolvers), |
| 84 | + }); |
| 85 | + |
| 86 | + const schema = options.schema |
| 87 | + ? mergeSchemas({ |
| 88 | + schemas: [options.schema, executableSchema], |
| 89 | + }) |
| 90 | + : executableSchema; |
| 91 | + |
| 92 | + return schema; |
| 93 | + } |
| 94 | + |
| 95 | + private getResolvers(optionResolvers) { |
| 96 | + optionResolvers = Array.isArray(optionResolvers) |
| 97 | + ? optionResolvers |
| 98 | + : [optionResolvers]; |
| 99 | + return this.extendResolvers([ |
| 100 | + this.resolversExplorerService.explore(), |
| 101 | + this.delegatesExplorerService.explore(), |
| 102 | + ...this.scalarsExplorerService.explore(), |
| 103 | + ...optionResolvers, |
| 104 | + ]); |
| 105 | + } |
| 106 | + |
| 107 | + private extendResolvers(resolvers: any[]) { |
| 108 | + return resolvers.reduce((prev, curr) => extend(prev, curr), {}); |
| 109 | + } |
| 110 | +} |
0 commit comments