aboutsummaryrefslogtreecommitdiffstats
path: root/src/services/graphql/api.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/services/graphql/api.ts')
-rw-r--r--src/services/graphql/api.ts27
1 files changed, 27 insertions, 0 deletions
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 <T extends RequestType>(
+ query: string,
+ variables: VariablesType<T>
+): Promise<T> => {
+ const client = getGraphQLClient();
+
+ try {
+ const response = await client.request(query, variables);
+ return response;
+ } catch (error) {
+ console.error(error, undefined, 2);
+ process.exit(1);
+ }
+};