Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Renaud009 opened this issue Apr 11, 2019 · 6 comments
Closed

Comments

@Renaud009
Copy link

Renaud009 commented Apr 11, 2019

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?

@Renaud009 Renaud009 changed the title Reuse apollo-datasource-rest classes outside of Apollo Server context [apollo-datasource-rest] - Reuse classes outside of Apollo Server context Apr 11, 2019
@Renaud009
Copy link
Author

Renaud009 commented Apr 11, 2019

Actually relates to this #2240

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

this.authenticationAPI = new AuthenticationAPI()
this.authenticationAPI.initialize({})
const token = await this.authenticationAPI.login(credentials)

@dancon
Copy link

dancon commented Apr 15, 2019

i have the same issue

@martijnwalraven
Copy link
Contributor

As mentioned above, data sources are automatically initialized by Apollo Server with the context, so you'll need to do this manually when using them separately.

@quentinus95
Copy link

@martijnwalraven do you plan having a clean support for using datasources in subscriptions?

@martijnwalraven
Copy link
Contributor

@quentinus95 Yes, but not until Apollo Server 3 unfortunately. Subscriptions are currently implemented as a completely separate server that doesn't share the request pipeline with the regular server.

@robe007
Copy link

robe007 commented Sep 17, 2020

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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants