Skip to content

Commit

Permalink
Improve typings
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Nov 9, 2022
1 parent 95704c7 commit e2fc041
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 34 deletions.
10 changes: 10 additions & 0 deletions .changeset/fast-doors-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@graphql-tools/executor-apollo-link': patch
'@graphql-tools/executor-graphql-ws': patch
'@graphql-tools/executor-http': patch
'@graphql-tools/executor-legacy-ws': patch
'@graphql-tools/executor-urql-exchange': patch
'@graphql-tools/url-loader': patch
---

Improve typings
35 changes: 12 additions & 23 deletions packages/executors/http/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export type AsyncImportFn = (moduleName: string) => PromiseLike<any>;
export type SyncImportFn = (moduleName: string) => any;

export interface HTTPExecutorOptions {
endpoint?: string;
fetch?: FetchFn;
/**
* Whether to use the GET HTTP method for queries when querying the original schema
*/
Expand Down Expand Up @@ -65,37 +67,24 @@ export interface HTTPExecutorOptions {

export type HeadersConfig = Record<string, string>;

export interface ExecutionExtensions {
headers?: HeadersConfig;
endpoint?: string;
}

export function buildHTTPExecutor(
endpoint: string,
syncFetchFn: SyncFetchFn,
options?: HTTPExecutorOptions
): SyncExecutor<any, ExecutionExtensions>;
options?: Omit<HTTPExecutorOptions, 'fetch'> & { fetch: SyncFetchFn }
): SyncExecutor<any, HTTPExecutorOptions>;

export function buildHTTPExecutor(
endpoint: string,
asyncFetchFn: AsyncFetchFn,
options?: HTTPExecutorOptions
): AsyncExecutor<any, ExecutionExtensions>;
options?: Omit<HTTPExecutorOptions, 'fetch'> & { fetch: AsyncFetchFn }
): AsyncExecutor<any, HTTPExecutorOptions>;

export function buildHTTPExecutor(
initialEndpoint: string,
fetchFn: FetchFn = defaultFetch,
options?: HTTPExecutorOptions
): Executor<any, ExecutionExtensions> {
const defaultMethod = options?.method || 'POST';
const executor = (request: ExecutionRequest<any, any, any, ExecutionExtensions>) => {
export function buildHTTPExecutor(options?: HTTPExecutorOptions): Executor<any, HTTPExecutorOptions> {
const executor = (request: ExecutionRequest<any, any, any, HTTPExecutorOptions>) => {
const fetchFn = request.extensions?.fetch ?? options?.fetch ?? defaultFetch;
const controller = cancelNeeded() ? new AbortController() : undefined;
let method = defaultMethod;
let method = request.extensions?.method || options?.method || 'POST';

const operationAst = getOperationASTFromRequest(request);
const operationType = operationAst.operation;

if (options?.useGETForQueries && operationType === 'query') {
if (options?.useGETForQueries || (request.extensions?.useGETForQueries && operationType === 'query')) {
method = 'GET';
}

Expand All @@ -105,7 +94,7 @@ export function buildHTTPExecutor(
accept = 'text/event-stream';
}

const endpoint = request.extensions?.endpoint || initialEndpoint;
const endpoint = request.extensions?.endpoint || options?.endpoint || '/graphql';
const headers = Object.assign(
{
accept,
Expand Down
6 changes: 3 additions & 3 deletions packages/executors/http/src/prepareGETUrl.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { stripIgnoredCharacters } from 'graphql';

export function prepareGETUrl({
baseUrl,
baseUrl = '',
query,
variables,
operationName,
extensions,
}: {
baseUrl: string;
query: string;
variables: any;
variables?: any;
operationName?: string;
extensions?: any;
}) {
const dummyHostname = 'https://dummyhostname.com';
const validUrl = baseUrl.startsWith('http')
? baseUrl
: baseUrl.startsWith('/')
: baseUrl?.startsWith('/')
? `${dummyHostname}${baseUrl}`
: `${dummyHostname}/${baseUrl}`;
const urlObj = new URL(validUrl);
Expand Down
6 changes: 3 additions & 3 deletions packages/executors/urql-exchange/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export function executorExchange(executor: Executor): Exchange {
context: operation.context,
extensions: {
endpoint: operation.context.url,
customFetch: operation.context.fetch,
useGETForQueries: !!operation.context.preferGetMethod,
fetch: operation.context.fetch,
useGETForQueries: operation.context.preferGetMethod,
headers: extraFetchOptions?.headers,
method: extraFetchOptions?.method as 'GET' | 'POST',
method: extraFetchOptions?.method,
},
};
return make<OperationResult<TData>>(observer => {
Expand Down
14 changes: 9 additions & 5 deletions packages/loaders/url/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,27 +123,31 @@ function isCompatibleUri(uri: string): boolean {
export class UrlLoader implements Loader<LoadFromUrlOptions> {
buildHTTPExecutor(
endpoint: string,
fetch: SyncFetchFn,
fetchFn: SyncFetchFn,
options?: LoadFromUrlOptions
): SyncExecutor<any, ExecutionExtensions>;

buildHTTPExecutor(
endpoint: string,
fetch: AsyncFetchFn,
fetchFn: AsyncFetchFn,
options?: LoadFromUrlOptions
): AsyncExecutor<any, ExecutionExtensions>;

buildHTTPExecutor(
initialEndpoint: string,
fetch: FetchFn,
fetchFn: FetchFn,
options?: LoadFromUrlOptions
): Executor<any, ExecutionExtensions> {
const HTTP_URL = switchProtocols(initialEndpoint, {
wss: 'https',
ws: 'http',
});

return buildHTTPExecutor(HTTP_URL, fetch as any, options);
return buildHTTPExecutor({
endpoint: HTTP_URL,
fetch: fetchFn as any,
...options,
});
}

buildWSExecutor(
Expand Down Expand Up @@ -241,7 +245,7 @@ export class UrlLoader implements Loader<LoadFromUrlOptions> {

buildSubscriptionExecutor(
subscriptionsEndpoint: string,
fetch: FetchFn,
fetch: AsyncFetchFn | SyncFetchFn,
importFn: AsyncImportFn | SyncImportFn,
options?: LoadFromUrlOptions
): Executor {
Expand Down

0 comments on commit e2fc041

Please sign in to comment.