apollographql / federation

🌐  Build and scale a single data graph across multiple services with Apollo's federation gateway.
https://apollographql.com/docs/federation/
Other
667 stars 254 forks source link

How to dynamically route request to service list #747

Open rickgong opened 3 years ago

rickgong commented 3 years ago

Is it possible to route request dynamically by request host?

For example, if request host if us-dev.mycompany.org, i would like to route this request to graph-mdm.us-dev in service list.

` class DnsRouteDataSource extends RemoteGraphQLDataSource { willSendRequest({request, context}) { if (request.host === 'us-dev.mycompany.org') { //send request to graph-mdm.us-dev } else if (request.host === 'graph-mdm.uk-dev') { //send request to graph-mdm.uk-dev
} } }

const gateway = new ApolloGateway({ serviceList: [ {name: 'graph-mdm.us-dev', url: 'http://10.17.80.125:8080'}, {name: 'graph-mdm.uk-dev', url: 'http://10.17.80.126:8080'}, ], buildService({name, url}) { return new DnsRouteDataSource({url}); } });

`

jonahmolinski commented 3 years ago

Could you have 1 deployment for each host configured with different service lists? I.e., docker container with a service list and a hostname passed in as ENVs?

Cheers.

rickgong commented 3 years ago

Could you have 1 deployment for each host configured with different service lists? I.e., docker container with a service list and a hostname passed in as ENVs?

Cheers.

Thanks jonahmolinski! That should work!

While the problem is the k8s cluster has a dozen namespaces, each has a decicated DNS like: us-dev.mycompany.org ca-dev.mycompany.org uk-dev.mycompany.org cn-dev.mycompany.org br-dev.mycompany.org mx-dev.mycompany.org global-dev.mycompany.org

I'm trying to have one replicaset of gateway pods to serve all DNSs. The only reason is we do not have that CPU/Memory to setup dedicated gateway for every namespaces.

For example: if request is from us-dev.mycompany.org, route to subgraph services in k8s namespace us-dev. if request is from uk-dev.mycompany.org, route to subgraph services in k8s namespace uk-dev.

I'm trying to research the extention point of gateway to do the trick.

rickgong commented 3 years ago

This is not possible after further research. A shared gateway ends up with duplicated subgraphs in different namespaces.