javascript - 无法在 typescript 中配置 Apolloserver 的数据源
问题描述
我对 TypeScript 和 Apollo 很陌生。我第一次尝试设置我的 Apollo 服务器,但我不断收到有关“数据源”的错误消息。我不确定这意味着什么以及如何解决它。
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}) 但我认为这不能解决问题...
有谁知道是什么原因造成的?
解决方案
改变
const { RESTDataSource } = require('apollo-datasource-rest');
到
import { RESTDataSource } from 'apollo-datasource-rest';
这应该会为您解决。
推荐阅读
- angular - 应用程序cordova + angular中的后退按钮正在路由器插座中累积视图
- join - 具有来自 SAS 查找表的日期的子集数据
- autohotkey - autohotkey:如何让鼠标右键同时模仿自己和鼠标左键的功能?
- sql - 如何在 t-sql 中将新的根元素添加到 json 字符串
- three.js - THREE.js Line 在哪里与网格相交
- apache-nifi - 无法在 nifi 上添加扩展
- pandas - 使用 seaborn:如何在箱线图中显示分位数的值
- python - Mypy 抱怨 __setitem__ 签名
- python - pymysql insert into 报错scrapy
- synchronization - 如何使用 WebRTC 同步视频