multiple clients #1523
-
Hi, I need to query two different graphql servers. One is I know it's possible with apollo to add a second client. Something like Is there an easy version for urql as well? Thanks a lot! |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 29 replies
-
I'd suggest creating an abstraction over the hooks honestly, where you use the
|
Beta Was this translation helpful? Give feedback.
-
Was discussed before in issue #429 |
Beta Was this translation helpful? Give feedback.
-
Hey urlq people. :) I'd like to +1 this, with my quite extreme use case: we have multiple microservices served in AWS AppSync endpoints, and for top down architectural decisions we currently have 5 different graphql endpoints for the frontend to handle. With Apollo we were initially importing and using clients locally, but recently we managed to split the link dynamically. Is the approach here to just overwrite the top client url with a local url in the context of the useQuery? Cheers! |
Beta Was this translation helpful? Give feedback.
-
If using React a way to workaround the single client limit is by nesting another This can work well without getting complicated so long as the nested parts of the tree only need to use one client. |
Beta Was this translation helpful? Give feedback.
-
The way I've solved this was inspired by what kitten wrote in #1523 (reply in thread). My use case is I am using a hosted CMS (DatoCMS) which has a GraphQL API, and also have a local API for e-commerce cart operations. I could proxy the CMS API through my local API, but that makes no sense as it massively increases the amount of traffic the local API has to handle. I am using query GetCart {
cart {
id
items {
product {
name
}
}
}
} I have an array of selection set names which should be sent to my local API, and the rest get sent to the CMS. This probably wouldn't work for more complex queries, but it works for my use case and could be expanded to use whatever details of const API_QUERY_SELECTION_SETS = ["cart", "clearCart"];
const urlSplitterExchange = ({ forward }) => {
return (ops$) => {
const shared = pipe(ops$, share);
// Split out queries/mutations, which we want to handle...
const queries = pipe(
shared,
filter((op) => op.kind === "query" || op.kind === "mutation"),
onPush((op) => {
// Get each operations first selection set and check if it's in API_QUERY_SELECTION_SETS
// and set the URL and headers accordingly
const selectionSet = op.query.definitions[0].selectionSet.selections[0].name.value;
if (API_QUERY_SELECTION_SETS.includes(selectionSet)) {
op.context.url = API_URL;
op.context.fetchOptions.headers = API_HEADERS;
} else {
op.context.url = DATO_URL;
op.context.fetchOptions.headers = DATO_HEADERS;
}
})
);
// ...and other messages, which we just forward
const others = pipe(
shared,
filter((op) => !(op.kind === "query" || op.kind === "mutation"))
);
return forward(merge([queries, others]));
};
}; Then add this exchange before the So with the above example, queries or mutations named Cheers, |
Beta Was this translation helpful? Give feedback.
-
The problem with changing client's URL on the fly, is that cache is unintentionally shared. If you send a query to Url1, then for whatever reason, send the same query to Url2, you'll hit the cached response of Url1. |
Beta Was this translation helpful? Give feedback.
-
I'm using urql to fetch some data from the graph (graphql for ethereum basically). I have a similar issue with using different URLs, I need to fetch a lot of different data from different graph endpoints and desperately need a nice way of using different urls. |
Beta Was this translation helpful? Give feedback.
-
Hello! Thank you for the awesome library. Could you please reconsider #446. If using multiple SaaS APIs in a client is not recommended, what would be the best approach? |
Beta Was this translation helpful? Give feedback.
I'd suggest creating an abstraction over the hooks honestly, where you use the
context
argument to our hooks to supply a different url. For insance: