Back to blogs
Blog
Writing in public
Longer posts — published here first, then cross-posted to Dev.to with a canonical URL back to this site.
Blog
Longer posts — published here first, then cross-posted to Dev.to with a canonical URL back to this site.
Back to blogs
Back to blogs
Postreactquery, graphql, github, api
Graphql mutations using the github graphql api with react-query we'll use...
Date published

Modify your nextauth client and add a callbacks section that will get the access token from the...

Why You Might Still Pick Next.js Over TanStack Start From a TanStack Start...

A complete theme management system for React applications with SSR support! ✨ 🌟...
we'll use follow and unfollow user mutations it takes in parmeter
1input:FollowUserInput|UnfollowUserInput
which has the fields
1{userId:String! ,clientMutationId:String}
so we'll be passing in the userId which we'll get from the user as id , we can ignore clientMutationId since it's not required
1import gql from "graphql-tag";23export const FOLLOWUSER = gql`4 mutation followUser($input: FollowUserInput!) {5 followUser(input: $input) {6 clientMutationId7 }8 }9`;1011export const UNFOLLOWUSER = gql`12mutation unfollowUser($input:UnfollowUserInput!){13 unfollowUser(input:$input){14 clientMutationId15 }16}17`;
1import { useMutation } from "react-query";2import { GraphQLClient } from "graphql-request";3import { DocumentNode } from "graphql";45export const useGQLmutation = (6 token: string,7 mutation: DocumentNode,8 config = {}9) => {10 const endpoint = "https://api.github.com/graphql";11 const headers = {12 headers: {13 Authorization: `Bearer ${token}`,14 "Content-Type": "application/json",15 },16 };17 const graphQLClient = new GraphQLClient(endpoint, headers);18 const fetchData = async (vars: any) => {19 return await graphQLClient.request(mutation,vars);20 };2122 return useMutation((vars:any) => {return fetchData(vars)},config);2324};25
1const followMutation = useGQLmutation(token,FOLLOWUSER)2const unfollowMutation = useGQLmutation(token,UNFOLLOWUSER)3const [yes, setYes] = useState<any>(dev?.viewerIsFollowing);45const followThem = (their_id: string) => {6 setYes(true);7 // followUser(their_name, token);8 followMutation.mutate({input:{userId:their_id}})9 };10 const unfollowThem = (their_id: string) => {11 setYes(false);12 // unfollowUser(their_name, token);13 unfollowMutation.mutate({input:{userId:their_id}})14 };15