diff options
Diffstat (limited to 'source/library/fetcher')
| -rw-r--r-- | source/library/fetcher/http.ts | 30 | ||||
| -rw-r--r-- | source/library/fetcher/sse.ts | 18 | ||||
| -rw-r--r-- | source/library/fetcher/types.ts | 20 | ||||
| -rw-r--r-- | source/library/fetcher/websocket.ts | 18 |
4 files changed, 86 insertions, 0 deletions
diff --git a/source/library/fetcher/http.ts b/source/library/fetcher/http.ts new file mode 100644 index 0000000..3138226 --- /dev/null +++ b/source/library/fetcher/http.ts @@ -0,0 +1,30 @@ + + + +/*** UTILITY ------------------------------------------ ***/ + +import type { Fetcher, FetcherOptions } from "./types.ts"; + +/*** EXPORT ------------------------------------------- ***/ + +export function createHttpFetcher(options: FetcherOptions): Fetcher { + const fetchImpl = options.fetch ?? globalThis.fetch; + + return async (req) => { + const response = await fetchImpl(options.url, { + body: JSON.stringify({ + operationName: req.operationName, + query: req.query, + variables: req.variables + }), + headers: { + "Content-Type": "application/json", + ...options.headers, + ...req.headers + }, + method: "POST" + }); + + return await response.json(); + }; +} diff --git a/source/library/fetcher/sse.ts b/source/library/fetcher/sse.ts new file mode 100644 index 0000000..b6805dc --- /dev/null +++ b/source/library/fetcher/sse.ts @@ -0,0 +1,18 @@ + + + +/*** UTILITY ------------------------------------------ ***/ + +import type { Fetcher, FetcherOptions } from "./types.ts"; + +/*** EXPORT ------------------------------------------- ***/ + +/** + * Server-Sent Events fetcher for graphql-sse protocol. + * Stub implementation — see PLAN.md stage v0.4 for full implementation. + */ +export function createSseFetcher(_options: FetcherOptions): Fetcher { + return () => { + throw new Error("SSE fetcher not yet implemented — see PLAN.md v0.4"); + }; +} diff --git a/source/library/fetcher/types.ts b/source/library/fetcher/types.ts new file mode 100644 index 0000000..af849a9 --- /dev/null +++ b/source/library/fetcher/types.ts @@ -0,0 +1,20 @@ + + + +/*** EXPORT ------------------------------------------- ***/ + +export type FetcherRequest = { + headers?: Record<string, string>; + operationName?: string | null; + query: string; + variables?: Record<string, unknown>; +}; + +export type FetcherResult = Record<string, unknown>; +export type Fetcher = (req: FetcherRequest) => Promise<FetcherResult> | AsyncIterable<FetcherResult>; + +export type FetcherOptions = { + fetch?: typeof globalThis.fetch; + headers?: Record<string, string>; + url: string; +}; diff --git a/source/library/fetcher/websocket.ts b/source/library/fetcher/websocket.ts new file mode 100644 index 0000000..6376e76 --- /dev/null +++ b/source/library/fetcher/websocket.ts @@ -0,0 +1,18 @@ + + + +/*** UTILITY ------------------------------------------ ***/ + +import type { Fetcher, FetcherOptions } from "./types.ts"; + +/*** EXPORT ------------------------------------------- ***/ + +/** + * WebSocket fetcher for graphql-ws protocol. + * Stub implementation — see PLAN.md stage v0.4 for full implementation. + */ +export function createWsFetcher(_options: FetcherOptions): Fetcher { + return () => { + throw new Error("WebSocket fetcher not yet implemented — see PLAN.md v0.4"); + }; +} |