I am trying to configure a data source in Apollo server with TypeScript:
my-rest-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
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
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:
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. So I don’t understand where the error is coming from.