Skip to content

Commit

Permalink
feat(gqwery): fetch executor (#243)
Browse files Browse the repository at this point in the history
* feat(gqwery): fetch executor

* refactor: remove ability to specify a fetchFn
  • Loading branch information
nmathew98 committed Apr 6, 2024
1 parent 3cabb13 commit 5766f9f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
44 changes: 44 additions & 0 deletions packages/gqwery/src/executor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import {
import type { IResolvers } from "@graphql-tools/utils";
import { type DocumentNode, print } from "graphql";
import { makeExecutableSchema } from "@graphql-tools/schema";
import {
buildHTTPExecutor,
type HTTPExecutorOptions,
} from "@graphql-tools/executor-http";
export { buildHTTPExecutor } from "@graphql-tools/executor-http";

export interface CombinedExecutorOptions {
[OperationTypeNode.QUERY]: Executor;
Expand Down Expand Up @@ -58,6 +63,45 @@ export const buildCombinedExecutor = (options: CombinedExecutorOptions) => {
};
};

export interface BuildFetchExecutorOptions
extends Omit<HTTPExecutorOptions, "timeout" | "fetch"> {
signal?: AbortSignal;
}

export const buildFetchExecutor = (options?: BuildFetchExecutorOptions) => {
const abortableFetch = new Proxy(fetch, {
apply: (fetch, thisArg, args) => {
const fetchOptions: RequestInit = {
...args.at(-1),
signal: options?.signal,
};

return Reflect.apply(fetch, thisArg, [args[0], fetchOptions]);
},
});

const executor = buildHTTPExecutor({
...options,
fetch: abortableFetch,
});

return async <R extends Record<string, any> = any>(
executorRequest: ExecutionRequest,
) => {
const result = await executor(executorRequest);

if (isAsyncIterable(result)) {
return result;
}

if (result.errors) {
throw new AggregateError(result.errors);
}

return result.data as R;
};
};

export interface BuildMockExecutorOptions<TResolvers = IResolvers> {
typeDefs: string | DocumentNode;
store?: IMockStore;
Expand Down
7 changes: 3 additions & 4 deletions packages/gqwery/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ export { createQueryKey, isQueryKey } from "./cache/query-key";
export {
buildCombinedExecutor,
buildMockExecutor,
buildFetchExecutor,
type BuildMockExecutorOptions,
} from "./executor";

export { buildGraphQLWSExecutor } from "@graphql-tools/executor-graphql-ws";
export { buildHTTPExecutor } from "@graphql-tools/executor-http";
export { RequestManager } from "./executor/request-manager";

export { buildGraphQLWSExecutor as buildWebSocketExecutor } from "@graphql-tools/executor-graphql-ws";

export { OperationTypeNode as OperationType } from "graphql";
export { OperationTypeNode } from "graphql";

0 comments on commit 5766f9f

Please sign in to comment.