import { GraphQLClient, RequestDocument } from 'graphql-request';
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
import { GraphQLClientRequestHeaders, VariablesAndRequestHeadersArgs } from 'graphql-request/build/esm/types';
import { Variables } from 'graphql-request';
import { graphql } from '../gql';
import retry from 'async-retry';

class BaseFunctions {
    client: GraphQLClient;

    constructor(client: GraphQLClient) {
        this.client = client;
    }

    async request<T, V extends Variables = Variables>(
        query: DocumentNode<T,V>, 
        ...variablesAndRequestHeaders: VariablesAndRequestHeadersArgs<V> 
    ): Promise<T> {
        return retry(
            async () => {
                return this.client.request(query, ...variablesAndRequestHeaders);
            },
            {
                retries: 5,
                minTimeout: 1000, 
                randomize: true,
                factor: 2,
            }
        );
    }
}

export { BaseFunctions };
