Plugin cho phép bạn mở rộng các chức năng cốt lõi của Apollo Server bằng cách thực hiện các thao tác tùy chỉnh để phản hồi lại một số sự kiện nhất định. Hiện tại, các sự kiện này tương ứng với từng giai đoạn (phase) riêng lẻ trong vòng đời request GraphQL, cũng như sự kiện khởi động (startup) của chính Apollo Server (đọc thêm tại đây). Ví dụ, một plugin logging cơ bản có thể log lại chuỗi GraphQL query đi kèm mỗi request gửi tới Apollo Server.
Plugin tùy chỉnh
Để tạo một plugin, khai báo một class được gắn decorator @Plugin export từ package @nestjs/apollo. Ngoài ra, để có gợi ý code tốt hơn, hãy implement interface ApolloServerPlugin từ package @apollo/server.
import { ApolloServerPlugin, GraphQLRequestListener } from '@apollo/server';
import { Plugin } from '@nestjs/apollo';
@Plugin()
export class LoggingPlugin implements ApolloServerPlugin {
async requestDidStart(): Promise<GraphQLRequestListener<any>> {
console.log('Request started');
return {
async willSendResponse() {
console.log('Will send response');
},
};
}
}
Với đoạn code này, ta có thể đăng ký LoggingPlugin như một provider.
@Module({
providers: [LoggingPlugin],
})
export class CommonModule {}
Nest sẽ tự động khởi tạo plugin và áp dụng nó vào Apollo Server.
Ghi chú người dịch Cơ chế này giống hệt cách @Injectable() hoạt động với các provider thông thường trong Nest — bạn không tự tạo instance của plugin bằng tay, IoC container sẽ lo việc đó (kèm khả năng inject dependency vào constructor của plugin) rồi tự “gắn” nó vào Apollo Server dưới hậu trường. Khác với việc gọi trực tiếp server.addPlugin(...) như khi dùng Apollo Server thuần, ở đây bạn chỉ cần khai báo decorator và đưa vào providers, phần còn lại Nest tự lo.
Dùng plugin có sẵn
Có sẵn nhiều plugin được cung cấp out-of-the-box. Để dùng một plugin có sẵn, chỉ cần import nó và thêm vào mảng plugins:
GraphQLModule.forRoot({
// ...
plugins: [ApolloServerOperationRegistry({ /* options */})]
}),
Gợi ý Plugin ApolloServerOperationRegistry được export từ package @apollo/server-plugin-operation-registry.
Plugin với Mercurius
Một số plugin Fastify đặc thù cho mercurius phải được load sau plugin mercurius (đọc thêm tại đây) trong cây plugin (plugin tree).
Cảnh báo mercurius-upload là một ngoại lệ và cần được đăng ký trong file main.
Vì lý do đó, MercuriusDriver cung cấp một tùy chọn cấu hình plugins không bắt buộc. Nó là một mảng các object gồm hai thuộc tính: plugin và options của nó. Do đó, đăng ký cache plugin sẽ trông như sau:
GraphQLModule.forRoot({
driver: MercuriusDriver,
// ...
plugins: [
{
plugin: cache,
options: {
ttl: 10,
policy: {
Query: {
add: true
}
}
},
}
]
}),
Ghi chú người dịch “Plugin tree” ở đây ám chỉ cơ chế đăng ký plugin lồng nhau của Fastify (fastify.register()), khác hẳn hệ middleware tuyến tính của Express. Vì mercurius bản thân nó cũng là một Fastify plugin, plugin nào cần truy cập context GraphQL do mercurius tạo ra (schema, decorator…) bắt buộc phải register sau nó — register trước sẽ không thấy được các decorator mà mercurius gắn vào instance Fastify.