2023-12-05 14:46:20 +00:00
|
|
|
import { GraphQLClient, Variables } from 'graphql-request'
|
|
|
|
|
import { TypedDocumentNode } from '@graphql-typed-document-node/core'
|
|
|
|
|
import { GraphQLResponse } from 'graphql-request/build/esm/types'
|
2023-11-22 10:53:36 +00:00
|
|
|
|
|
|
|
|
export const gqlClient = new GraphQLClient(
|
|
|
|
|
`${process.env.NEXT_PUBLIC_TABBY_SERVER_URL ?? ''}/graphql`
|
|
|
|
|
)
|
2023-12-05 14:46:20 +00:00
|
|
|
|
|
|
|
|
export interface ValidationError {
|
|
|
|
|
path: string,
|
|
|
|
|
message: string,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ValidationErrors {
|
|
|
|
|
errors: Array<ValidationError>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useGraphQLForm<TResult, TVariables extends Variables | undefined>(
|
|
|
|
|
document: TypedDocumentNode<TResult, TVariables>,
|
|
|
|
|
options?: {
|
|
|
|
|
onError?: (path: string, message: string) => void,
|
|
|
|
|
}
|
|
|
|
|
) {
|
|
|
|
|
const onSubmit = async (values: TVariables) => {
|
|
|
|
|
try {
|
|
|
|
|
await gqlClient.request(document, values)
|
|
|
|
|
} catch (err) {
|
|
|
|
|
const { errors = [] } = (err as any).response as GraphQLResponse
|
|
|
|
|
for (const error of errors) {
|
|
|
|
|
if (error.extensions && error.extensions["validation-errors"]) {
|
|
|
|
|
const validationErrors = error.extensions["validation-errors"] as ValidationErrors
|
|
|
|
|
for (const error of validationErrors.errors) {
|
|
|
|
|
options?.onError && options?.onError(error.path, error.message)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
options?.onError && options?.onError("root", error.message)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return { onSubmit };
|
|
|
|
|
}
|