Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gqwery): fetch executor #243

Merged
merged 2 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";