From fa6adedc42e9c6ec39cc30df16b54900c220b094 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Mon, 20 Dec 2021 00:15:20 +0100 Subject: refactor: rewrite types and services I was repeating myself a lot in services. So I rewrited the different functions to improve readability and I extracted some formatting functions to put them in utils. I also rewrited/reorganized some types to keep consistent names. --- src/services/graphql/api.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/services/graphql/api.ts (limited to 'src/services/graphql/api.ts') diff --git a/src/services/graphql/api.ts b/src/services/graphql/api.ts new file mode 100644 index 0000000..de8024f --- /dev/null +++ b/src/services/graphql/api.ts @@ -0,0 +1,27 @@ +import { RequestType, VariablesType } from '@ts/types/app'; +import { GraphQLClient } from 'graphql-request'; + +export const getGraphQLClient = (): GraphQLClient => { + const apiUrl: string = process.env.NEXT_PUBLIC_GRAPHQL_API || ''; + + if (!apiUrl) throw new Error('API URL not defined.'); + + const graphQLClient = new GraphQLClient(apiUrl); + + return graphQLClient; +}; + +export const fetchApi = async ( + query: string, + variables: VariablesType +): Promise => { + const client = getGraphQLClient(); + + try { + const response = await client.request(query, variables); + return response; + } catch (error) { + console.error(error, undefined, 2); + process.exit(1); + } +}; -- cgit v1.2.3