@graphql-tools/executor-http
Version:
A set of utils for faster development of GraphQL tools
176 lines (172 loc) • 7.69 kB
text/typescript
import { ExecutionRequest, IncrementalSpec, DisposableSyncExecutor, DisposableAsyncExecutor } from '@graphql-tools/utils';
import { OperationDefinitionNode, GraphQLResolveInfo, DocumentNode } from 'graphql';
declare const isLiveQueryOperationDefinitionNode: (node: OperationDefinitionNode) => boolean | undefined;
type SyncFetchFn = (url: string, init?: RequestInit, context?: any, info?: GraphQLResolveInfo) => SyncResponse;
type SyncResponse = Omit<Response, 'json' | 'text'> & {
json: () => any;
text: () => string;
};
type AsyncFetchFn = (url: string, options?: RequestInit, context?: any, info?: GraphQLResolveInfo) => Promise<Response> | Response;
type RegularFetchFn = (url: string) => Promise<Response> | Response;
type FetchFn = AsyncFetchFn | SyncFetchFn | RegularFetchFn;
type AsyncImportFn = (moduleName: string) => PromiseLike<any>;
type SyncImportFn = (moduleName: string) => any;
interface HTTPExecutorOptions {
/**
* The endpoint to use when querying the upstream API. Can also be a factory function that returns the
* endpoint based on the `ExecutionRequest` allowing for dynamic endpoints, such as using environment
* variables or other runtime values.
* @default '/graphql'
*/
endpoint?: string | ((executorRequest?: ExecutionRequest) => string);
/**
* The WHATWG compatible fetch implementation to use
* @see https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
* @default globalThis.fetch
*/
fetch?: FetchFn;
/**
* Whether to use the GET HTTP method for queries when querying the original schema
* @default false
*/
useGETForQueries?: boolean;
/**
* Whether to use the GET HTTP method for hashed Automatic Persisted Query requests.
* Full query fallbacks and mutations continue to use POST.
* @default false
*/
useGETForHashedQueries?: boolean;
/**
* Whether to set `content-type: application/json` on GET requests when one isn't already provided.
* This can be useful for compatibility with servers that require a preflighted GET request.
* @default false
*/
useContentTypeForGETRequests?: boolean;
/**
* Additional headers to include when querying the original schema
*/
headers?: HeadersConfig | ((executorRequest?: ExecutionRequest) => HeadersConfig);
/**
* HTTP method to use when querying the original schema.x
* @default 'POST'
*/
method?: 'GET' | 'POST';
/**
* Timeout in milliseconds
*/
timeout?: number | ((request: ExecutionRequest) => number | undefined);
/**
* Request Credentials
* @default 'same-origin'
* @see https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
*/
credentials?: RequestCredentials;
/**
* Retry attempts
*/
retry?: number;
/**
* WHATWG compatible `File` implementation
* @see https://developer.mozilla.org/en-US/docs/Web/API/File
*/
File?: typeof File;
/**
* WHATWG compatible `FormData` implementation
* @see https://developer.mozilla.org/en-US/docs/Web/API/FormData
*/
FormData?: typeof FormData;
/**
* Print function for `DocumentNode`
* Useful when you want to memoize the print function or use a different implementation to minify the query etc.
*/
print?: (doc: DocumentNode) => string;
/**
* Enable Automatic Persisted Queries
* @see https://www.apollographql.com/docs/apollo-server/performance/apq/
*/
apq?: boolean;
/**
* On dispose abort error
*/
getDisposeReason?(): Error | undefined;
/**
* Whether to deduplicate inflight requests with the same parameters.
* This can be useful to avoid making multiple identical requests to the upstream service when
* multiple parts of the gateway are requesting the same data at the same time.
*
* Requests are deduplicated only within the same executor and the same `context` object,
* because the `fetch` implementation receives the `context` and may handle each request
* differently. See `deduplicateInflightRequestsPerContext` to deduplicate across contexts.
*
* @default true
*/
deduplicateInflightRequests?: boolean;
/**
* Whether inflight requests are deduplicated only within the same `context` object.
*
* When `false`, identical requests are deduplicated across different contexts within the
* same executor. Use this only when the `fetch` implementation does not handle requests
* differently based on the `context`, because the `context` of the first request will be
* used for all deduplicated requests.
*
* Hive Gateway's HTTP transport always sets this to `false` so that identical subgraph
* requests from different clients are deduplicated.
*
* @default true
*/
deduplicateInflightRequestsPerContext?: boolean;
/**
* This option allows you to include the response detauls in the result extensions when a request fails.
* This can be useful for debugging and error handling purposes, as it provides additional context about the response that led to the error.
* However, be cautious when enabling this option, as response headers may contain sensitive information.
*
* @default false
*/
exposeHTTPDetailsInExtensions?: boolean;
/**
* How to deliver `multipart/mixed` incremental (`@defer` / `@stream`) results.
*
* - `true` (default): merge each part into a growing snapshot (legacy consumers)
* - `false`: yield each part as received, matching the default schema executor
*
* Hive Gateway's HTTP transport always sets this to `false`.
*
* @default true
*/
mergeIncremental?: boolean;
/**
* Force a specific incremental delivery Accept parameter on upstream requests.
*
* Mapped through `INCREMENTAL_DELIVERY_SPECS` from `@graphql-tools/utils`:
* - `'v0.1'` → `multipart/mixed;deferSpec=20220824`
* - `'v0.2'` → `multipart/mixed;incrementalSpec=v0.2`
*
* When omitted (default), send **bare** `multipart/mixed` (no spec parameter)
* so the subgraph can return whichever format it supports. Also honored from
* `ExecutionRequest.incrementalSpec` per request.
*
* Gateway client responses are negotiated separately by Yoga from the
* incoming client `Accept` header; this option only affects upstream calls.
*/
incrementalSpec?: IncrementalSpec;
}
type HeadersConfig = Record<string, string>;
interface InflightRequestOptions {
url: string;
method: string;
headers: Record<string, string>;
body?: string | FormData;
credentials?: RequestCredentials;
signal?: AbortSignal;
}
declare function buildHTTPExecutor(options?: Omit<HTTPExecutorOptions, 'fetch'> & {
fetch: SyncFetchFn;
}): DisposableSyncExecutor<any, HTTPExecutorOptions>;
declare function buildHTTPExecutor(options?: Omit<HTTPExecutorOptions, 'fetch'> & {
fetch: AsyncFetchFn;
}): DisposableAsyncExecutor<any, HTTPExecutorOptions>;
declare function buildHTTPExecutor(options?: Omit<HTTPExecutorOptions, 'fetch'> & {
fetch: RegularFetchFn;
}): DisposableAsyncExecutor<any, HTTPExecutorOptions>;
declare function buildHTTPExecutor(options?: Omit<HTTPExecutorOptions, 'fetch'>): DisposableAsyncExecutor<any, HTTPExecutorOptions>;
export { type AsyncFetchFn, type AsyncImportFn, type FetchFn, type HTTPExecutorOptions, type HeadersConfig, type InflightRequestOptions, type RegularFetchFn, type SyncFetchFn, type SyncImportFn, type SyncResponse, buildHTTPExecutor, isLiveQueryOperationDefinitionNode };