Created
August 21, 2023 00:27
-
-
Save renatarko/44316935700bade92994225693462648 to your computer and use it in GitHub Desktop.
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
| const HASURA_GRAPHQL_URL = process.env.HASURA_GRAPHQL_URL as string; | |
| const NEXT_PUBLIC_HASURA_GRAPHQL_URL = process.env.NEXT_PUBLIC_HASURA_GRAPHQL_URL as string; | |
| const HASURA_GRAPHQL_ADMIN_SECRET = process.env.HASURA_GRAPHQL_ADMIN_SECRET as string; | |
| const requestOptions = { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| 'x-hasura-admin-secret': HASURA_GRAPHQL_ADMIN_SECRET, | |
| 'x-hasura-role': 'admin' | |
| } | |
| }; | |
| const requestOptions2 = { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| // 'x-hasura-admin-secret': HASURA_GRAPHQL_ADMIN_SECRET, | |
| 'x-hasura-role': 'anonymous' | |
| } | |
| }; | |
| const request = async <T>( | |
| query: string, | |
| variables: Record<string, any> = {}, | |
| revalidate = 0 | |
| ): Promise<T> => { | |
| try { | |
| const body = { | |
| query, | |
| variables, | |
| ...(revalidate ? { next: { revalidate } } : {}) | |
| }; | |
| const response = await fetch(NEXT_PUBLIC_HASURA_GRAPHQL_URL, { | |
| ...requestOptions, | |
| body: JSON.stringify(body) | |
| }); | |
| if (!response.ok) { | |
| throw new Error(`GraphQL request error: ${response.statusText}`); | |
| } | |
| const responseData = await response.json(); | |
| return responseData as T; | |
| } catch (error: any) { | |
| throw new Error(`GraphQL request error: ${error.message}`); | |
| } | |
| }; | |
| const req = async <T>( | |
| query: string, | |
| variables: Record<string, any> = {}, | |
| revalidate = 0 | |
| ): Promise<T> => { | |
| try { | |
| const body = { | |
| query, | |
| variables, | |
| ...(revalidate ? { next: { revalidate } } : {}) | |
| }; | |
| const response = await fetch(NEXT_PUBLIC_HASURA_GRAPHQL_URL, { | |
| ...requestOptions2, | |
| body: JSON.stringify(body) | |
| }); | |
| if (!response.ok) { | |
| throw new Error(`GraphQL request error: ${response.statusText}`); | |
| } | |
| const responseData = await response.json(); | |
| return responseData as T; | |
| } catch (error: any) { | |
| throw new Error(`GraphQL request error: ${error.message}`); | |
| } | |
| }; | |
| const mutate = async <T>(mutation: string, variables: Record<string, any> = {}): Promise<T> => { | |
| try { | |
| const body = { | |
| query: mutation, | |
| variables | |
| }; | |
| const response = await fetch(HASURA_GRAPHQL_URL, { | |
| ...requestOptions, | |
| body: JSON.stringify(body) | |
| }); | |
| if (!response.ok) { | |
| throw new Error(`GraphQL request error: ${response.statusText}`); | |
| } | |
| const responseData = await response.json(); | |
| return responseData as T; | |
| } catch (error: any) { | |
| throw new Error(`GraphQL request error: ${error.message}`); | |
| } | |
| }; | |
| export const client = { | |
| request, | |
| req, | |
| mutate | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment