在 nestjs 应用程序中使用新的 apollo 服务器版本时出现 Typescript 错误
Typescript error using new apollo server version in nestjs application
我已经在我的 nestJS 应用程序中将 apollo-server-plugin-base
更新到 v3.2,现在我确实收到了这个简单的 nestjs 插件的两个打字稿错误:
import { Plugin } from '@nestjs/graphql'
import {
ApolloServerPlugin,
GraphQLRequestListener
} from 'apollo-server-plugin-base'
@Plugin()
export class LoggingPlugin implements ApolloServerPlugin {
requestDidStart(): GraphQLRequestListener {
console.log('Request started')
return {
willSendResponse() {
console.log('Will send response')
}
}
}
}
不幸的是我根本不明白这个错误。因此我需要一些帮助。
requestDidStart():
TS2416: Property 'requestDidStart' in type 'LoggingPlugin' is not assignable to the same property in base type 'ApolloServerPlugin<BaseContext>'.
Type '() => GraphQLRequestListener<BaseContext>' is not assignable to type '(requestContext: GraphQLRequestContext<BaseContext>) => Promise<void | GraphQLRequestListener<BaseContext>>'.
Type 'GraphQLRequestListener<BaseContext>' is missing the following properties from type 'Promise<void | GraphQLRequestListener<BaseContext>>': then, catch, finally, [Symbol.toStringTag]
将发送响应:
TS2322: Type '() => void' is not assignable to type '(requestContext: GraphQLRequestContextWillSendResponse<BaseContext>) => Promise<void>'.
Type 'void' is not assignable to type 'Promise<void>'.
从错误消息来看,您似乎应该使 requestDidStart
成为一个异步函数,方法是返回 void promise return Promise<void>.resolve();
或向方法中添加 async
关键字声明 async requestDidStart()
.
以下对我有用,并为日志添加了更多风味:
import { Logger } from '@nestjs/common';
import { Plugin } from '@nestjs/graphql';
import { GraphQLRequestContext } from 'apollo-server-core';
import { ApolloServerPlugin, GraphQLRequestListener } from 'apollo-server-plugin-base';
@Plugin()
export class LoggingPlugin implements ApolloServerPlugin {
logger: Logger;
constructor() {
this.logger = new Logger('ApolloServer');
}
// Fires whenever a GraphQL request is received from a client.
async requestDidStart(requestContext: GraphQLRequestContext): Promise<GraphQLRequestListener> {
this.logger.log(
'Request received:\n' +
requestContext.request.query +
'\nvariables:' +
JSON.stringify(requestContext.request.variables)
);
return {
async willSendResponse() {
// console.log('Will send response');
},
};
}
}
我已经在我的 nestJS 应用程序中将 apollo-server-plugin-base
更新到 v3.2,现在我确实收到了这个简单的 nestjs 插件的两个打字稿错误:
import { Plugin } from '@nestjs/graphql'
import {
ApolloServerPlugin,
GraphQLRequestListener
} from 'apollo-server-plugin-base'
@Plugin()
export class LoggingPlugin implements ApolloServerPlugin {
requestDidStart(): GraphQLRequestListener {
console.log('Request started')
return {
willSendResponse() {
console.log('Will send response')
}
}
}
}
不幸的是我根本不明白这个错误。因此我需要一些帮助。
requestDidStart():
TS2416: Property 'requestDidStart' in type 'LoggingPlugin' is not assignable to the same property in base type 'ApolloServerPlugin<BaseContext>'.
Type '() => GraphQLRequestListener<BaseContext>' is not assignable to type '(requestContext: GraphQLRequestContext<BaseContext>) => Promise<void | GraphQLRequestListener<BaseContext>>'.
Type 'GraphQLRequestListener<BaseContext>' is missing the following properties from type 'Promise<void | GraphQLRequestListener<BaseContext>>': then, catch, finally, [Symbol.toStringTag]
将发送响应:
TS2322: Type '() => void' is not assignable to type '(requestContext: GraphQLRequestContextWillSendResponse<BaseContext>) => Promise<void>'.
Type 'void' is not assignable to type 'Promise<void>'.
从错误消息来看,您似乎应该使 requestDidStart
成为一个异步函数,方法是返回 void promise return Promise<void>.resolve();
或向方法中添加 async
关键字声明 async requestDidStart()
.
以下对我有用,并为日志添加了更多风味:
import { Logger } from '@nestjs/common';
import { Plugin } from '@nestjs/graphql';
import { GraphQLRequestContext } from 'apollo-server-core';
import { ApolloServerPlugin, GraphQLRequestListener } from 'apollo-server-plugin-base';
@Plugin()
export class LoggingPlugin implements ApolloServerPlugin {
logger: Logger;
constructor() {
this.logger = new Logger('ApolloServer');
}
// Fires whenever a GraphQL request is received from a client.
async requestDidStart(requestContext: GraphQLRequestContext): Promise<GraphQLRequestListener> {
this.logger.log(
'Request received:\n' +
requestContext.request.query +
'\nvariables:' +
JSON.stringify(requestContext.request.variables)
);
return {
async willSendResponse() {
// console.log('Will send response');
},
};
}
}