Orb*_*bit 3 javascript node.js graphql apollo-server
我正在尝试将 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"
}, {
params: {
"user": username,
"password": password
},
"jsonrpc": "2.0"
})
}
}
Run Code Online (Sandbox Code Playgroud)
这是 index.js apollo 服务器“设置”文件:
const server = new ApolloServer({
typeDefs,
resolvers,
dataSources: () => {
return {
managementDatabase: new ManagementDatabase(),
API: new API() //was previously imported
}
}
});
server.listen().then(({
url
}) => {
log(` Server ready at ${url}`)
})
Run Code Online (Sandbox Code Playgroud)
GraphQL 的解析器文件:
Query: {
lessons: async (_source, _args, {
dataSources
}) => {
const data = await dataSources.webuntisAPI.login(process.env.USER, process.env.PW);
console.log(data);
return data;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7872 次 |
| 最近记录: |