question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[apollo-datasource-rest] - Reuse classes outside of Apollo Server context

See original GitHub issue

I’m using the package apollo-datasource-rest alongside Apollo Server to manage external REST requests and its working fine.

module.exports = class AuthenticationAPI extends RESTDataSource {
  constructor() {
    super()
    this.baseURL = REST_ENDPOINT
  }

  async login(args) {
    return this.post('/FormLogin/login', args)
  }

}

I would like to call those same class methods on server init before actually launching the Apollo Server. Naively I tried to instanciate the class with new and then call methods on the instance but at the moment I’m getting this error when class is instantiated outside the apollo server “context”.

// from anywhere, executed outside of apollo server context
const authenticationAPI = new AuthenticationAPI()
this.authenticationAPI.login(data)
-------
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'fetch' of undefined
    at AuthenticationAPI.<anonymous> (/<folder>/node_modules/apollo-datasource-rest/dist/RESTDataSource.js:151:63)

Is there a way to archieve this or will I have to duplicate same fetching methods?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

13reactions
Renaud009commented, Apr 11, 2019

Actually relates to this https://github.com/apollographql/apollo-server/issues/2240

Calling initialize() function with empty object fixed my problem.

this.authenticationAPI = new AuthenticationAPI()
this.authenticationAPI.initialize({})
const token = await this.authenticationAPI.login(credentials)
1reaction
robe007commented, Oct 11, 2022

I had the same problem with apollo-datasource-rest combined with graphql-yoga, but with the clue of @Renaud009, now it works.

You can see my comments about it here

Read more comments on GitHub >

github_iconTop Results From Across the Web

Data sources - Apollo GraphQL Docs
Data sources are classes that Apollo Server can use to encapsulate fetching data from a particular source, such as a database or a...
Read more >
GraphQL Server Tutorial with Apollo Server and Express
Learn how to build a GraphQL server with Apollo Server and Express with ... This responsibility is handled by resolvers outside of the...
Read more >
Creating a GraphQL API while utilizing your REST API
apollo-datasource-rest. Used to take advantage of caching and other class-based inheritance around REST sources.
Read more >
Apollo Server: How to access 'context' outside of resolvers in ...
const { RESTDataSource } = require('apollo-datasource-rest'); class UserAPI extends RESTDataSource { constructor() { super() this.baseURL = ' ...
Read more >
2. データソースをつなぎこむ - Apollo GraphQL Docs
そしてその data sources を Apollo server に接続します。 ... const { RESTDataSource } = require('apollo-datasource-rest'); class LaunchAPI extends ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found