DEV Community

You don't really need Apollo

Pablo Berganza on June 10, 2020

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...
Collapse
 
viralsangani profile image
Viral Sangani

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.

Collapse
 
pabloabc profile image
Pablo Berganza

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!

Collapse
 
itsjzt profile image
Saurabh Sharma

If you need good caching but dont want to have apollo, then minimalist GraphQL client urql

Collapse
 
pabloabc profile image
Pablo Berganza

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.

Collapse
 
jakesweb profile image
Jacob Colborn

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.

Collapse
 
pabloabc profile image
Pablo Berganza • Edited

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.

Collapse
 
viralsangani profile image
Viral Sangani • Edited

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,

const [shouldFetch, setShouldFetch] = React.useState(false)
Enter fullscreen mode Exit fullscreen mode
const { data } = useSWR(
    shouldFetch
        ? [query, { firstName, lastName, email, password1, password2 }]
        : null,
        gqlFetcher
    )
Enter fullscreen mode Exit fullscreen mode

Didn't work. How do I send Graphql request onPress() with useSWR??

Collapse
 
pabloabc profile image
Pablo Berganza • Edited

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 new variables object from being created if name 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.

Collapse
 
sortbyrandom profile image
sort by random

Thanks for the tip to use useMemo. I was getting into an infinite loop as well, and had no idea what to do.

Collapse
 
viralsangani profile image
Viral Sangani

Thank you so much, It helped a lot. I was not using useMemo, and getting in the loop.

Thread Thread
 
pabloabc profile image
Pablo Berganza

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.