Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
gql-client mutation error handling 🔨
Browse files Browse the repository at this point in the history
  • Loading branch information
srttk committed Oct 11, 2020
1 parent 0bd51b8 commit 21cbf95
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 57 deletions.
8 changes: 4 additions & 4 deletions src/lib/db.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { PrismaClient } from '@prisma/client'
import { PrismaClient } from '@prisma/client';

const log = []; // ['query', 'info', 'warn']

const db = new PrismaClient({
log
})
log
});

export default db
export default db;
114 changes: 61 additions & 53 deletions src/lib/gql-client.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,82 @@
import useSWR, { mutate } from 'swr';
import { useState } from 'react';
import { GraphQLClient } from 'graphql-request'
import { GraphQLClient } from 'graphql-request';
import { RequestDocument, Variables } from 'graphql-request/dist/types';

const GQL_BASE_URL = process.env.NEXT_PUBLIC_GRAPHQL_BASE
const GQL_BASE_URL = process.env.NEXT_PUBLIC_GRAPHQL_BASE;

interface QueryState {
data?:any
loading: boolean
error: any
refresh(): Promise<any>
data?: any;
loading: boolean;
error: any;
refresh(): Promise<any>;
}

interface RequestHeaders {
[key:string]: string
[key: string]: string;
}

export const GqlClient = new GraphQLClient(GQL_BASE_URL, {});

export function useQuery(
gqlQuery: RequestDocument,
variables: Variables = {}
): QueryState {
const key = [gqlQuery];
key.push(JSON.stringify(variables));

const { data, error } = useSWR(key, (query, variables) =>
rawRequest(query, variables)
);
return {
data,
loading: !error && !data,
error,
refresh: () => mutate(key)
};
}

export const GqlClient = new GraphQLClient(GQL_BASE_URL, {})

export function useQuery (gqlQuery: RequestDocument, variables:Variables = {}): QueryState {

const key = [gqlQuery];
key.push(JSON.stringify(variables))

const { data, error } = useSWR(key, (query, variables) => rawRequest(query, variables))
return {
data,
loading: !error && !data,
error,
refresh: () => mutate(key)
}
}

export function rawRequest(query:RequestDocument, variables?:Variables,headers: null | RequestHeaders = null ) {

if(headers) {
GqlClient.setHeaders(headers)
export function rawRequest(
query: RequestDocument,
variables?: Variables,
headers: null | RequestHeaders = null
) {
if (headers) {
GqlClient.setHeaders(headers);
}
return GqlClient.request(query, variables)
return GqlClient.request(query, variables);
}


interface IQueryState<T> {
loading: boolean
refetch?: () => Promise<T | void>
data?: T
errors?: any
loading: boolean;
refetch?: () => Promise<T | void>;
data?: T;
errors?: any;
}

interface IMutationState<T> {
loading: boolean
data?: T
errors?: object[]
loading: boolean;
data?: T;
errors?: object[];
}

export function useMutation<T>(query:RequestDocument):[IMutationState<T>, (vars: Variables) => Promise<T | void>] {

const [state, setState] = useState<IQueryState<T>>({ loading: false })

const execute = (variables:Variables) => {
setState({loading: true })
return rawRequest(query, variables).then(data => {
setState({data, loading:false})
return data
}).catch(error => {
setState({errors:error, loading:false})
})
}

return [state, execute]

}
export function useMutation<T>(
query: RequestDocument
): [IMutationState<T>, (vars: Variables) => Promise<T | void>] {
const [state, setState] = useState<IQueryState<T>>({ loading: false });

const execute = (variables: Variables = {}) => {
setState({ loading: true });
return rawRequest(query, variables)
.then((data) => {
setState({ data, loading: false });
return data;
})
.catch((error) => {
setState({ errors: error, loading: false });
return Promise.reject(error);
});
};

return [state, execute];
}

0 comments on commit 21cbf95

Please sign in to comment.