[apollo-datasource-rest] - Reuse classes outside of Apollo Server context
See original GitHub issueI’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:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Actually relates to this https://github.com/apollographql/apollo-server/issues/2240
Calling initialize() function with empty object fixed my problem.
I had the same problem with
apollo-datasource-rest
combined withgraphql-yoga
, but with the clue of @Renaud009, now it works.You can see my comments about it here