aboutsummaryrefslogtreecommitdiffstats
path: root/src/services/graphql/api.ts
blob: a5be026d26ba5b14c467d49d1d517912071a8407 (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
import { RequestType, VariablesType } from '@ts/types/app';
import { settings } from '@utils/config';
import { GraphQLClient } from 'graphql-request';

export const getGraphQLClient = (): GraphQLClient => {
  const apiUrl = settings.api.url;

  if (!apiUrl) throw new Error('API URL not defined.');

  return new GraphQLClient(apiUrl);
};

export const fetchApi = async <T extends RequestType>(
  query: string,
  variables: VariablesType<T>
): Promise<T> => {
  const client = getGraphQLClient();

  try {
    return await client.request(query, variables);
  } catch (error) {
    console.error(error, undefined, 2);
    process.exit(1);
  }
};