3
votes

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;
        }
}
2
What does console.log(this.fetch); show in the console?kmoser
The output is: [Function: bound fetch]Orbit
Which file and line number is throwing this error?kmoser
@kmoser the error is in node_modules\\apollo-datasource-rest\\dist\\RESTDataSource.js:158:63). I also noticed something we printing this in the API class. There is a property called httpFetch which is undefined. When looking in to the restdatasource.js file the line that is throwing the error const response = yield this.httpCache.fetch(request, {.. uses this.httpCache which is created like this in the constructor: this.httpCache = new HTTPCache_1.HTTPCache(config.cache, this.httpFetch) using httpFetch which(i guess) is undefined. But I have no clue whyOrbit

2 Answers

4
votes

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

2
votes

I am writing this as an answer instead of comment because I don't have enough reps for that. In the tutorial on their official website, this is mentioned. this is mentioned Their tutorial runs fine when cloned and run, but the same isn't working when I implement it with overriding initialize. However, if I don't override it, it works as you've mentioned. Are we doing it wrong? Or their docs need to be updated? Because the incorrect user being present on context is a serious case.