aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/helpers/graphql.ts
blob: e07b151968e6ac505e7a0907db9f6674d1890a22 (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import type { Nullable } from '../../types';
import { CONFIG } from '../config';

/**
 * Retrieve the API url from settings.
 *
 * @returns {string} The API url.
 */
export const getGraphQLUrl = (): string => {
  if (!CONFIG.api.url) throw new Error('You forgot to define the API url.');

  return CONFIG.api.url;
};

export type GraphQLData<T> = Record<string, Nullable<T>>;

type GraphQLResponse<T extends GraphQLData<unknown>> = {
  data: T;
  errors?: { message: string }[];
};

export type FetchGraphQLConfig = {
  query: string;
  url: string;
  variables?: Record<string, unknown>;
};

/**
 * Retrieve GraphQL data using fetch.
 *
 * @template T - The expected data type.
 * @param {FetchGraphQLConfig} config - A configuration object.
 * @returns {Promise<T>} The data.
 */
export const fetchGraphQL = async <
  T extends GraphQLData<unknown> = GraphQLData<unknown>,
>({
  query,
  url,
  variables,
}: FetchGraphQLConfig): Promise<T> => {
  const response = await fetch(url, {
    method: 'POST',
    headers: {
      'content-type': 'application/json;charset=UTF-8',
    },
    body: JSON.stringify({
      query,
      variables,
    }),
  });

  const { data, errors }: GraphQLResponse<T> = await response.json();

  if (!response.ok) {
    const error = new Error(
      errors?.map((e) => e.message).join('\n') ?? 'Network response was not OK'
    );

    return Promise.reject(error);
  }

  return data;
};