我正在尝试将 REST 数据源添加到我的 Apollo 服务器。我创建了一个扩展 RESTDataSource 的类,其中包含所有 API 请求。当尝试从我的 GraphQL 解析器代码调用登录方法时,会抛出错误
我怎样才能解决这个问题?
我已经尝试过:在 API 类构造函数中绑定登录和获取方法
this.login = this.login.bind(this); this.fetch = this.fetch.bind(this);
从构造函数调用登录方法
REST数据源类
class API extends RESTDataSource {
constructor() {
super();
this.baseURL = URL;
this.login = this.login.bind(this);
this.fetch = this.fetch.bind(this);
console.log(this.fetch);
console.log(this.login);
}
initialize(config) {
//config can store different information
//the current user can be stored in config.context for easy access
this.context = config.context;
}
async login(username, password) {
return this.post(`/`, {
"id": 1
}, {
"method": "authenticate"
}, { …Run Code Online (Sandbox Code Playgroud)