Changing a client's URL #1093
-
Hello! 👋 import { createClient, Provider } from 'urql';
const client = createClient({
url: 'http://localhost:3000/graphql',
});
const App = () => (
<Provider value={client}>
<YourRoutes />
</Provider>
); In the app I'm using I have a "toggle" for certain users to choose between a "Staging" and a "Production" API, the effect of which should be to change the Looking at the code, I see that The alternative I see would be to use some kind of state and build a new client when I need to change the url import { createClient, Provider } from 'urql';
const buildClient = (url) =>
createClient({
url,
});
const App = () => {
const [client, setClient] = useState(buildClient(Config.graphQLURL)); // pass setClient down to components, possibly via its own context?
<Provider value={client}>
<YourRoutes />
</Provider>
} What would you recommend? I'd be inclined to just set Thanks in advance 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hey I once made an exchange with this purpose, multi-endpoints exchange which allows you to choose url's given certain conditions 003c0a1. For the case of recreating the client, I'd advise on that for your particular customer, this mainly because it also resets the state, ... Mutating the url would be a last resort imo. You can also make a custom wrapper for your hook-calls and pass in the url through the In short, your approach seems fine to me personally and can be expanded on for cache-clearing, ... |
Beta Was this translation helpful? Give feedback.
-
@JoviDeCroock I'm using a
|
Beta Was this translation helpful? Give feedback.
Hey
I once made an exchange with this purpose, multi-endpoints exchange which allows you to choose url's given certain conditions 003c0a1.
For the case of recreating the client, I'd advise on that for your particular customer, this mainly because it also resets the state, ... Mutating the url would be a last resort imo. You can also make a custom wrapper for your hook-calls and pass in the url through the
context
parameter.In short, your approach seems fine to me personally and can be expanded on for cache-clearing, ...