Я пытаюсь добавить источник данных REST на мой сервер Apollo. Я создал класс, который расширяет RESTDataSource, который содержит все запросы API. При попытке вызвать метод входа из моего кода распознавателя GraphQL выдается ошибка
Как это исправить?
Я уже пробовал: привязать метод входа и выборки в конструкторе класса API
this.login = this.login.bind (this);this.fetch = this.fetch.bind (this);
вызов метода входа из конструктора
класс RESTDataSource
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"
})
}
}
Вот сервер index.js apolloФайл 'setup':
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}`)
})
Файл резолвера для GraphQL:
Query: {
lessons: async (_source, _args, {
dataSources
}) => {
const data = await dataSources.webuntisAPI.login(process.env.USER, process.env.PW);
console.log(data);
return data;
}
}