[英]Unsafe access to member with 'any' type when configuring REST data source in Apollo server with TypeScript
I am trying to configure a data source in Apollo server with TypeScript:我正在尝试使用 TypeScript 在 Apollo 服务器中配置数据源:
my-rest-datasource.ts我的休息-datasource.ts
import { RESTDataSource } from 'apollo-datasource-rest';
import ResolverCtx from 'types/resolver-ctx';
import {
IdType,
GetResultType,
} from '../types/myTypes';
class MyRestDataSource extends RESTDataSource<ResolverCtx> {
constructor() {
super();
this.baseURL = 'http://localhost:8080/';
}
async myGetMethod(id: IdType): Promise<GetResultType> {
return this.get(`/get-data/${id}`);
}
}
export default MyRestDataSource;
myResolvers.ts myResolvers.ts
import { Resolvers } from 'types/codegen';
import ResolverCtx from 'types/resolver-ctx';
const myResolvers: Resolvers<ResolverCtx> = {
Query: {
getData: async (_, { id }, ctx) => await ctx.dataSources.myRestDataSource.myGetMethod(id)
},
};
export default myResolvers;
resolver-ctx.ts解析器-ctx.ts
import MyRestDataSource from '../datasources/my-rest-datasource';
type ApplicationContext = Record<string, any>;
type ApplicationDataSources = {
myRestDataSource: MyRestDataSource;
};
type ResolverCtx = ApplicationContext & {
dataSources: ApplicationDataSources;
};
export default ResolverCtx;
However in my-resolvers.ts I am getting ts-lint errors on the getData method:但是,在 my-resolvers.ts 中,我在 getData 方法上遇到了 ts-lint 错误:
Unsafe member access .dataSources on an `any` value.eslint@typescript-eslint/no-unsafe-member-access
Unsafe call of an `any` typed value.eslint@typescript-eslint/no-unsafe-call
If I mouse over the ctx, dataSources or myGetMethod in the return statement, the correct types are shown.如果我将鼠标悬停在 return 语句中的 ctx、dataSources 或 myGetMethod 上,则会显示正确的类型。 So I don't understand where the error is coming from.
所以我不明白错误来自哪里。
This turned out to be to do with the ESLint config within the context of a mono repo.事实证明,这与单声道存储库上下文中的 ESLint 配置有关。
The code above was in a Lerna implementation with an .eslintrc file at the root which had this property in it:上面的代码在 Lerna 实现中,根目录下有一个 .eslintrc 文件,其中包含此属性:
{
"parserOptions": {
"project": "./tsconfig.json"
}
}
The code above sat in a packages directory of the monorepo in a package, this did not have it's own .eslintrc上面的代码位于一个包中的 monorepo 的包目录中,它没有自己的 .eslintrc
This appeared to cause issues with the module resolution or type resolution for the ESlint Typescript linting rules, causing erroneous errors to be thrown.这似乎会导致 ESlint Typescript linting 规则的模块解析或类型解析出现问题,从而导致抛出错误错误。
The solution was to add another .eslintrc file in the package directory which was the root folder of the package containing the code in my initial question.解决方案是在包目录中添加另一个 .eslintrc 文件,该文件是包含我最初问题中的代码的包的根文件夹。 I had to add this rule to the new .eslintrc:
我不得不将此规则添加到新的 .eslintrc 中:
{
"parserOptions": {
"project": "./packages/myPackage1/tsconfig.json"
}
}
This seems to have fixed the resolution problem.这似乎解决了分辨率问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.