import { GraphQLClient } from '../classes/GraphQLClient.js' import type { GraphQLClientResponse, RawRequestOptions, Variables, VariablesAndRequestHeadersArgs, } from '../helpers/types.js' /** * Send a GraphQL Query to the GraphQL server for execution. */ export const rawRequest: RawRequest = async ( ...args: RawRequestArgs ): Promise> => { const [urlOrOptions, query, ...variablesAndRequestHeaders] = args const requestOptions = parseRawRequestExtendedArgs(urlOrOptions, query, ...variablesAndRequestHeaders) const client = new GraphQLClient(requestOptions.url) return client.rawRequest({ ...requestOptions, }) } // dprint-ignore interface RawRequest { (url: string, query: string, ...variablesAndRequestHeaders: VariablesAndRequestHeadersArgs): Promise> (options: RawRequestExtendedOptions): Promise> } // dprint-ignore type RawRequestArgs = | [options: RawRequestExtendedOptions, query?: string, ...variablesAndRequestHeaders: VariablesAndRequestHeadersArgs] | [url: string, query?: string, ...variablesAndRequestHeaders: VariablesAndRequestHeadersArgs] export const parseRawRequestExtendedArgs = ( urlOrOptions: string | RawRequestExtendedOptions, query?: string, ...variablesAndRequestHeaders: VariablesAndRequestHeadersArgs ): RawRequestExtendedOptions => { const [variables, requestHeaders] = variablesAndRequestHeaders return typeof urlOrOptions === `string` ? ({ url: urlOrOptions, query: query as string, variables, requestHeaders, signal: undefined, } as unknown as RawRequestExtendedOptions) : urlOrOptions } export type RawRequestExtendedOptions = { url: string } & RawRequestOptions export const parseRawRequestArgs = ( queryOrOptions: string | RawRequestOptions, variables?: V, requestHeaders?: HeadersInit, ): RawRequestOptions => { return (queryOrOptions as RawRequestOptions).query ? (queryOrOptions as RawRequestOptions) : ({ query: queryOrOptions as string, variables: variables, requestHeaders: requestHeaders, signal: undefined, } as unknown as RawRequestOptions) }