NestJS与Apollo Federation中buildFederatedSchema废弃警告解决方法
问题说明
使用NestJS和Apollo Federation开发过程中,更新依赖后出现如下告警:
(node:19113) DeprecationWarning: 'buildFederatedSchema' is deprecated. Use 'buildSubgraphSchema' instead.
代码中没有直接调用buildSubgraphSchema的场景,也未在NestJS或Apollo Federation官方文档中找到消除该废弃警告的方法。项目可正常运行,需要消除该告警。
项目依赖(package.json)
"dependencies": { "@apollo/federation": "^0.33.3", "@apollo/subgraph": "^0.1.2", "@nestjs/common": "^8.0.0", "@nestjs/config": "^1.0.2", "@nestjs/core": "^8.0.0", "@nestjs/graphql": "^9.1.1", "@nestjs/mongoose": "^9.0.1", "@nestjs/platform-express": "^8.0.0", "apollo-server-express": "^3.4.0", "class-transformer": "^0.4.0", "class-validator": "^0.13.1", "graphql": "^15.6.1", "helmet": "^4.6.0", "joi": "^17.4.2", "mongoose": "^6.0.12", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", "rxjs": "^7.2.0" },
GraphQL模块配置(graphql.module.ts)
import { Module } from '@nestjs/common'; import { GraphQLFederationModule } from '@nestjs/graphql'; import { ApolloServerPluginInlineTraceDisabled } from 'apollo-server-core'; @Module({ imports: [ GraphQLFederationModule.forRoot({ plugins: [ApolloServerPluginInlineTraceDisabled()], autoSchemaFile: true, playground: true, introspection: true, buildSchemaOptions: { dateScalarMode: 'isoDate', }, context: ({ req }) => ({ jwt: req.headers.authorization, }), }), ], }) export class GraphqlModule {}
解决方案
将@nestjs/graphql依赖从v9版本升级到v10版本即可解决该问题。v10版本的@nestjs/graphql已经完成了Apollo Federation新API的适配,内部替换了废弃的buildFederatedSchema调用,升级完成后重启服务,弃用警告就会消失。
内容的提问来源于stack exchange,提问作者Marcos Henrique da Silva
相关产品推荐
相关产品推荐

