跳到主要内容

迁移指南

从v10迁移到v11

本章提供了从@nestjs/graphql版本10迁移到版本11的一组指南。 作为这个主要版本的一部分,我们将Apollo驱动程序更新为与Apollo Server v4兼容(而不是v3)。 注意:Apollo Server v4中存在一些重大变化(特别是在插件和生态系统包方面), 因此您将不得不相应地更新代码库。 有关更多信息,请参见Apollo Server v4迁移指南。

Apollo包

不再安装apollo-server-express包,您将需要安装@apollo/server

npm uninstall apollo-server-express
npm install @apollo/server

如果使用Fastify适配器,您将需要安装@as-integrations/fastify包:

npm uninstall apollo-server-fastify
npm install @apollo/server @as-integrations/fastify

Mercurius包

Mercurius网关不再是mercurius包的一部分。相反,您将需要单独安装@mercuriusjs/gateway包:

npm install @mercuriusjs/gateway

同样,为了创建联合模式,您将需要安装@mercuriusjs/federation包:

npm install @mercuriusjs/federation

从v10迁移到v9

本章提供了从@nestjs/graphql版本9迁移到版本10的一组指南。 此主要版本发布的重点是提供一个更轻量级、与平台无关的核心库。

引入“driver”包

在最新版本中,我们决定将@nestjs/graphql包拆分成几个单独的库, 让您可以选择在项目中使用Apollo(@nestjs/apollo)、 Mercurius(@nestjs/mercurius)或其他GraphQL库。

这意味着现在您必须明确指定应用程序将使用的驱动程序。

之前

import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';

@Module({
imports: [
GraphQLModule.forRoot({
autoSchemaFile: 'schema.gql',
}),
],
})
export class AppModule {}

// 之后
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 {}

插件

Apollo Server插件允许您对某些事件做出自定义操作。由于这是Apollo的一个独有功能,我们将其从@nestjs/graphql移动到新创建的@nestjs/apollo包中,因此您必须更新应用程序中的导入。

// 之前
import { Plugin } from '@nestjs/graphql';

// 之后
import { Plugin } from '@nestjs/apollo';

指令

schemaDirectives功能已被v8版本的@graphql-tools/schema包中的新Schema指令API替换。

// 之前
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;
};
}
}

// 之后
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函数:

GraphQLModule.forRoot<ApolloDriverConfig>({
...
transformSchema: schema => upperDirectiveTransformer(schema, 'upper'),
})

联合

GraphQLFederationModule已被删除,并替换为相应的驱动程序类。

// 之前
GraphQLFederationModule.forRoot({
autoSchemaFile: true,
});

// 之后
GraphQLModule.forRoot<ApolloFederationDriverConfig>({
driver: ApolloFederationDriver,
autoSchemaFile: true,
});
提示

ApolloFederationDriver类和ApolloFederationDriverConfig都从@nestjs/apollo包中导出。

同样,不再使用专用的GraphQLGatewayModule,只需将适当的驱动程序类传递给您的GraphQLModule设置:

// 之前
GraphQLGatewayModule.forRoot({
gateway: {
supergraphSdl: new Intros

pectAndCompose({
subgraphs: [
{ name: 'users', url: 'http://localhost:3000/graphql' },
{ name: 'posts', url: 'http://localhost:3001/graphql' },
],
}),
},
});

// 之后
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包中导出。