### 遷移到 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`包中導出的。
- 介紹
- 概述
- 第一步
- 控制器
- 提供者
- 模塊
- 中間件
- 異常過濾器
- 管道
- 守衛
- 攔截器
- 自定義裝飾器
- 基礎知識
- 自定義提供者
- 異步提供者
- 動態模塊
- 注入作用域
- 循環依賴
- 模塊參考
- 懶加載模塊
- 應用上下文
- 生命周期事件
- 跨平臺
- 測試
- 技術
- 數據庫
- Mongo
- 配置
- 驗證
- 緩存
- 序列化
- 版本控制
- 定時任務
- 隊列
- 日志
- Cookies
- 事件
- 壓縮
- 文件上傳
- 流式處理文件
- HTTP模塊
- Session(會話)
- MVC
- 性能(Fastify)
- 服務器端事件發送
- 安全
- 認證(Authentication)
- 授權(Authorization)
- 加密和散列
- Helmet
- CORS(跨域請求)
- CSRF保護
- 限速
- GraphQL
- 快速開始
- 解析器(resolvers)
- 變更(Mutations)
- 訂閱(Subscriptions)
- 標量(Scalars)
- 指令(directives)
- 接口(Interfaces)
- 聯合類型
- 枚舉(Enums)
- 字段中間件
- 映射類型
- 插件
- 復雜性
- 擴展
- CLI插件
- 生成SDL
- 其他功能
- 聯合服務
- 遷移指南
- Websocket
- 網關
- 異常過濾器
- 管道
- 守衛
- 攔截器
- 適配器
- 微服務
- 概述
- Redis
- MQTT
- NATS
- RabbitMQ
- Kafka
- gRPC
- 自定義傳輸器
- 異常過濾器
- 管道
- 守衛
- 攔截器
- 獨立應用
- Cli
- 概述
- 工作空間
- 庫
- 用法
- 腳本
- Openapi
- 介紹
- 類型和參數
- 操作
- 安全
- 映射類型
- 裝飾器
- CLI插件
- 其他特性
- 遷移指南
- 秘籍
- CRUD 生成器
- 熱重載
- MikroORM
- TypeORM
- Mongoose
- 序列化
- 路由模塊
- Swagger
- 健康檢查
- CQRS
- 文檔
- Prisma
- 靜態服務
- Nest Commander
- 問答
- Serverless
- HTTP 適配器
- 全局路由前綴
- 混合應用
- HTTPS 和多服務器
- 請求生命周期
- 常見錯誤
- 實例
- 遷移指南
- 發現
- 誰在使用Nest?