The first time I tried GraphQL was when I would still have considered myself as just a back-end developer. About two years ago I gave myself the op...
For further actions, you may consider blocking this person and/or reporting abuse
I feel apollo is overrated. Since I used REST in the past, I am more familiar with fetch and Axios. Now I have been using Graphql for few projects, and fetch works just fine. Though I haven't used
swr
in the past, now I will.Part of the reason of this post is that a lot of people coming from REST will find Apollo as their first choice if they look for GraphQL. I'm trying to say fetch (and of course Axios) is perfectly fine for this!
swr
offers caching, which is one of the features people choose Apollo for. That's why I mentioned it 😁Thanks for your comment!
If you need good caching but dont want to have apollo, then minimalist GraphQL client urql
That's actually what I like to use if I feel the need for a more complete library! I've only had good experiences with it.
My first lesson in GraphQL used Apollo on the client-side. It was probably too much to start with since I was also learning how to use React and how to build the GraphQL API, as well. Apollo just added this extra thing to learn. I never felt that I could use anything else, but just a simple fetch for json data seems like such a good use case to simply learning. Especially if you are just trying to learn how to set up the GraphQL APIs.
Exactly my story for a long time!
Using fetch should even be enough for a lot of more "serious" apps. And if you want caching,
swr
already gives that to you in a much lighter package.Hey, I was following your suggestion of useSWR in graphql, but I got stuck. I want to send a graphql request onPress event.
I tried the this,
Didn't work. How do I send Graphql request onPress() with useSWR??
Hey! I don't see anything particularly wrong on what you're showing me here. So whatever is causing you an issue should be somewhere else?
I just quickly made you a Codesandbox showing how to do this here.
Note that
useMemo
is necessary here since it prevents a newvariables
object from being created ifname
does not change. If it weren't there, every time the component re-rendered a new request to the GraphQL API would be made creating an infinite loop in some cases.Thanks for the tip to use
useMemo
. I was getting into an infinite loop as well, and had no idea what to do.Thank you so much, It helped a lot. I was not using useMemo, and getting in the loop.
I'm glad I could help!
Yeah that's a little detail to remember. Since
useSWR
shallowly compares each argument, and each newly created object's reference is different, you have to make sure to pass the same reference (not create a new object) if you don't want a re-fetch.