多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
### 迁移到 v10 本文提供了一组从`@nestjs/graphql`版本 9 迁移到版本 10 的指南。这个主要版本版本的重点是提供一个更轻量级的、与平台无关的核心库。 #### 引入“驱动程序”包[#](https://docs.nestjs.com/graphql/migration-guide#introducing-driver-packages) 在最新版本中,我们决定将`@nestjs/graphql`包分解为几个单独的库,让您选择是否使用 Apollo (`@nestjs/apollo`)、Mercurius (`@nestjs/mercurius` ) 或项目中的另一个 GraphQL 库。 这意味着现在您必须明确指定您的应用程序将使用什么驱动程序。 ~~~typescript // Before import { Module } from '@nestjs/common'; import { GraphQLModule } from '@nestjs/graphql'; @Module({ imports: [ GraphQLModule.forRoot({ autoSchemaFile: 'schema.gql', }), ], }) export class AppModule {} // After import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo'; import { Module } from '@nestjs/common'; import { GraphQLModule } from '@nestjs/graphql'; @Module({ imports: [ GraphQLModule.forRoot<ApolloDriverConfig>({ driver: ApolloDriver, autoSchemaFile: 'schema.gql', }), ], }) export class AppModule {} ~~~ #### 插件[#](https://docs.nestjs.com/graphql/migration-guide#plugins) Apollo Server 插件允许您执行自定义操作以响应某些事件。 由于这是 Apollo 独有的功能,我们将其从`@nestjs/graphql` 移至新创建的`@nestjs/apollo` 包,因此您必须更新应用程序中的导入。 ~~~typescript // Before import { Plugin } from '@nestjs/graphql'; // After import { Plugin } from '@nestjs/apollo'; ~~~ #### 指令[#](https://docs.nestjs.com/graphql/migration-guide#directives) `schemaDirectives` 功能已替换为`@graphql-tools/schema`包 v8 中的新 [Schema 指令 API](https://www.graphql-tools.com/docs/schema-directives)。 ~~~typescript // Before import { SchemaDirectiveVisitor } from '@graphql-tools/utils'; import { defaultFieldResolver, GraphQLField } from 'graphql'; export class UpperCaseDirective extends SchemaDirectiveVisitor { visitFieldDefinition(field: GraphQLField<any, any>) { const { resolve = defaultFieldResolver } = field; field.resolve = async function (...args) { const result = await resolve.apply(this, args); if (typeof result === 'string') { return result.toUpperCase(); } return result; }; } } // After import { getDirective, MapperKind, mapSchema } from '@graphql-tools/utils'; import { defaultFieldResolver, GraphQLSchema } from 'graphql'; export function upperDirectiveTransformer( schema: GraphQLSchema, directiveName: string, ) { return mapSchema(schema, { [MapperKind.OBJECT_FIELD]: (fieldConfig) => { const upperDirective = getDirective( schema, fieldConfig, directiveName, )?.[0]; if (upperDirective) { const { resolve = defaultFieldResolver } = fieldConfig; // Replace the original resolver with a function that *first* calls // the original resolver, then converts its result to upper case fieldConfig.resolve = async function (source, args, context, info) { const result = await resolve(source, args, context, info); if (typeof result === 'string') { return result.toUpperCase(); } return result; }; return fieldConfig; } }, }); } ~~~ 要将此指令实现应用于包含`@upper`指令的模式,请使用`transformSchema`函数: ~~~typescript GraphQLModule.forRoot<ApolloDriverConfig>({ ... transformSchema: schema => upperDirectiveTransformer(schema, 'upper'), }) ~~~ #### 联合[#](https://docs.nestjs.com/graphql/migration-guide#federation) `GraphQLFederationModule` 已被移除并替换为相应的驱动程序类: ~~~typescript // Before GraphQLFederationModule.forRoot({ autoSchemaFile: true, }); // After GraphQLModule.forRoot<ApolloFederationDriverConfig>({ driver: ApolloFederationDriver, autoSchemaFile: true, }); ~~~ > **提示**`ApolloFederationDriver`类和`ApolloFederationDriverConfig`都是从`@nestjs/apollo`包中导出的。 同样,不要使用专用的`GraphQLGatewayModule`,只需将适当的`driver`类传递给您的`GraphQLModule`设置: ~~~typescript // Before GraphQLGatewayModule.forRoot({ gateway: { supergraphSdl: new IntrospectAndCompose({ subgraphs: [ { name: 'users', url: 'http://localhost:3000/graphql' }, { name: 'posts', url: 'http://localhost:3001/graphql' }, ], }), }, }); // After GraphQLModule.forRoot<ApolloGatewayDriverConfig>({ driver: ApolloGatewayDriver, gateway: { supergraphSdl: new IntrospectAndCompose({ subgraphs: [ { name: 'users', url: 'http://localhost:3000/graphql' }, { name: 'posts', url: 'http://localhost:3001/graphql' }, ], }), }, }); ~~~ > **提示**`ApolloGatewayDriver`类和`ApolloGatewayDriverConfig`都是从`@nestjs/apollo`包中导出的。