How to deal with multiple environments ?
See original GitHub issueHello,
We currently encounter a specific case where we have to deal with multiple graphql apis. We have a graphql api developed by another team and we develop our own graphql api which returns mock data. This second api allows us to do a lot of tests and to develop new features even if the real graphql api is not ready yet.
We would like to use different environments in function of the query. Some queries can already be done on the real api, but some other exist only on the mock api.
The issue is that all the methods (useQuery, useRefetch, etc.) take the environment from the context provider, and we have no solution to override that for one useQuery for example.
Is there a solution to deal with multiple environments ? The only solution I see for now is an evolution in the methods useQuery etc. to add an extra optional parameter customEnvironment and in the method : const environment = customEnvironment || useRelayEnvironment();.
What do you think ?
Thank you !
Edit : Issue solved with the use of networkCacheConfig.metadata object to change dynamically the url in the fetchQuery method (https://github.com/relay-tools/relay-hooks/issues/59#issuecomment-595809130)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:11 (4 by maintainers)

Top Related StackOverflow Question
Hi @davidgarry, for your use case it seems sufficient to use a single environment and correctly use useQuery and the networkCacheConfig (specifically the metadata field) and manage your case at the network level.
However your request seems very similar to the solutions adopted in Apollo: https://medium.com/open-graphql/apollo-multiple-clients-with-react-b34b571210a5
@morrys , thank you a lot for your help. It works perfectly with the cacheConfig.metadata object ! I’m able to change the url to use dynamically.