Ad
Cannot Read Property 'fetch' Of Undefined
I am trying to add a REST datasource to my Apollo Server. I created a class which extends RESTDataSource which contains all the API requests. When trying to call the login method from my GraphQL resolver code the error is thrown
How can I fix this?
I already tried: Binding the login and fetch method in the API class constructor
this.login = this.login.bind(this); this.fetch = this.fetch.bind(this);
calling the login method from the constructor
RESTDataSource class
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"
})
}
}
Here's the index.js apollo server 'setup' file:
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}`)
})
Resolver file for GraphQL:
Query: {
lessons: async (_source, _args, {
dataSources
}) => {
const data = await dataSources.webuntisAPI.login(process.env.USER, process.env.PW);
console.log(data);
return data;
}
}
Ad
Answer
Solved the issue.
The problem was the method initalize(config)
which is unnecessary in this case.
So to fix the issue, just remove the initalize method from your code
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad