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: dynamic baseurl #1181

Closed
danilobuerger opened this issue Jun 16, 2018 · 10 comments
Closed

apollo-datasource-rest: dynamic baseurl #1181

danilobuerger opened this issue Jun 16, 2018 · 10 comments

Comments

@danilobuerger
Copy link
Contributor

It would be nice to specify a dynamic baseurl for a RESTDataSource to be able to use fx consul srv entries.

@evans
Copy link
Contributor

evans commented Jun 20, 2018

@danilobuerger hmm interesting, I'm not sure I follow. How do you set the url dynamically? What information would it depend on?

The other option is to set baseUrl to '' and then inside of get put the full url.

@martijnwalraven might be interested as well.

@martijnwalraven
Copy link
Contributor

martijnwalraven commented Jun 20, 2018

This is already supported by using a getter instead of setting the baseURL property directly. Something like:

  get baseURL() {
    if (this.context.environment === 'development') {
      return 'https://dev.example-api.com';
    } else {
      return 'https://prod.example-api.com';
    }
  }

Would that work for your use case? Not sure how Consul SRV entries fit into this. What makes them dynamic? What are they based on?

@danilobuerger
Copy link
Contributor Author

Hi @evans and @martijnwalraven ! Thanks for responding, I should probably have been more specific. Sorry about that. What I would like to do is something like (shortened):

function baseURL(path: string, params?: URLSearchParamsInit, init?: RequestInit) {
  return new Promise<string>((resolve, reject) => {
    dns.resolveSrv(path.split("/")[1] + ".service.consul", (err, addresses) => {
      err ? reject(err) : resolve("http://" + addresses[0].name + ":" + addresses[0].port);
    });
  });
}

Basically return a promise that would resolve to a base url based on the supplied params. In case of consul one could for example use part of the path to get the name of a microservice to resolve its srv to get its (possible load balanced, round robin, etc.) address.

@martijnwalraven
Copy link
Contributor

@danilobuerger Hmmm, we could make it possible to return a promise from the baseURL getter, that would make it possible to resolve the record to get the host and port.

But why does the URL need to depend on the path, params or other call-specific information? Couldn't you just define a data source per microservice?

You also probably wouldn't want to resolve for every call, that seems like quite a bit of overhead?

@danilobuerger
Copy link
Contributor Author

True, this was just an example in brevity. You could cache the result for example or track a round robin on the returned addresses. For this specific example, you could probably get away with defining it per microservice and not having to rely on the path. I included path, params, init in the example above because those are in context anyway when baseURL is currently getting called and I bet some other guy will come along and needing it for some use case we haven't thought of once (if) returning a promise is possible.

@martijnwalraven
Copy link
Contributor

@danilobuerger I experimented some and came up with 2e6073d#diff-94c74c4c5e5f1e69b942b0b56a962852R133. Not sure if it is worth the complexity, but curious what you think.

@godspeedelbow
Copy link

godspeedelbow commented Jun 28, 2018

+1 I need to poll a configuration server first before I know baseURL, so it would be great if the baseURL getter can return a Promise. Otherwise, I have to postpone instantiating ApolloServer until I have resolved the baseURL value

@danilobuerger
Copy link
Contributor Author

@martijnwalraven Awesome work! Line read looks good. Concept makes sense.

@martijnwalraven
Copy link
Contributor

@danilobuerger @godspeedelbow I incorporated the feedback and opened a PR with a discussion and some questions. Your input is appreciated! #1277

@danilobuerger
Copy link
Contributor Author

Fixed in #1277

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 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

4 participants