GraphQL requests are simply POST requests made to your GraphQL endpoint. Why, oh why, does it need all this overhead of setting up an Apollo Client...
For further actions, you may consider blocking this person and/or reporting abuse
Hi, this article looks nice for me. The only reason for using Apollo Client of mine is "caching", nothing else.
I have used graphql-request for my recently cloud functions and I like it's small.
But I'm using Gatsby, with a lot of issue related to SSR. Have you success with Vercel SWR in Gatsby? And how about the caching strategy between Apollo Client and Vercel SWR?
For me, I just need very simple caching strategy and setup also.
Thanks for nice article again :D
Hey Shina!
SWR is the caching solution while
graphql-request
will take care of making the actual GraphQL requests.I have no experience with using Vercel SWR in Gatsby but I have used it with Next.js (which also implements SSR).
Okay thanks, let me deep dive into and see <3 keep moving on.
Where did you find out about this syntax:
The examples in the graphql-request repo look like your apollo client version:
Hi Stuart! The two are the same (GraphQL syntax is the same no matter which client you use.
The
getMovie
example requires a parameter (title
) which is why you see it written like that.Is that what you were talking about?
If that is the case, then I think there is a mistake in your code in the article, because they do not match. Or I'm not understanding, because I expected the two examples to be compare/contrast of the different clients.
You're 100% right! As a compare/contract article, I should keep the code consistent. Thank you for pointing that out!
hi,
I am learning Nextjs with Graphql.
const ALL_PRODUCTS2 = /* GraphQL */I have a problem when using useSWR to fetch data from Graphql api.
;`query allProducts($skip: String!, $take: String!) {
allProducts(skip: $skip, take: $take) {
id
name
}
}
const fetcher = (query: any, first: string, take: string) => request('localhost:3000/api/graphql', query, {first, take});
export default function Products() {
const {data} = useSWR([ALL_PRODUCTS2, '0', '3'], fetcher)
const loading: boolean = !data;
.......
}`
=> I get the error :
ClientError: Variable "$skip" of required type "String!" was not provided.: {"response":{"errors":[{"message":"Variable \"$skip\" of required type \"String!\" was not provided.","locations":[{"line":2,"column":23}]}],"status":500},"request":{"query":"\n query allProducts($skip: String!, $take: String!) {\n allProducts(skip: $skip, take: $take) {\n id\n name\n }\n }\n","variables":{"first":"0","take":"3"}}}
Please help me to fix it. thank you!!!
I am trying migrate from apolo to swr, and follow your instruction but... With apolo, it works.
Thank you!
Hi @hongducphan ,
You passed 'first' instead of 'skip' in the fetcher function. That is the problem. Renaming it should fix the issue.
Hi Aryan, excellent work. I am considering switching from Apollo to graphql-request as well. But graphql-request does not seem to have pagination support for relay style cursor. Do you have any idea on how to enable pagination for the above solution you are proposing?
Hey Aryan, thanks for the article! Quick question, for authentication, what's your recommended way of doing it? Do we have to use graphql-request? Or any other way of doing it? Does SWR have authentication?
Hey! This is the first I've heard of it. Looks right up my alley. I'll try it in a future project!
Thanks Aryan!! your post was very helpful!! i can believe how helpful swr & graphql-request are!
Any way to implement GraphQL with useSWRInfinite?
Hello, Aryan. Great article, congratz! Do you make your server-side requests using SWR as well, or did you migrate only the client-side requests?
I make all GraphQL requests using graphql-request. SWR is only for caching (and only for the React client).
I followed the same path (Apollo => SWR + graphql-request). It fits perfectly into my workflow. Also, it seems to be slightly quicker than Apollo.
Thank you for your article, Aryan.
also, let me suggest:
const loading = !error && !data