我对TypeScript和Apollo还是很陌生。我正在尝试首次设置我的Apollo服务器,但我不断收到有关“ DataSource”的错误。我不确定这意味着什么以及如何解决。
import { ApolloServer, gql } from 'apollo-server';
//...
const { RESTDataSource } = require('apollo-datasource-rest');
class MoviesAPI extends RESTDataSource {
constructor() {
super();
this.baseURL = 'https://movies-api.example.com/';
}
async getMovie(id) {
return this.get(`movies/${id}`);
}
async getMostViewedMovies(limit = 10) {
const data = await this.get('movies', {
per_page: limit,
order_by: 'most_viewed',
});
return data.results;
}
}
const SERVERCONFIG = {
'typeDefs': gql(typeDefs) ,
resolvers,
dataSources: ()=> ({
MoviesAPI: new MoviesAPI(),
} ) ,
};
const server = new ApolloServer(SERVERCONFIG);
我收到以下错误:
Type '{ 'typeDefs': DocumentNode; resolvers: { Query: {}; }; dataSources: () => { MoviesAPI: MoviesAPI; }; }' is not assignable to type 'ApolloServerExpressConfig'.
The types returned by 'dataSources()' are incompatible between these types.
Type '{ MoviesAPI: MoviesAPI; }' is not assignable to type 'DataSources<object>'.
Property 'MoviesAPI' is incompatible with index signature.
Type 'MoviesAPI' has no properties in common with type 'DataSource<object>'.
如果我做类似的事情,我可以使错误消失:dataSources:()=>({... MoviesAPI}),但我认为这不能解决问题...
有人知道是什么原因造成的吗?
答案 0 :(得分:1)
更改
const { RESTDataSource } = require('apollo-datasource-rest');
到
import { RESTDataSource } from 'apollo-datasource-rest';
这应该为您解决。