Created
August 26, 2024 07:52
-
-
Save dreitagebart/d3ade40ebc0db24d4495aeab6d9086e1 to your computer and use it in GitHub Desktop.
custom fetcher for next js and react-query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import request from 'graphql-request' | |
import { type TypedDocumentNode } from '@graphql-typed-document-node/core' | |
import { | |
QueryClient, | |
useMutation, | |
useQuery, | |
useQueryClient, | |
type UseQueryResult | |
} from '@tanstack/react-query' | |
export const useGraphQLQuery = <TResult, TVariables>( | |
document: TypedDocumentNode<TResult, TVariables>, | |
...[variables]: TVariables extends Record<string, never> ? [] : [TVariables] | |
): UseQueryResult<TResult> => { | |
return useQuery({ | |
queryKey: [(document.definitions[0] as any).name.value, variables], | |
queryFn: async ({ queryKey }) => { | |
return request( | |
'http://localhost:3000/api/graphql', | |
document, | |
queryKey[1] ? queryKey[1] : undefined | |
) | |
} | |
}) | |
} | |
export const useGraphQLMutation = <TResult, TVariables>( | |
document: TypedDocumentNode<TResult, TVariables>, | |
...[variables]: TVariables extends object | undefined ? [] : [TVariables] | |
) => { | |
const mutation = useMutation<TResult, Error, TVariables>({ | |
mutationKey: [(document.definitions[0] as any).name.value, variables], | |
mutationFn: async (variables: any) => { | |
return request('http://localhost:3000/api/graphql', document, variables) | |
} | |
}) | |
return mutation | |
} | |
export const useQueryInvalidation = <TResult, TVariables>( | |
document: TypedDocumentNode<TResult, TVariables>, | |
...[variables]: TVariables extends Record<string, never> ? [] : [TVariables] | |
) => { | |
const queryClient = useQueryClient() | |
return () => | |
queryClient.invalidateQueries({ | |
queryKey: [(document.definitions[0] as any).name.value, variables] | |
}) | |
} | |
export const prefetchQuery = async <TResult, TVariables>( | |
client: QueryClient, | |
document: TypedDocumentNode<TResult, TVariables>, | |
...[variables]: TVariables extends Record<string, never> ? [] : [TVariables] | |
) => { | |
await client.prefetchQuery({ | |
queryKey: [(document.definitions[0] as any).name.value, variables], | |
queryFn: async ({ queryKey }) => { | |
return request( | |
'http://localhost:3000/api/graphql', | |
document, | |
queryKey[1] ? queryKey[1] : undefined | |
) | |
} | |
}) | |
} | |
export const useSetQueryData = <TResult, TVariables>( | |
document: TypedDocumentNode<TResult, TVariables> | |
) => { | |
const queryClient = useQueryClient() | |
return (data: TResult, variables?: TVariables) => { | |
queryClient.setQueryData( | |
[(document.definitions[0] as any).name.value, variables], | |
data | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment