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,272 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/cli/config.ts
|
|
21
|
+
var config_exports = {};
|
|
22
|
+
__export(config_exports, {
|
|
23
|
+
defineConfig: () => defineConfig,
|
|
24
|
+
loadConfig: () => loadConfig,
|
|
25
|
+
resolveOutputDir: () => resolveOutputDir
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(config_exports);
|
|
28
|
+
var import_promises = require("fs/promises");
|
|
29
|
+
var import_node_url = require("url");
|
|
30
|
+
var import_node_path = require("path");
|
|
31
|
+
|
|
32
|
+
// src/codegen/clients.ts
|
|
33
|
+
var CLIENT_KINDS = ["buildgql", "apollo", "urql", "none"];
|
|
34
|
+
function isClientKind(value) {
|
|
35
|
+
return typeof value === "string" && CLIENT_KINDS.includes(value);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// src/cli/assert-scalars.ts
|
|
39
|
+
var SCALAR_KEYS = [
|
|
40
|
+
"name",
|
|
41
|
+
"from",
|
|
42
|
+
"declare",
|
|
43
|
+
"input",
|
|
44
|
+
"output"
|
|
45
|
+
];
|
|
46
|
+
var IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
47
|
+
var RESERVED_NAMES = /* @__PURE__ */ new Set([
|
|
48
|
+
"break",
|
|
49
|
+
"case",
|
|
50
|
+
"catch",
|
|
51
|
+
"class",
|
|
52
|
+
"const",
|
|
53
|
+
"continue",
|
|
54
|
+
"debugger",
|
|
55
|
+
"default",
|
|
56
|
+
"delete",
|
|
57
|
+
"do",
|
|
58
|
+
"else",
|
|
59
|
+
"enum",
|
|
60
|
+
"export",
|
|
61
|
+
"extends",
|
|
62
|
+
"false",
|
|
63
|
+
"finally",
|
|
64
|
+
"for",
|
|
65
|
+
"function",
|
|
66
|
+
"if",
|
|
67
|
+
"import",
|
|
68
|
+
"in",
|
|
69
|
+
"instanceof",
|
|
70
|
+
"new",
|
|
71
|
+
"null",
|
|
72
|
+
"return",
|
|
73
|
+
"super",
|
|
74
|
+
"switch",
|
|
75
|
+
"this",
|
|
76
|
+
"throw",
|
|
77
|
+
"true",
|
|
78
|
+
"try",
|
|
79
|
+
"typeof",
|
|
80
|
+
"var",
|
|
81
|
+
"void",
|
|
82
|
+
"while",
|
|
83
|
+
"with",
|
|
84
|
+
"await",
|
|
85
|
+
"implements",
|
|
86
|
+
"interface",
|
|
87
|
+
"let",
|
|
88
|
+
"package",
|
|
89
|
+
"private",
|
|
90
|
+
"protected",
|
|
91
|
+
"public",
|
|
92
|
+
"static",
|
|
93
|
+
"yield",
|
|
94
|
+
"any",
|
|
95
|
+
"bigint",
|
|
96
|
+
"boolean",
|
|
97
|
+
"never",
|
|
98
|
+
"number",
|
|
99
|
+
"object",
|
|
100
|
+
"string",
|
|
101
|
+
"symbol",
|
|
102
|
+
"undefined",
|
|
103
|
+
"unknown",
|
|
104
|
+
"as"
|
|
105
|
+
]);
|
|
106
|
+
var UNSAFE_IN_QUOTED_SPECIFIER = /['"\n\r\\]/;
|
|
107
|
+
function isPlainRecord(value) {
|
|
108
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
109
|
+
}
|
|
110
|
+
function assertNonEmptyString(configName, scalar, key, value) {
|
|
111
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
112
|
+
throw new Error(`buildgql: ${configName}'s "scalars.${scalar}.${key}" must be a non-empty string`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function assertScalarsConfig(configName, value) {
|
|
116
|
+
if (!isPlainRecord(value)) {
|
|
117
|
+
throw new Error(`buildgql: ${configName}'s "scalars" must be a record keyed by GraphQL scalar name`);
|
|
118
|
+
}
|
|
119
|
+
for (const [scalar, entry] of Object.entries(value)) {
|
|
120
|
+
if (typeof entry === "string") {
|
|
121
|
+
if (entry.length === 0) {
|
|
122
|
+
throw new Error(`buildgql: ${configName}'s "scalars.${scalar}" must not be an empty string`);
|
|
123
|
+
}
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
if (!isPlainRecord(entry)) {
|
|
127
|
+
throw new Error(
|
|
128
|
+
`buildgql: ${configName}'s "scalars.${scalar}" must be a TypeScript type string, or an object with "input"/"output", "name" + "from", or "name" + "declare"`
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
for (const key of Object.keys(entry)) {
|
|
132
|
+
if (!SCALAR_KEYS.includes(key)) {
|
|
133
|
+
throw new Error(
|
|
134
|
+
`buildgql: ${configName}'s "scalars.${scalar}" has an unknown key "${key}" \u2014 expected one of ` + SCALAR_KEYS.map((k) => `"${k}"`).join(", ")
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const { name, from, declare, input, output } = entry;
|
|
139
|
+
if (from !== void 0 && declare !== void 0) {
|
|
140
|
+
throw new Error(
|
|
141
|
+
`buildgql: ${configName}'s "scalars.${scalar}" sets both "from" and "declare" \u2014 a type is either imported or declared, not both`
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
if (from !== void 0) {
|
|
145
|
+
assertNonEmptyString(configName, scalar, "from", from);
|
|
146
|
+
if (UNSAFE_IN_QUOTED_SPECIFIER.test(from)) {
|
|
147
|
+
throw new Error(
|
|
148
|
+
`buildgql: ${configName}'s "scalars.${scalar}.from" ("${from}") contains a character ("'", '"', a backslash, or a newline) that cannot appear there \u2014 "from" is emitted inside a quoted module specifier. Use forward slashes even for a Windows-style path.`
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (declare !== void 0) assertNonEmptyString(configName, scalar, "declare", declare);
|
|
153
|
+
if (input !== void 0) assertNonEmptyString(configName, scalar, "input", input);
|
|
154
|
+
if (output !== void 0) assertNonEmptyString(configName, scalar, "output", output);
|
|
155
|
+
if (name !== void 0) {
|
|
156
|
+
assertNonEmptyString(configName, scalar, "name", name);
|
|
157
|
+
if (!IDENTIFIER.test(name)) {
|
|
158
|
+
throw new Error(
|
|
159
|
+
`buildgql: ${configName}'s "scalars.${scalar}.name" ("${name}") is not a valid TypeScript identifier`
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
if (RESERVED_NAMES.has(name)) {
|
|
163
|
+
throw new Error(
|
|
164
|
+
`buildgql: ${configName}'s "scalars.${scalar}.name" ("${name}") is a reserved word and cannot be spliced into "import type { ${name} }" or "export type ${name} = ..." \u2014 choose a different name`
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
if (from === void 0 && declare === void 0) {
|
|
168
|
+
throw new Error(
|
|
169
|
+
`buildgql: ${configName}'s "scalars.${scalar}" sets "name" but neither "from" nor "declare" \u2014 nothing in the generated module would declare that type`
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
} else if (from !== void 0 || declare !== void 0) {
|
|
173
|
+
throw new Error(
|
|
174
|
+
`buildgql: ${configName}'s "scalars.${scalar}" sets "${from !== void 0 ? "from" : "declare"}" but not "name" \u2014 "name" is what the generated module refers to the type by`
|
|
175
|
+
);
|
|
176
|
+
} else if (input === void 0 && output === void 0) {
|
|
177
|
+
throw new Error(
|
|
178
|
+
`buildgql: ${configName}'s "scalars.${scalar}" is empty \u2014 set "input"/"output", or "name" together with "from" or "declare"`
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// src/cli/config.ts
|
|
185
|
+
function defineConfig(config) {
|
|
186
|
+
return config;
|
|
187
|
+
}
|
|
188
|
+
var CANDIDATES = ["buildgql.config.ts", "buildgql.config.mts", "buildgql.config.mjs", "buildgql.config.js"];
|
|
189
|
+
async function exists(p) {
|
|
190
|
+
try {
|
|
191
|
+
await (0, import_promises.access)(p);
|
|
192
|
+
return true;
|
|
193
|
+
} catch {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
function isRecord(value) {
|
|
198
|
+
return typeof value === "object" && value !== null;
|
|
199
|
+
}
|
|
200
|
+
function isStringRecord(value) {
|
|
201
|
+
return isRecord(value) && Object.values(value).every((v) => typeof v === "string");
|
|
202
|
+
}
|
|
203
|
+
function errorMessage(err) {
|
|
204
|
+
return err instanceof Error ? err.message : String(err);
|
|
205
|
+
}
|
|
206
|
+
function isMissingTypeStrippingSupport(err) {
|
|
207
|
+
return isRecord(err) && (err.code === "ERR_UNKNOWN_FILE_EXTENSION" || err.code === "ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX");
|
|
208
|
+
}
|
|
209
|
+
function assertBuildGQLConfig(name, value) {
|
|
210
|
+
if (typeof value.schema !== "string" || value.schema.length === 0) {
|
|
211
|
+
throw new Error(`buildgql: ${name} is missing "schema" \u2014 set it to a URL, a .json, or a .graphql file`);
|
|
212
|
+
}
|
|
213
|
+
if (value.output !== void 0 && typeof value.output !== "string") {
|
|
214
|
+
throw new Error(`buildgql: ${name}'s "output" must be a string path`);
|
|
215
|
+
}
|
|
216
|
+
if (value.headers !== void 0 && !isStringRecord(value.headers)) {
|
|
217
|
+
throw new Error(`buildgql: ${name}'s "headers" must be a record of string values`);
|
|
218
|
+
}
|
|
219
|
+
if (value.scalars !== void 0) assertScalarsConfig(name, value.scalars);
|
|
220
|
+
if (value.client !== void 0 && !isClientKind(value.client)) {
|
|
221
|
+
throw new Error(
|
|
222
|
+
`buildgql: ${name}'s "client" must be one of ${CLIENT_KINDS.map((k) => `"${k}"`).join(", ")}`
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
var defaultImporter = (url) => import(url);
|
|
227
|
+
async function loadConfig(cwd = process.cwd(), importModule = defaultImporter) {
|
|
228
|
+
let missingTypeStrippingMessage;
|
|
229
|
+
for (const name of CANDIDATES) {
|
|
230
|
+
const path = (0, import_node_path.join)(cwd, name);
|
|
231
|
+
if (!await exists(path)) continue;
|
|
232
|
+
let mod;
|
|
233
|
+
try {
|
|
234
|
+
mod = await importModule((0, import_node_url.pathToFileURL)(path).href);
|
|
235
|
+
} catch (err) {
|
|
236
|
+
if ((name.endsWith(".ts") || name.endsWith(".mts")) && isMissingTypeStrippingSupport(err)) {
|
|
237
|
+
missingTypeStrippingMessage = `buildgql: could not load ${name}. Node must be able to run TypeScript directly (Node >= 22.6 with --experimental-strip-types, or Node >= 23.6). Otherwise rename it to buildgql.config.mjs. Original error: ${errorMessage(err)}`;
|
|
238
|
+
process.stderr.write(
|
|
239
|
+
`buildgql: skipping ${name} \u2014 this Node cannot run TypeScript directly. ${errorMessage(err)}
|
|
240
|
+
`
|
|
241
|
+
);
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
throw new Error(`buildgql: failed to load ${name}: ${errorMessage(err)}`);
|
|
245
|
+
}
|
|
246
|
+
if (!isRecord(mod) || !("default" in mod)) {
|
|
247
|
+
throw new Error(`buildgql: ${name} must have a default export`);
|
|
248
|
+
}
|
|
249
|
+
const config = mod.default;
|
|
250
|
+
if (!isRecord(config)) {
|
|
251
|
+
throw new Error(`buildgql: ${name}'s default export must be an object (use defineConfig({...}))`);
|
|
252
|
+
}
|
|
253
|
+
assertBuildGQLConfig(name, config);
|
|
254
|
+
return { config, path };
|
|
255
|
+
}
|
|
256
|
+
if (missingTypeStrippingMessage) {
|
|
257
|
+
throw new Error(missingTypeStrippingMessage);
|
|
258
|
+
}
|
|
259
|
+
throw new Error(
|
|
260
|
+
`buildgql: no config found in ${cwd}. Create a buildgql.config.ts (or .js) exporting defineConfig({ schema: 'https://...' | './schema.graphql' }).`
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
function resolveOutputDir(config, cwd) {
|
|
264
|
+
const out = config.output ?? "./src/gql";
|
|
265
|
+
return (0, import_node_path.isAbsolute)(out) ? out : (0, import_node_path.resolve)(cwd, out);
|
|
266
|
+
}
|
|
267
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
268
|
+
0 && (module.exports = {
|
|
269
|
+
defineConfig,
|
|
270
|
+
loadConfig,
|
|
271
|
+
resolveOutputDir
|
|
272
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/** Which GraphQL client the generated module binds to. */
|
|
2
|
+
type ClientKind = 'buildgql' | 'apollo' | 'urql' | 'none';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The object form of a `scalars` entry, for scalars a single inline type expression cannot
|
|
6
|
+
* express. Every key is optional, but the combination is not free-form — `assertScalarsConfig`
|
|
7
|
+
* (in `src/cli/assert-scalars.ts`) rejects the shapes this type alone would still allow:
|
|
8
|
+
*
|
|
9
|
+
* - `{ name, from }` — import `name` from `from` and use it in both positions.
|
|
10
|
+
* - `{ name, declare }`— emit `export type <name> = <declare>;` and use it in both positions.
|
|
11
|
+
* - `{ input, output }`— raw type expressions, different per position, nothing declared.
|
|
12
|
+
*
|
|
13
|
+
* `input`/`output` may be combined with `name` to override one position while still importing
|
|
14
|
+
* or declaring the named type (e.g. an input that also accepts the raw wire form).
|
|
15
|
+
* `from` and `declare` are mutually exclusive: a type is either imported or declared.
|
|
16
|
+
*/
|
|
17
|
+
interface ScalarTypeConfig {
|
|
18
|
+
/** The TypeScript type name this scalar maps to. Required with `from` or `declare`. */
|
|
19
|
+
readonly name?: string;
|
|
20
|
+
/** Module specifier `name` is imported from. See the plan's import-path rule. */
|
|
21
|
+
readonly from?: string;
|
|
22
|
+
/** Right-hand side of a type alias emitted as `export type <name> = <declare>;`. */
|
|
23
|
+
readonly declare?: string;
|
|
24
|
+
/** TS type in argument and input-object-field position. Defaults to `name`, else `output`. */
|
|
25
|
+
readonly input?: string;
|
|
26
|
+
/** TS type in result position. Defaults to `name`, else `input`. */
|
|
27
|
+
readonly output?: string;
|
|
28
|
+
}
|
|
29
|
+
/** One `scalars` entry: a raw TypeScript type expression, or the object form above. */
|
|
30
|
+
type ScalarConfig = string | ScalarTypeConfig;
|
|
31
|
+
|
|
32
|
+
interface BuildGQLConfig {
|
|
33
|
+
/** URL, path to an introspection .json, or path to an SDL file. */
|
|
34
|
+
readonly schema: string;
|
|
35
|
+
/** Sent with the introspection request when `schema` is a URL. */
|
|
36
|
+
readonly headers?: Record<string, string>;
|
|
37
|
+
/** Directory for the generated module. Defaults to `./src/gql`. */
|
|
38
|
+
readonly output?: string;
|
|
39
|
+
/**
|
|
40
|
+
* Maps custom GraphQL scalars to TypeScript types. A string is a raw type expression used in
|
|
41
|
+
* both argument and result position (`{ DateTime: 'string' }`); the object form covers types a
|
|
42
|
+
* single expression cannot express — see `ScalarTypeConfig`:
|
|
43
|
+
*
|
|
44
|
+
* ```js
|
|
45
|
+
* scalars: {
|
|
46
|
+
* DateTime: { input: 'string | Date', output: 'string' },
|
|
47
|
+
* Money: { name: 'Money', from: './src/types/money' },
|
|
48
|
+
* JSON: { name: 'JSONValue', declare: 'string | number | boolean | null | JSONValue[] | { [k: string]: JSONValue }' },
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* A relative `from` is written against *this config file* and rewritten against `output`;
|
|
53
|
+
* a package specifier is emitted verbatim, which is what a generated module published as an
|
|
54
|
+
* npm package needs.
|
|
55
|
+
*/
|
|
56
|
+
readonly scalars?: Record<string, ScalarConfig>;
|
|
57
|
+
/**
|
|
58
|
+
* Which GraphQL client the generated module binds to. `'buildgql'` (the default) re-exports
|
|
59
|
+
* buildgql's own `createClient`; `'apollo'` and `'urql'` re-export that client's adapter
|
|
60
|
+
* instead; `'none'` binds no client at all. Adapters require the `graphql` package.
|
|
61
|
+
*/
|
|
62
|
+
readonly client?: ClientKind;
|
|
63
|
+
}
|
|
64
|
+
declare function defineConfig(config: BuildGQLConfig): BuildGQLConfig;
|
|
65
|
+
/** Loads an ES module given its `file:` URL. Overridable in tests only — see `loadConfig`. */
|
|
66
|
+
type ConfigImporter = (url: string) => Promise<unknown>;
|
|
67
|
+
declare function loadConfig(cwd?: string, importModule?: ConfigImporter): Promise<{
|
|
68
|
+
config: BuildGQLConfig;
|
|
69
|
+
path: string;
|
|
70
|
+
}>;
|
|
71
|
+
declare function resolveOutputDir(config: BuildGQLConfig, cwd: string): string;
|
|
72
|
+
|
|
73
|
+
export { type BuildGQLConfig, type ClientKind, type ConfigImporter, type ScalarConfig, type ScalarTypeConfig, defineConfig, loadConfig, resolveOutputDir };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/** Which GraphQL client the generated module binds to. */
|
|
2
|
+
type ClientKind = 'buildgql' | 'apollo' | 'urql' | 'none';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The object form of a `scalars` entry, for scalars a single inline type expression cannot
|
|
6
|
+
* express. Every key is optional, but the combination is not free-form — `assertScalarsConfig`
|
|
7
|
+
* (in `src/cli/assert-scalars.ts`) rejects the shapes this type alone would still allow:
|
|
8
|
+
*
|
|
9
|
+
* - `{ name, from }` — import `name` from `from` and use it in both positions.
|
|
10
|
+
* - `{ name, declare }`— emit `export type <name> = <declare>;` and use it in both positions.
|
|
11
|
+
* - `{ input, output }`— raw type expressions, different per position, nothing declared.
|
|
12
|
+
*
|
|
13
|
+
* `input`/`output` may be combined with `name` to override one position while still importing
|
|
14
|
+
* or declaring the named type (e.g. an input that also accepts the raw wire form).
|
|
15
|
+
* `from` and `declare` are mutually exclusive: a type is either imported or declared.
|
|
16
|
+
*/
|
|
17
|
+
interface ScalarTypeConfig {
|
|
18
|
+
/** The TypeScript type name this scalar maps to. Required with `from` or `declare`. */
|
|
19
|
+
readonly name?: string;
|
|
20
|
+
/** Module specifier `name` is imported from. See the plan's import-path rule. */
|
|
21
|
+
readonly from?: string;
|
|
22
|
+
/** Right-hand side of a type alias emitted as `export type <name> = <declare>;`. */
|
|
23
|
+
readonly declare?: string;
|
|
24
|
+
/** TS type in argument and input-object-field position. Defaults to `name`, else `output`. */
|
|
25
|
+
readonly input?: string;
|
|
26
|
+
/** TS type in result position. Defaults to `name`, else `input`. */
|
|
27
|
+
readonly output?: string;
|
|
28
|
+
}
|
|
29
|
+
/** One `scalars` entry: a raw TypeScript type expression, or the object form above. */
|
|
30
|
+
type ScalarConfig = string | ScalarTypeConfig;
|
|
31
|
+
|
|
32
|
+
interface BuildGQLConfig {
|
|
33
|
+
/** URL, path to an introspection .json, or path to an SDL file. */
|
|
34
|
+
readonly schema: string;
|
|
35
|
+
/** Sent with the introspection request when `schema` is a URL. */
|
|
36
|
+
readonly headers?: Record<string, string>;
|
|
37
|
+
/** Directory for the generated module. Defaults to `./src/gql`. */
|
|
38
|
+
readonly output?: string;
|
|
39
|
+
/**
|
|
40
|
+
* Maps custom GraphQL scalars to TypeScript types. A string is a raw type expression used in
|
|
41
|
+
* both argument and result position (`{ DateTime: 'string' }`); the object form covers types a
|
|
42
|
+
* single expression cannot express — see `ScalarTypeConfig`:
|
|
43
|
+
*
|
|
44
|
+
* ```js
|
|
45
|
+
* scalars: {
|
|
46
|
+
* DateTime: { input: 'string | Date', output: 'string' },
|
|
47
|
+
* Money: { name: 'Money', from: './src/types/money' },
|
|
48
|
+
* JSON: { name: 'JSONValue', declare: 'string | number | boolean | null | JSONValue[] | { [k: string]: JSONValue }' },
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* A relative `from` is written against *this config file* and rewritten against `output`;
|
|
53
|
+
* a package specifier is emitted verbatim, which is what a generated module published as an
|
|
54
|
+
* npm package needs.
|
|
55
|
+
*/
|
|
56
|
+
readonly scalars?: Record<string, ScalarConfig>;
|
|
57
|
+
/**
|
|
58
|
+
* Which GraphQL client the generated module binds to. `'buildgql'` (the default) re-exports
|
|
59
|
+
* buildgql's own `createClient`; `'apollo'` and `'urql'` re-export that client's adapter
|
|
60
|
+
* instead; `'none'` binds no client at all. Adapters require the `graphql` package.
|
|
61
|
+
*/
|
|
62
|
+
readonly client?: ClientKind;
|
|
63
|
+
}
|
|
64
|
+
declare function defineConfig(config: BuildGQLConfig): BuildGQLConfig;
|
|
65
|
+
/** Loads an ES module given its `file:` URL. Overridable in tests only — see `loadConfig`. */
|
|
66
|
+
type ConfigImporter = (url: string) => Promise<unknown>;
|
|
67
|
+
declare function loadConfig(cwd?: string, importModule?: ConfigImporter): Promise<{
|
|
68
|
+
config: BuildGQLConfig;
|
|
69
|
+
path: string;
|
|
70
|
+
}>;
|
|
71
|
+
declare function resolveOutputDir(config: BuildGQLConfig, cwd: string): string;
|
|
72
|
+
|
|
73
|
+
export { type BuildGQLConfig, type ClientKind, type ConfigImporter, type ScalarConfig, type ScalarTypeConfig, defineConfig, loadConfig, resolveOutputDir };
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
// src/cli/config.ts
|
|
2
|
+
import { access } from "fs/promises";
|
|
3
|
+
import { pathToFileURL } from "url";
|
|
4
|
+
import { isAbsolute, join, resolve } from "path";
|
|
5
|
+
|
|
6
|
+
// src/codegen/clients.ts
|
|
7
|
+
var CLIENT_KINDS = ["buildgql", "apollo", "urql", "none"];
|
|
8
|
+
function isClientKind(value) {
|
|
9
|
+
return typeof value === "string" && CLIENT_KINDS.includes(value);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// src/cli/assert-scalars.ts
|
|
13
|
+
var SCALAR_KEYS = [
|
|
14
|
+
"name",
|
|
15
|
+
"from",
|
|
16
|
+
"declare",
|
|
17
|
+
"input",
|
|
18
|
+
"output"
|
|
19
|
+
];
|
|
20
|
+
var IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
21
|
+
var RESERVED_NAMES = /* @__PURE__ */ new Set([
|
|
22
|
+
"break",
|
|
23
|
+
"case",
|
|
24
|
+
"catch",
|
|
25
|
+
"class",
|
|
26
|
+
"const",
|
|
27
|
+
"continue",
|
|
28
|
+
"debugger",
|
|
29
|
+
"default",
|
|
30
|
+
"delete",
|
|
31
|
+
"do",
|
|
32
|
+
"else",
|
|
33
|
+
"enum",
|
|
34
|
+
"export",
|
|
35
|
+
"extends",
|
|
36
|
+
"false",
|
|
37
|
+
"finally",
|
|
38
|
+
"for",
|
|
39
|
+
"function",
|
|
40
|
+
"if",
|
|
41
|
+
"import",
|
|
42
|
+
"in",
|
|
43
|
+
"instanceof",
|
|
44
|
+
"new",
|
|
45
|
+
"null",
|
|
46
|
+
"return",
|
|
47
|
+
"super",
|
|
48
|
+
"switch",
|
|
49
|
+
"this",
|
|
50
|
+
"throw",
|
|
51
|
+
"true",
|
|
52
|
+
"try",
|
|
53
|
+
"typeof",
|
|
54
|
+
"var",
|
|
55
|
+
"void",
|
|
56
|
+
"while",
|
|
57
|
+
"with",
|
|
58
|
+
"await",
|
|
59
|
+
"implements",
|
|
60
|
+
"interface",
|
|
61
|
+
"let",
|
|
62
|
+
"package",
|
|
63
|
+
"private",
|
|
64
|
+
"protected",
|
|
65
|
+
"public",
|
|
66
|
+
"static",
|
|
67
|
+
"yield",
|
|
68
|
+
"any",
|
|
69
|
+
"bigint",
|
|
70
|
+
"boolean",
|
|
71
|
+
"never",
|
|
72
|
+
"number",
|
|
73
|
+
"object",
|
|
74
|
+
"string",
|
|
75
|
+
"symbol",
|
|
76
|
+
"undefined",
|
|
77
|
+
"unknown",
|
|
78
|
+
"as"
|
|
79
|
+
]);
|
|
80
|
+
var UNSAFE_IN_QUOTED_SPECIFIER = /['"\n\r\\]/;
|
|
81
|
+
function isPlainRecord(value) {
|
|
82
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
83
|
+
}
|
|
84
|
+
function assertNonEmptyString(configName, scalar, key, value) {
|
|
85
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
86
|
+
throw new Error(`buildgql: ${configName}'s "scalars.${scalar}.${key}" must be a non-empty string`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function assertScalarsConfig(configName, value) {
|
|
90
|
+
if (!isPlainRecord(value)) {
|
|
91
|
+
throw new Error(`buildgql: ${configName}'s "scalars" must be a record keyed by GraphQL scalar name`);
|
|
92
|
+
}
|
|
93
|
+
for (const [scalar, entry] of Object.entries(value)) {
|
|
94
|
+
if (typeof entry === "string") {
|
|
95
|
+
if (entry.length === 0) {
|
|
96
|
+
throw new Error(`buildgql: ${configName}'s "scalars.${scalar}" must not be an empty string`);
|
|
97
|
+
}
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (!isPlainRecord(entry)) {
|
|
101
|
+
throw new Error(
|
|
102
|
+
`buildgql: ${configName}'s "scalars.${scalar}" must be a TypeScript type string, or an object with "input"/"output", "name" + "from", or "name" + "declare"`
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
for (const key of Object.keys(entry)) {
|
|
106
|
+
if (!SCALAR_KEYS.includes(key)) {
|
|
107
|
+
throw new Error(
|
|
108
|
+
`buildgql: ${configName}'s "scalars.${scalar}" has an unknown key "${key}" \u2014 expected one of ` + SCALAR_KEYS.map((k) => `"${k}"`).join(", ")
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const { name, from, declare, input, output } = entry;
|
|
113
|
+
if (from !== void 0 && declare !== void 0) {
|
|
114
|
+
throw new Error(
|
|
115
|
+
`buildgql: ${configName}'s "scalars.${scalar}" sets both "from" and "declare" \u2014 a type is either imported or declared, not both`
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
if (from !== void 0) {
|
|
119
|
+
assertNonEmptyString(configName, scalar, "from", from);
|
|
120
|
+
if (UNSAFE_IN_QUOTED_SPECIFIER.test(from)) {
|
|
121
|
+
throw new Error(
|
|
122
|
+
`buildgql: ${configName}'s "scalars.${scalar}.from" ("${from}") contains a character ("'", '"', a backslash, or a newline) that cannot appear there \u2014 "from" is emitted inside a quoted module specifier. Use forward slashes even for a Windows-style path.`
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (declare !== void 0) assertNonEmptyString(configName, scalar, "declare", declare);
|
|
127
|
+
if (input !== void 0) assertNonEmptyString(configName, scalar, "input", input);
|
|
128
|
+
if (output !== void 0) assertNonEmptyString(configName, scalar, "output", output);
|
|
129
|
+
if (name !== void 0) {
|
|
130
|
+
assertNonEmptyString(configName, scalar, "name", name);
|
|
131
|
+
if (!IDENTIFIER.test(name)) {
|
|
132
|
+
throw new Error(
|
|
133
|
+
`buildgql: ${configName}'s "scalars.${scalar}.name" ("${name}") is not a valid TypeScript identifier`
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
if (RESERVED_NAMES.has(name)) {
|
|
137
|
+
throw new Error(
|
|
138
|
+
`buildgql: ${configName}'s "scalars.${scalar}.name" ("${name}") is a reserved word and cannot be spliced into "import type { ${name} }" or "export type ${name} = ..." \u2014 choose a different name`
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
if (from === void 0 && declare === void 0) {
|
|
142
|
+
throw new Error(
|
|
143
|
+
`buildgql: ${configName}'s "scalars.${scalar}" sets "name" but neither "from" nor "declare" \u2014 nothing in the generated module would declare that type`
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
} else if (from !== void 0 || declare !== void 0) {
|
|
147
|
+
throw new Error(
|
|
148
|
+
`buildgql: ${configName}'s "scalars.${scalar}" sets "${from !== void 0 ? "from" : "declare"}" but not "name" \u2014 "name" is what the generated module refers to the type by`
|
|
149
|
+
);
|
|
150
|
+
} else if (input === void 0 && output === void 0) {
|
|
151
|
+
throw new Error(
|
|
152
|
+
`buildgql: ${configName}'s "scalars.${scalar}" is empty \u2014 set "input"/"output", or "name" together with "from" or "declare"`
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// src/cli/config.ts
|
|
159
|
+
function defineConfig(config) {
|
|
160
|
+
return config;
|
|
161
|
+
}
|
|
162
|
+
var CANDIDATES = ["buildgql.config.ts", "buildgql.config.mts", "buildgql.config.mjs", "buildgql.config.js"];
|
|
163
|
+
async function exists(p) {
|
|
164
|
+
try {
|
|
165
|
+
await access(p);
|
|
166
|
+
return true;
|
|
167
|
+
} catch {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
function isRecord(value) {
|
|
172
|
+
return typeof value === "object" && value !== null;
|
|
173
|
+
}
|
|
174
|
+
function isStringRecord(value) {
|
|
175
|
+
return isRecord(value) && Object.values(value).every((v) => typeof v === "string");
|
|
176
|
+
}
|
|
177
|
+
function errorMessage(err) {
|
|
178
|
+
return err instanceof Error ? err.message : String(err);
|
|
179
|
+
}
|
|
180
|
+
function isMissingTypeStrippingSupport(err) {
|
|
181
|
+
return isRecord(err) && (err.code === "ERR_UNKNOWN_FILE_EXTENSION" || err.code === "ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX");
|
|
182
|
+
}
|
|
183
|
+
function assertBuildGQLConfig(name, value) {
|
|
184
|
+
if (typeof value.schema !== "string" || value.schema.length === 0) {
|
|
185
|
+
throw new Error(`buildgql: ${name} is missing "schema" \u2014 set it to a URL, a .json, or a .graphql file`);
|
|
186
|
+
}
|
|
187
|
+
if (value.output !== void 0 && typeof value.output !== "string") {
|
|
188
|
+
throw new Error(`buildgql: ${name}'s "output" must be a string path`);
|
|
189
|
+
}
|
|
190
|
+
if (value.headers !== void 0 && !isStringRecord(value.headers)) {
|
|
191
|
+
throw new Error(`buildgql: ${name}'s "headers" must be a record of string values`);
|
|
192
|
+
}
|
|
193
|
+
if (value.scalars !== void 0) assertScalarsConfig(name, value.scalars);
|
|
194
|
+
if (value.client !== void 0 && !isClientKind(value.client)) {
|
|
195
|
+
throw new Error(
|
|
196
|
+
`buildgql: ${name}'s "client" must be one of ${CLIENT_KINDS.map((k) => `"${k}"`).join(", ")}`
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
var defaultImporter = (url) => import(url);
|
|
201
|
+
async function loadConfig(cwd = process.cwd(), importModule = defaultImporter) {
|
|
202
|
+
let missingTypeStrippingMessage;
|
|
203
|
+
for (const name of CANDIDATES) {
|
|
204
|
+
const path = join(cwd, name);
|
|
205
|
+
if (!await exists(path)) continue;
|
|
206
|
+
let mod;
|
|
207
|
+
try {
|
|
208
|
+
mod = await importModule(pathToFileURL(path).href);
|
|
209
|
+
} catch (err) {
|
|
210
|
+
if ((name.endsWith(".ts") || name.endsWith(".mts")) && isMissingTypeStrippingSupport(err)) {
|
|
211
|
+
missingTypeStrippingMessage = `buildgql: could not load ${name}. Node must be able to run TypeScript directly (Node >= 22.6 with --experimental-strip-types, or Node >= 23.6). Otherwise rename it to buildgql.config.mjs. Original error: ${errorMessage(err)}`;
|
|
212
|
+
process.stderr.write(
|
|
213
|
+
`buildgql: skipping ${name} \u2014 this Node cannot run TypeScript directly. ${errorMessage(err)}
|
|
214
|
+
`
|
|
215
|
+
);
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
throw new Error(`buildgql: failed to load ${name}: ${errorMessage(err)}`);
|
|
219
|
+
}
|
|
220
|
+
if (!isRecord(mod) || !("default" in mod)) {
|
|
221
|
+
throw new Error(`buildgql: ${name} must have a default export`);
|
|
222
|
+
}
|
|
223
|
+
const config = mod.default;
|
|
224
|
+
if (!isRecord(config)) {
|
|
225
|
+
throw new Error(`buildgql: ${name}'s default export must be an object (use defineConfig({...}))`);
|
|
226
|
+
}
|
|
227
|
+
assertBuildGQLConfig(name, config);
|
|
228
|
+
return { config, path };
|
|
229
|
+
}
|
|
230
|
+
if (missingTypeStrippingMessage) {
|
|
231
|
+
throw new Error(missingTypeStrippingMessage);
|
|
232
|
+
}
|
|
233
|
+
throw new Error(
|
|
234
|
+
`buildgql: no config found in ${cwd}. Create a buildgql.config.ts (or .js) exporting defineConfig({ schema: 'https://...' | './schema.graphql' }).`
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
function resolveOutputDir(config, cwd) {
|
|
238
|
+
const out = config.output ?? "./src/gql";
|
|
239
|
+
return isAbsolute(out) ? out : resolve(cwd, out);
|
|
240
|
+
}
|
|
241
|
+
export {
|
|
242
|
+
defineConfig,
|
|
243
|
+
loadConfig,
|
|
244
|
+
resolveOutputDir
|
|
245
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
var _chunk2Z2AKLA3cjs = require('../chunk-2Z2AKLA3.cjs');
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
exports.BuildGQLError = _chunk2Z2AKLA3cjs.BuildGQLError; exports.BuildGQLHttpError = _chunk2Z2AKLA3cjs.BuildGQLHttpError; exports.BuildGQLResponseError = _chunk2Z2AKLA3cjs.BuildGQLResponseError; exports.createClient = _chunk2Z2AKLA3cjs.createClient; exports.sseTransport = _chunk2Z2AKLA3cjs.sseTransport; exports.wsTransport = _chunk2Z2AKLA3cjs.wsTransport;
|