[英]Typescript error using new apollo server version in nestjs application
I've updated apollo-server-plugin-base
to v3.2 in my nestJS application and now I do get two typescript errors for this simple nestjs plugin:我已经在我的 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')
}
}
}
}
Unfortunately I do not understand the error at all.不幸的是,我根本不明白这个错误。 Therefore I need some help.
因此我需要一些帮助。
requestDidStart(): 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]
willSendResponse: willSendResponse:
TS2322: Type '() => void' is not assignable to type '(requestContext: GraphQLRequestContextWillSendResponse<BaseContext>) => Promise<void>'.
Type 'void' is not assignable to type 'Promise<void>'.
From the error message, it looks like you should be making requestDidStart
an asynchronous function, either by returning a void promise return Promise<void>.resolve();
从错误消息来看,您似乎应该通过返回一个 void 承诺
return Promise<void>.resolve();
使requestDidStart
成为一个异步函数return Promise<void>.resolve();
or by adding the async
keyword to the method declaration async requestDidStart()
.或者通过将
async
关键字添加到方法声明async requestDidStart()
。
The following works for me, and adds a bit more flavour to the logging:以下对我有用,并为日志记录添加了更多风味:
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');
},
};
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.