Creating a GraphQL fetcher for nanoquery #297
-
Hey, thanks for making this library! I've been using it for some time and it's a joy to use. Right now I'm interested in using nanoquery to fetch from a GraphQL server, but I'm confused on how I would go about writing the fetcher function, and I couldn't find any examples about that on the docs. I'm having trouble specifically with the fact that the fetcher can only take This is what I have right now: import { nanoquery } from "@nanostores/query";
export const [createGQLFetcherStore, createGQLMutatorStore] = nanoquery({
fetcher: (...[query, variables]) =>
fetch(import.meta.env.PUBLIC_GQL_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
query,
variables: JSON.parse(variables as string),
}),
}).then((r) => r.json()),
}); So, how would it be done? :) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
The question for @dkzlv |
Beta Was this translation helpful? Give feedback.
-
Hey there, @Contresito! It's generally up to you how you structure your fetcher functions. The way you did it works too, but I agree that looks a bit cumbersome, and may even have performance tax (more on that below). Here's a demo of how I would approach that. The key takeaway is that I would not use the global fetcher function and instead define fetcher functions in every fetcher store individually. I would then define variables as keys, and just pass them into a generalized Feel free to ask more stuff. In general, if you have The reason why Nonetheless, we decide against that. Primary reason is that it would bloat cache/memory consumption too much. Take my example above. It would be very tempting to put the whole query as key, right? But that would mean that this whole huge string will live in memory for every cache value! And if you decided to pass the whole response object from So instead, we force you to use primitives—they are typically more space efficient. So, as you can see, I put |
Beta Was this translation helpful? Give feedback.
Hey there, @Contresito!
It's generally up to you how you structure your fetcher functions. The way you did it works too, but I agree that looks a bit cumbersome, and may even have performance tax (more on that below).
Here's a demo of how I would approach that. The key takeaway is that I would not use the global fetcher function and instead define fetcher functions in every fetcher store individually. I would then define variables as keys, and just pass them into a generalized
request
function that fetches stuff from GQL endpoint. It's not too much of repetition, but it looks/behaves a bit better.Feel free to ask more stuff. In general, if you have
nanoquery
-related questions, you should…