buildgql 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +375 -0
- package/dist/adapters/apollo.cjs +23 -0
- package/dist/adapters/apollo.d.cts +28 -0
- package/dist/adapters/apollo.d.ts +28 -0
- package/dist/adapters/apollo.js +23 -0
- package/dist/adapters/urql.cjs +16 -0
- package/dist/adapters/urql.d.cts +26 -0
- package/dist/adapters/urql.d.ts +26 -0
- package/dist/adapters/urql.js +16 -0
- package/dist/chunk-2Z2AKLA3.cjs +267 -0
- package/dist/chunk-BM7L4MCF.cjs +27 -0
- package/dist/chunk-RSDTCILU.js +267 -0
- package/dist/chunk-SNFCCROD.js +27 -0
- package/dist/cli/bin.js +842 -0
- package/dist/cli/config.cjs +272 -0
- package/dist/cli/config.d.cts +73 -0
- package/dist/cli/config.d.ts +73 -0
- package/dist/cli/config.js +245 -0
- package/dist/client/index.cjs +16 -0
- package/dist/client/index.d.cts +93 -0
- package/dist/client/index.d.ts +93 -0
- package/dist/client/index.js +16 -0
- package/dist/document-BZ9RdxXB.d.cts +16 -0
- package/dist/document-BZ9RdxXB.d.ts +16 -0
- package/dist/index.cjs +313 -0
- package/dist/index.d.cts +85 -0
- package/dist/index.d.ts +85 -0
- package/dist/index.js +313 -0
- package/dist/operation-DxbEION8.d.cts +200 -0
- package/dist/operation-DxbEION8.d.ts +200 -0
- package/package.json +148 -0
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/client/errors.ts
|
|
2
|
+
var BuildGQLError = class extends Error {
|
|
3
|
+
};
|
|
4
|
+
var BuildGQLResponseError = class extends BuildGQLError {
|
|
5
|
+
constructor(errors, data) {
|
|
6
|
+
super(`buildgql: ${errors.map((e) => e.message).join("; ") || "GraphQL request failed"}`);
|
|
7
|
+
this.name = "BuildGQLResponseError";
|
|
8
|
+
this.errors = errors;
|
|
9
|
+
this.data = data;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var BuildGQLHttpError = class extends BuildGQLError {
|
|
13
|
+
constructor(status, body) {
|
|
14
|
+
super(`buildgql: GraphQL request failed with HTTP ${status}`);
|
|
15
|
+
this.name = "BuildGQLHttpError";
|
|
16
|
+
this.status = status;
|
|
17
|
+
this.body = body;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// src/client/headers.ts
|
|
22
|
+
async function resolveHeaders(source) {
|
|
23
|
+
if (!source) return {};
|
|
24
|
+
return typeof source === "function" ? await source() : source;
|
|
25
|
+
}
|
|
26
|
+
function mergeHeaders(base, override) {
|
|
27
|
+
const headers = new Headers(base);
|
|
28
|
+
for (const [name, value] of new Headers(_nullishCoalesce(override, () => ( {})))) headers.set(name, value);
|
|
29
|
+
return headers;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/client/create-client.ts
|
|
33
|
+
function createClient(options) {
|
|
34
|
+
const doFetch = _nullishCoalesce(options.fetch, () => ( globalThis.fetch));
|
|
35
|
+
if (typeof doFetch !== "function") {
|
|
36
|
+
throw new Error("buildgql: no fetch implementation available \u2014 pass one via createClient({ fetch })");
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
options,
|
|
40
|
+
async execute(op, ...rest) {
|
|
41
|
+
const [vars, opts] = rest;
|
|
42
|
+
const headers = mergeHeaders(await resolveHeaders(options.headers), _optionalChain([opts, 'optionalAccess', _ => _.headers]));
|
|
43
|
+
headers.set("content-type", "application/json");
|
|
44
|
+
if (!headers.has("accept")) headers.set("accept", "application/json");
|
|
45
|
+
const res = await doFetch(options.url, {
|
|
46
|
+
method: "POST",
|
|
47
|
+
headers,
|
|
48
|
+
// `RequestInit.signal` is `AbortSignal | null`; `??` bridges our `undefined`.
|
|
49
|
+
signal: _nullishCoalesce(_optionalChain([opts, 'optionalAccess', _2 => _2.signal]), () => ( null)),
|
|
50
|
+
body: JSON.stringify({
|
|
51
|
+
query: op.document,
|
|
52
|
+
operationName: op.name,
|
|
53
|
+
variables: _nullishCoalesce(vars, () => ( {}))
|
|
54
|
+
})
|
|
55
|
+
});
|
|
56
|
+
if (!res.ok) throw new BuildGQLHttpError(res.status, await res.text().catch(() => ""));
|
|
57
|
+
const raw = await res.text();
|
|
58
|
+
let payload;
|
|
59
|
+
try {
|
|
60
|
+
payload = JSON.parse(raw);
|
|
61
|
+
} catch (e2) {
|
|
62
|
+
throw new BuildGQLHttpError(res.status, raw);
|
|
63
|
+
}
|
|
64
|
+
if (payload.errors && payload.errors.length > 0) {
|
|
65
|
+
throw new BuildGQLResponseError(payload.errors, payload.data);
|
|
66
|
+
}
|
|
67
|
+
return payload.data;
|
|
68
|
+
},
|
|
69
|
+
subscribe(op, ...rest) {
|
|
70
|
+
const transport = options.subscriptions;
|
|
71
|
+
if (!transport) {
|
|
72
|
+
throw new Error("buildgql: createClient({ subscriptions }) is required to run subscriptions");
|
|
73
|
+
}
|
|
74
|
+
const [vars, opts] = rest;
|
|
75
|
+
const controller = new AbortController();
|
|
76
|
+
if (_optionalChain([opts, 'optionalAccess', _3 => _3.signal])) {
|
|
77
|
+
if (opts.signal.aborted) controller.abort();
|
|
78
|
+
else opts.signal.addEventListener("abort", () => controller.abort());
|
|
79
|
+
}
|
|
80
|
+
const payload = {
|
|
81
|
+
query: op.document,
|
|
82
|
+
operationName: op.name,
|
|
83
|
+
variables: _nullishCoalesce(vars, () => ( {}))
|
|
84
|
+
};
|
|
85
|
+
return {
|
|
86
|
+
async *[Symbol.asyncIterator]() {
|
|
87
|
+
const base = typeof options.headers === "function" ? await options.headers() : _nullishCoalesce(options.headers, () => ( {}));
|
|
88
|
+
const headers = mergeHeaders(base, _optionalChain([opts, 'optionalAccess', _4 => _4.headers]));
|
|
89
|
+
for await (const chunk of transport.subscribe(payload, controller.signal, headers)) {
|
|
90
|
+
if (chunk.errors && chunk.errors.length > 0) {
|
|
91
|
+
throw new BuildGQLResponseError(chunk.errors, chunk.data);
|
|
92
|
+
}
|
|
93
|
+
yield chunk.data;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/client/sse-transport.ts
|
|
102
|
+
async function* sseEvents(body) {
|
|
103
|
+
const reader = body.getReader();
|
|
104
|
+
const decoder = new TextDecoder();
|
|
105
|
+
let buffer = "";
|
|
106
|
+
for (; ; ) {
|
|
107
|
+
const { done, value } = await reader.read();
|
|
108
|
+
if (done) break;
|
|
109
|
+
buffer += decoder.decode(value, { stream: true }).replace(/\r\n/g, "\n");
|
|
110
|
+
let sep = buffer.indexOf("\n\n");
|
|
111
|
+
while (sep !== -1) {
|
|
112
|
+
const raw = buffer.slice(0, sep);
|
|
113
|
+
buffer = buffer.slice(sep + 2);
|
|
114
|
+
let event = "message";
|
|
115
|
+
const dataLines = [];
|
|
116
|
+
for (const line of raw.split("\n")) {
|
|
117
|
+
if (line.startsWith("event:")) event = line.slice(6).trim();
|
|
118
|
+
else if (line.startsWith("data:")) dataLines.push(line.slice(5).trim());
|
|
119
|
+
}
|
|
120
|
+
yield { event, data: dataLines.join("\n") };
|
|
121
|
+
sep = buffer.indexOf("\n\n");
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
function sseTransport(opts) {
|
|
126
|
+
const doFetch = _nullishCoalesce(opts.fetch, () => ( globalThis.fetch));
|
|
127
|
+
return {
|
|
128
|
+
async *subscribe(payload, signal, callHeaders) {
|
|
129
|
+
const headers = mergeHeaders(await resolveHeaders(opts.headers), callHeaders);
|
|
130
|
+
headers.set("content-type", "application/json");
|
|
131
|
+
headers.set("accept", "text/event-stream");
|
|
132
|
+
const res = await doFetch(opts.url, {
|
|
133
|
+
method: "POST",
|
|
134
|
+
headers,
|
|
135
|
+
body: JSON.stringify(payload),
|
|
136
|
+
signal
|
|
137
|
+
});
|
|
138
|
+
if (!res.ok || !res.body) {
|
|
139
|
+
throw new BuildGQLHttpError(res.status, await res.text().catch(() => ""));
|
|
140
|
+
}
|
|
141
|
+
let completed = false;
|
|
142
|
+
for await (const evt of sseEvents(res.body)) {
|
|
143
|
+
if (evt.event === "complete") {
|
|
144
|
+
completed = true;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
if (evt.event !== "next") continue;
|
|
148
|
+
yield JSON.parse(evt.data);
|
|
149
|
+
}
|
|
150
|
+
if (!completed) {
|
|
151
|
+
throw new Error("buildgql: subscription stream ended before completing");
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// src/client/async-queue.ts
|
|
158
|
+
var AsyncQueue = class {
|
|
159
|
+
constructor() {
|
|
160
|
+
this.items = [];
|
|
161
|
+
this.done = false;
|
|
162
|
+
this.failure = null;
|
|
163
|
+
this.wake = null;
|
|
164
|
+
}
|
|
165
|
+
notify() {
|
|
166
|
+
const wake = this.wake;
|
|
167
|
+
this.wake = null;
|
|
168
|
+
_optionalChain([wake, 'optionalCall', _5 => _5()]);
|
|
169
|
+
}
|
|
170
|
+
/** No-op once the queue has been closed or has failed — the item is discarded, not buffered. */
|
|
171
|
+
push(item) {
|
|
172
|
+
if (this.done) return;
|
|
173
|
+
this.items.push(item);
|
|
174
|
+
this.notify();
|
|
175
|
+
}
|
|
176
|
+
/** Records a terminal error and closes. The first failure wins; later ones are ignored. */
|
|
177
|
+
fail(error) {
|
|
178
|
+
if (this.done) return;
|
|
179
|
+
this.failure = error;
|
|
180
|
+
this.done = true;
|
|
181
|
+
this.notify();
|
|
182
|
+
}
|
|
183
|
+
close() {
|
|
184
|
+
if (this.done) return;
|
|
185
|
+
this.done = true;
|
|
186
|
+
this.notify();
|
|
187
|
+
}
|
|
188
|
+
async *[Symbol.asyncIterator]() {
|
|
189
|
+
for (; ; ) {
|
|
190
|
+
while (this.items.length > 0) yield this.items.shift();
|
|
191
|
+
if (this.failure) throw this.failure;
|
|
192
|
+
if (this.done) return;
|
|
193
|
+
await new Promise((resolve) => {
|
|
194
|
+
this.wake = resolve;
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
// src/client/ws-transport.ts
|
|
201
|
+
function wsTransport(opts) {
|
|
202
|
+
const WS = _nullishCoalesce(opts.WebSocket, () => ( globalThis.WebSocket));
|
|
203
|
+
return {
|
|
204
|
+
// The standard `WebSocket` constructor has no way to set custom HTTP headers, so
|
|
205
|
+
// per-subscription headers (the third `subscribe` parameter) cannot be honored
|
|
206
|
+
// here — authenticate via `connectionParams` instead, which travels in the
|
|
207
|
+
// `connection_init` message body.
|
|
208
|
+
subscribe(payload, signal) {
|
|
209
|
+
const queue = new AsyncQueue();
|
|
210
|
+
let completed = false;
|
|
211
|
+
let aborted = false;
|
|
212
|
+
const socket = new WS(opts.url, "graphql-transport-ws");
|
|
213
|
+
const id = "1";
|
|
214
|
+
socket.onopen = async () => {
|
|
215
|
+
try {
|
|
216
|
+
const params = typeof opts.connectionParams === "function" ? await opts.connectionParams() : opts.connectionParams;
|
|
217
|
+
if (socket.readyState !== WS.OPEN) return;
|
|
218
|
+
socket.send(JSON.stringify({ type: "connection_init", payload: _nullishCoalesce(params, () => ( {})) }));
|
|
219
|
+
} catch (err) {
|
|
220
|
+
queue.fail(err instanceof Error ? err : new Error(String(err)));
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
socket.onmessage = (evt) => {
|
|
224
|
+
const msg = JSON.parse(String(evt.data));
|
|
225
|
+
if (msg.type === "connection_ack") {
|
|
226
|
+
socket.send(JSON.stringify({ id, type: "subscribe", payload }));
|
|
227
|
+
} else if (msg.type === "next" && msg.payload) {
|
|
228
|
+
queue.push(msg.payload);
|
|
229
|
+
} else if (msg.type === "error") {
|
|
230
|
+
const errors = Array.isArray(msg.payload) ? msg.payload : [];
|
|
231
|
+
queue.fail(new BuildGQLResponseError(errors, void 0));
|
|
232
|
+
} else if (msg.type === "complete") {
|
|
233
|
+
completed = true;
|
|
234
|
+
queue.close();
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
socket.onerror = () => {
|
|
238
|
+
queue.fail(new Error("buildgql: subscription socket error"));
|
|
239
|
+
};
|
|
240
|
+
socket.onclose = () => {
|
|
241
|
+
if (!completed && !aborted) {
|
|
242
|
+
queue.fail(new Error("buildgql: subscription stream ended before completing"));
|
|
243
|
+
}
|
|
244
|
+
queue.close();
|
|
245
|
+
};
|
|
246
|
+
signal.addEventListener("abort", () => {
|
|
247
|
+
aborted = true;
|
|
248
|
+
try {
|
|
249
|
+
socket.send(JSON.stringify({ id, type: "complete" }));
|
|
250
|
+
} catch (e3) {
|
|
251
|
+
}
|
|
252
|
+
socket.close();
|
|
253
|
+
queue.close();
|
|
254
|
+
});
|
|
255
|
+
return queue;
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
exports.BuildGQLError = BuildGQLError; exports.BuildGQLResponseError = BuildGQLResponseError; exports.BuildGQLHttpError = BuildGQLHttpError; exports.createClient = createClient; exports.sseTransport = sseTransport; exports.wsTransport = wsTransport;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }// src/adapters/document.ts
|
|
2
|
+
var _graphql = require('graphql');
|
|
3
|
+
var cache = /* @__PURE__ */ new WeakMap();
|
|
4
|
+
function toDocument(op) {
|
|
5
|
+
const hit = cache.get(op);
|
|
6
|
+
if (hit) return hit;
|
|
7
|
+
const doc = _graphql.parse.call(void 0, op.document);
|
|
8
|
+
cache.set(op, doc);
|
|
9
|
+
return doc;
|
|
10
|
+
}
|
|
11
|
+
function assertKind(op, allowed, helper) {
|
|
12
|
+
if (!allowed.includes(op.kind)) {
|
|
13
|
+
throw new Error(
|
|
14
|
+
`buildgql: ${helper}() expects a ${allowed.join(" or ")} operation, but "${op.name}" is a ${op.kind}.`
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function variablesOf(rest) {
|
|
19
|
+
const [vars] = rest;
|
|
20
|
+
return _nullishCoalesce(vars, () => ( {}));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
exports.toDocument = toDocument; exports.assertKind = assertKind; exports.variablesOf = variablesOf;
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
// src/client/errors.ts
|
|
2
|
+
var BuildGQLError = class extends Error {
|
|
3
|
+
};
|
|
4
|
+
var BuildGQLResponseError = class extends BuildGQLError {
|
|
5
|
+
constructor(errors, data) {
|
|
6
|
+
super(`buildgql: ${errors.map((e) => e.message).join("; ") || "GraphQL request failed"}`);
|
|
7
|
+
this.name = "BuildGQLResponseError";
|
|
8
|
+
this.errors = errors;
|
|
9
|
+
this.data = data;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var BuildGQLHttpError = class extends BuildGQLError {
|
|
13
|
+
constructor(status, body) {
|
|
14
|
+
super(`buildgql: GraphQL request failed with HTTP ${status}`);
|
|
15
|
+
this.name = "BuildGQLHttpError";
|
|
16
|
+
this.status = status;
|
|
17
|
+
this.body = body;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// src/client/headers.ts
|
|
22
|
+
async function resolveHeaders(source) {
|
|
23
|
+
if (!source) return {};
|
|
24
|
+
return typeof source === "function" ? await source() : source;
|
|
25
|
+
}
|
|
26
|
+
function mergeHeaders(base, override) {
|
|
27
|
+
const headers = new Headers(base);
|
|
28
|
+
for (const [name, value] of new Headers(override ?? {})) headers.set(name, value);
|
|
29
|
+
return headers;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/client/create-client.ts
|
|
33
|
+
function createClient(options) {
|
|
34
|
+
const doFetch = options.fetch ?? globalThis.fetch;
|
|
35
|
+
if (typeof doFetch !== "function") {
|
|
36
|
+
throw new Error("buildgql: no fetch implementation available \u2014 pass one via createClient({ fetch })");
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
options,
|
|
40
|
+
async execute(op, ...rest) {
|
|
41
|
+
const [vars, opts] = rest;
|
|
42
|
+
const headers = mergeHeaders(await resolveHeaders(options.headers), opts?.headers);
|
|
43
|
+
headers.set("content-type", "application/json");
|
|
44
|
+
if (!headers.has("accept")) headers.set("accept", "application/json");
|
|
45
|
+
const res = await doFetch(options.url, {
|
|
46
|
+
method: "POST",
|
|
47
|
+
headers,
|
|
48
|
+
// `RequestInit.signal` is `AbortSignal | null`; `??` bridges our `undefined`.
|
|
49
|
+
signal: opts?.signal ?? null,
|
|
50
|
+
body: JSON.stringify({
|
|
51
|
+
query: op.document,
|
|
52
|
+
operationName: op.name,
|
|
53
|
+
variables: vars ?? {}
|
|
54
|
+
})
|
|
55
|
+
});
|
|
56
|
+
if (!res.ok) throw new BuildGQLHttpError(res.status, await res.text().catch(() => ""));
|
|
57
|
+
const raw = await res.text();
|
|
58
|
+
let payload;
|
|
59
|
+
try {
|
|
60
|
+
payload = JSON.parse(raw);
|
|
61
|
+
} catch {
|
|
62
|
+
throw new BuildGQLHttpError(res.status, raw);
|
|
63
|
+
}
|
|
64
|
+
if (payload.errors && payload.errors.length > 0) {
|
|
65
|
+
throw new BuildGQLResponseError(payload.errors, payload.data);
|
|
66
|
+
}
|
|
67
|
+
return payload.data;
|
|
68
|
+
},
|
|
69
|
+
subscribe(op, ...rest) {
|
|
70
|
+
const transport = options.subscriptions;
|
|
71
|
+
if (!transport) {
|
|
72
|
+
throw new Error("buildgql: createClient({ subscriptions }) is required to run subscriptions");
|
|
73
|
+
}
|
|
74
|
+
const [vars, opts] = rest;
|
|
75
|
+
const controller = new AbortController();
|
|
76
|
+
if (opts?.signal) {
|
|
77
|
+
if (opts.signal.aborted) controller.abort();
|
|
78
|
+
else opts.signal.addEventListener("abort", () => controller.abort());
|
|
79
|
+
}
|
|
80
|
+
const payload = {
|
|
81
|
+
query: op.document,
|
|
82
|
+
operationName: op.name,
|
|
83
|
+
variables: vars ?? {}
|
|
84
|
+
};
|
|
85
|
+
return {
|
|
86
|
+
async *[Symbol.asyncIterator]() {
|
|
87
|
+
const base = typeof options.headers === "function" ? await options.headers() : options.headers ?? {};
|
|
88
|
+
const headers = mergeHeaders(base, opts?.headers);
|
|
89
|
+
for await (const chunk of transport.subscribe(payload, controller.signal, headers)) {
|
|
90
|
+
if (chunk.errors && chunk.errors.length > 0) {
|
|
91
|
+
throw new BuildGQLResponseError(chunk.errors, chunk.data);
|
|
92
|
+
}
|
|
93
|
+
yield chunk.data;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/client/sse-transport.ts
|
|
102
|
+
async function* sseEvents(body) {
|
|
103
|
+
const reader = body.getReader();
|
|
104
|
+
const decoder = new TextDecoder();
|
|
105
|
+
let buffer = "";
|
|
106
|
+
for (; ; ) {
|
|
107
|
+
const { done, value } = await reader.read();
|
|
108
|
+
if (done) break;
|
|
109
|
+
buffer += decoder.decode(value, { stream: true }).replace(/\r\n/g, "\n");
|
|
110
|
+
let sep = buffer.indexOf("\n\n");
|
|
111
|
+
while (sep !== -1) {
|
|
112
|
+
const raw = buffer.slice(0, sep);
|
|
113
|
+
buffer = buffer.slice(sep + 2);
|
|
114
|
+
let event = "message";
|
|
115
|
+
const dataLines = [];
|
|
116
|
+
for (const line of raw.split("\n")) {
|
|
117
|
+
if (line.startsWith("event:")) event = line.slice(6).trim();
|
|
118
|
+
else if (line.startsWith("data:")) dataLines.push(line.slice(5).trim());
|
|
119
|
+
}
|
|
120
|
+
yield { event, data: dataLines.join("\n") };
|
|
121
|
+
sep = buffer.indexOf("\n\n");
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
function sseTransport(opts) {
|
|
126
|
+
const doFetch = opts.fetch ?? globalThis.fetch;
|
|
127
|
+
return {
|
|
128
|
+
async *subscribe(payload, signal, callHeaders) {
|
|
129
|
+
const headers = mergeHeaders(await resolveHeaders(opts.headers), callHeaders);
|
|
130
|
+
headers.set("content-type", "application/json");
|
|
131
|
+
headers.set("accept", "text/event-stream");
|
|
132
|
+
const res = await doFetch(opts.url, {
|
|
133
|
+
method: "POST",
|
|
134
|
+
headers,
|
|
135
|
+
body: JSON.stringify(payload),
|
|
136
|
+
signal
|
|
137
|
+
});
|
|
138
|
+
if (!res.ok || !res.body) {
|
|
139
|
+
throw new BuildGQLHttpError(res.status, await res.text().catch(() => ""));
|
|
140
|
+
}
|
|
141
|
+
let completed = false;
|
|
142
|
+
for await (const evt of sseEvents(res.body)) {
|
|
143
|
+
if (evt.event === "complete") {
|
|
144
|
+
completed = true;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
if (evt.event !== "next") continue;
|
|
148
|
+
yield JSON.parse(evt.data);
|
|
149
|
+
}
|
|
150
|
+
if (!completed) {
|
|
151
|
+
throw new Error("buildgql: subscription stream ended before completing");
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// src/client/async-queue.ts
|
|
158
|
+
var AsyncQueue = class {
|
|
159
|
+
constructor() {
|
|
160
|
+
this.items = [];
|
|
161
|
+
this.done = false;
|
|
162
|
+
this.failure = null;
|
|
163
|
+
this.wake = null;
|
|
164
|
+
}
|
|
165
|
+
notify() {
|
|
166
|
+
const wake = this.wake;
|
|
167
|
+
this.wake = null;
|
|
168
|
+
wake?.();
|
|
169
|
+
}
|
|
170
|
+
/** No-op once the queue has been closed or has failed — the item is discarded, not buffered. */
|
|
171
|
+
push(item) {
|
|
172
|
+
if (this.done) return;
|
|
173
|
+
this.items.push(item);
|
|
174
|
+
this.notify();
|
|
175
|
+
}
|
|
176
|
+
/** Records a terminal error and closes. The first failure wins; later ones are ignored. */
|
|
177
|
+
fail(error) {
|
|
178
|
+
if (this.done) return;
|
|
179
|
+
this.failure = error;
|
|
180
|
+
this.done = true;
|
|
181
|
+
this.notify();
|
|
182
|
+
}
|
|
183
|
+
close() {
|
|
184
|
+
if (this.done) return;
|
|
185
|
+
this.done = true;
|
|
186
|
+
this.notify();
|
|
187
|
+
}
|
|
188
|
+
async *[Symbol.asyncIterator]() {
|
|
189
|
+
for (; ; ) {
|
|
190
|
+
while (this.items.length > 0) yield this.items.shift();
|
|
191
|
+
if (this.failure) throw this.failure;
|
|
192
|
+
if (this.done) return;
|
|
193
|
+
await new Promise((resolve) => {
|
|
194
|
+
this.wake = resolve;
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
// src/client/ws-transport.ts
|
|
201
|
+
function wsTransport(opts) {
|
|
202
|
+
const WS = opts.WebSocket ?? globalThis.WebSocket;
|
|
203
|
+
return {
|
|
204
|
+
// The standard `WebSocket` constructor has no way to set custom HTTP headers, so
|
|
205
|
+
// per-subscription headers (the third `subscribe` parameter) cannot be honored
|
|
206
|
+
// here — authenticate via `connectionParams` instead, which travels in the
|
|
207
|
+
// `connection_init` message body.
|
|
208
|
+
subscribe(payload, signal) {
|
|
209
|
+
const queue = new AsyncQueue();
|
|
210
|
+
let completed = false;
|
|
211
|
+
let aborted = false;
|
|
212
|
+
const socket = new WS(opts.url, "graphql-transport-ws");
|
|
213
|
+
const id = "1";
|
|
214
|
+
socket.onopen = async () => {
|
|
215
|
+
try {
|
|
216
|
+
const params = typeof opts.connectionParams === "function" ? await opts.connectionParams() : opts.connectionParams;
|
|
217
|
+
if (socket.readyState !== WS.OPEN) return;
|
|
218
|
+
socket.send(JSON.stringify({ type: "connection_init", payload: params ?? {} }));
|
|
219
|
+
} catch (err) {
|
|
220
|
+
queue.fail(err instanceof Error ? err : new Error(String(err)));
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
socket.onmessage = (evt) => {
|
|
224
|
+
const msg = JSON.parse(String(evt.data));
|
|
225
|
+
if (msg.type === "connection_ack") {
|
|
226
|
+
socket.send(JSON.stringify({ id, type: "subscribe", payload }));
|
|
227
|
+
} else if (msg.type === "next" && msg.payload) {
|
|
228
|
+
queue.push(msg.payload);
|
|
229
|
+
} else if (msg.type === "error") {
|
|
230
|
+
const errors = Array.isArray(msg.payload) ? msg.payload : [];
|
|
231
|
+
queue.fail(new BuildGQLResponseError(errors, void 0));
|
|
232
|
+
} else if (msg.type === "complete") {
|
|
233
|
+
completed = true;
|
|
234
|
+
queue.close();
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
socket.onerror = () => {
|
|
238
|
+
queue.fail(new Error("buildgql: subscription socket error"));
|
|
239
|
+
};
|
|
240
|
+
socket.onclose = () => {
|
|
241
|
+
if (!completed && !aborted) {
|
|
242
|
+
queue.fail(new Error("buildgql: subscription stream ended before completing"));
|
|
243
|
+
}
|
|
244
|
+
queue.close();
|
|
245
|
+
};
|
|
246
|
+
signal.addEventListener("abort", () => {
|
|
247
|
+
aborted = true;
|
|
248
|
+
try {
|
|
249
|
+
socket.send(JSON.stringify({ id, type: "complete" }));
|
|
250
|
+
} catch {
|
|
251
|
+
}
|
|
252
|
+
socket.close();
|
|
253
|
+
queue.close();
|
|
254
|
+
});
|
|
255
|
+
return queue;
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export {
|
|
261
|
+
BuildGQLError,
|
|
262
|
+
BuildGQLResponseError,
|
|
263
|
+
BuildGQLHttpError,
|
|
264
|
+
createClient,
|
|
265
|
+
sseTransport,
|
|
266
|
+
wsTransport
|
|
267
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/adapters/document.ts
|
|
2
|
+
import { parse } from "graphql";
|
|
3
|
+
var cache = /* @__PURE__ */ new WeakMap();
|
|
4
|
+
function toDocument(op) {
|
|
5
|
+
const hit = cache.get(op);
|
|
6
|
+
if (hit) return hit;
|
|
7
|
+
const doc = parse(op.document);
|
|
8
|
+
cache.set(op, doc);
|
|
9
|
+
return doc;
|
|
10
|
+
}
|
|
11
|
+
function assertKind(op, allowed, helper) {
|
|
12
|
+
if (!allowed.includes(op.kind)) {
|
|
13
|
+
throw new Error(
|
|
14
|
+
`buildgql: ${helper}() expects a ${allowed.join(" or ")} operation, but "${op.name}" is a ${op.kind}.`
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function variablesOf(rest) {
|
|
19
|
+
const [vars] = rest;
|
|
20
|
+
return vars ?? {};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export {
|
|
24
|
+
toDocument,
|
|
25
|
+
assertKind,
|
|
26
|
+
variablesOf
|
|
27
|
+
};
|