blob: de8024fb2d50bc282e691f5a4f9505b6351bd007 (
plain)
| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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);
  }
};
 |