goatly 0.1.0 → 0.1.2
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/README.md +874 -1
- package/bin/goatly.js +99 -0
- package/dist/cli/index.js +234 -0
- package/dist/compat/gatsby-node-entry.js +16 -0
- package/dist/compat/gatsby-package.json +6 -0
- package/dist/compat/md5-file.js +47 -0
- package/dist/compat-apis.json +72 -0
- package/dist/compat-apis.json.js +62 -0
- package/dist/graphql.js +3 -0
- package/dist/node/actions/index.js +619 -0
- package/dist/node/actions/job-store.js +180 -0
- package/dist/node/actions/jobs.js +174 -0
- package/dist/node/api-runner.js +258 -0
- package/dist/node/bootstrap.js +569 -0
- package/dist/node/build/assets.js +110 -0
- package/dist/node/build/incremental.js +208 -0
- package/dist/node/build/index.js +763 -0
- package/dist/node/build/memory-governor.js +450 -0
- package/dist/node/build/output-dirs.js +70 -0
- package/dist/node/build/page-data.js +40 -0
- package/dist/node/build/page-files.js +186 -0
- package/dist/node/build/partitioned-state.js +258 -0
- package/dist/node/build/query-worker.js +626 -0
- package/dist/node/build/query-workers.js +863 -0
- package/dist/node/build/render-thread.js +103 -0
- package/dist/node/build/render-worker.js +162 -0
- package/dist/node/build/state.js +24 -0
- package/dist/node/build/timing-breakdown.js +216 -0
- package/dist/node/build/work-clock.js +75 -0
- package/dist/node/build/worker-pool.js +242 -0
- package/dist/node/cache-layout.js +99 -0
- package/dist/node/cache.js +1430 -0
- package/dist/node/compat/chokidar-shim.js +148 -0
- package/dist/node/compat/core-utils-lmdb.js +145 -0
- package/dist/node/compat/create-node-id-shim.js +1 -0
- package/dist/node/compat/module-hooks.js +245 -0
- package/dist/node/compat/redux-shim.js +1 -0
- package/dist/node/config/index.js +162 -0
- package/dist/node/config/not-found-pages.js +95 -0
- package/dist/node/datastore/codec.js +144 -0
- package/dist/node/datastore/index.js +127 -0
- package/dist/node/datastore/logfile/cache.js +89 -0
- package/dist/node/datastore/logfile/index.js +1329 -0
- package/dist/node/datastore/logfile/record.js +71 -0
- package/dist/node/datastore/types.js +1 -0
- package/dist/node/develop/express-compat.js +207 -0
- package/dist/node/develop/graphiql.js +22 -0
- package/dist/node/develop/hydration-report.js +41 -0
- package/dist/node/develop/index.js +618 -0
- package/dist/node/develop/local-url.js +10 -0
- package/dist/node/develop/ssr-runner.js +66 -0
- package/dist/node/gql.js +5 -0
- package/dist/node/index.js +15 -0
- package/dist/node/info.js +270 -0
- package/dist/node/internal-plugins/data-bridge.js +196 -0
- package/dist/node/internal-plugins/page-creator.js +318 -0
- package/dist/node/loader/import-module.js +160 -0
- package/dist/node/node-helpers.js +66 -0
- package/dist/node/not-found-pages.js +220 -0
- package/dist/node/plugins/api-list.js +12 -0
- package/dist/node/plugins/load-plugins.js +427 -0
- package/dist/node/plugins/resolve-plugin.js +79 -0
- package/dist/node/plugins/static-exports.js +64 -0
- package/dist/node/query/compile.js +326 -0
- package/dist/node/query/dependencies.js +45 -0
- package/dist/node/query/execute.js +417 -0
- package/dist/node/query/extract.js +372 -0
- package/dist/node/query/gql-tada-introspection.js +124 -0
- package/dist/node/query/graphql-env.js +231 -0
- package/dist/node/query/graphql-runner.js +79 -0
- package/dist/node/redux/actions.js +1 -0
- package/dist/node/redux/index.js +133 -0
- package/dist/node/redux/node-changes.js +128 -0
- package/dist/node/redux/reducers.js +193 -0
- package/dist/node/redux/state.js +71 -0
- package/dist/node/renderer.js +124 -0
- package/dist/node/reporter-transports.js +259 -0
- package/dist/node/reporter.js +497 -0
- package/dist/node/schema/date.js +539 -0
- package/dist/node/schema/extensions.js +315 -0
- package/dist/node/schema/index.js +542 -0
- package/dist/node/schema/infer.js +388 -0
- package/dist/node/schema/inline-objects.js +128 -0
- package/dist/node/schema/naming.js +20 -0
- package/dist/node/schema/node-model.js +207 -0
- package/dist/node/schema/query-engine.js +1808 -0
- package/dist/node/schema/resolver-kinds.js +7 -0
- package/dist/node/schema/resolvers.js +273 -0
- package/dist/node/schema/type-builders.js +45 -0
- package/dist/node/schema/types.js +241 -0
- package/dist/node/serve.js +90 -0
- package/dist/node/site-page-nodes.js +277 -0
- package/dist/node/types.js +1 -0
- package/dist/node/utils/content-digest.js +107 -0
- package/dist/node/utils/cpus.js +104 -0
- package/dist/node/utils/create-node-id.js +51 -0
- package/dist/node/utils/env.js +25 -0
- package/dist/node/utils/fs-retry.js +74 -0
- package/dist/node/utils/memory-signals.js +441 -0
- package/dist/node/utils/memory.js +657 -0
- package/dist/node/utils/murmurhash.js +32 -0
- package/dist/node/utils/parser.js +33 -0
- package/dist/node/utils/partition-hash.js +19 -0
- package/dist/node/utils/paths.js +122 -0
- package/dist/node/utils/resolver.js +48 -0
- package/dist/node/utils/runtime.js +5 -0
- package/dist/node/utils/serializable.js +41 -0
- package/dist/node/utils/strings.js +41 -0
- package/dist/node/vite/config.js +289 -0
- package/dist/node/vite/paths.js +12 -0
- package/dist/node/vite/plugin-deps.js +232 -0
- package/dist/node/vite/plugin.js +283 -0
- package/dist/node/vite/react-compiler.js +362 -0
- package/dist/node/vite/source-files.js +83 -0
- package/dist/node/vite/static-image.js +136 -0
- package/dist/node/vite/styled-components.js +73 -0
- package/dist/node/vite/transforms.js +330 -0
- package/dist/node/wasm-data/host.js +406 -0
- package/dist/node/wasm-data/lower.js +406 -0
- package/dist/node/wasm-data/runner.js +79 -0
- package/dist/node/wasm-data/worker.js +95 -0
- package/dist/reporter.js +5 -0
- package/dist/runtime/api-runner.js +81 -0
- package/dist/runtime/app.js +256 -0
- package/dist/runtime/client-entry.js +4 -0
- package/dist/runtime/client.js +383 -0
- package/dist/runtime/default-html.js +31 -0
- package/dist/runtime/head-markup.js +100 -0
- package/dist/runtime/head-sync.js +101 -0
- package/dist/runtime/head-tags.js +45 -0
- package/dist/runtime/hydration-report.js +111 -0
- package/dist/runtime/index.js +12 -0
- package/dist/runtime/link.js +293 -0
- package/dist/runtime/loader.js +449 -0
- package/dist/runtime/not-found.js +61 -0
- package/dist/runtime/page-renderer.js +35 -0
- package/dist/runtime/preamble.js +47 -0
- package/dist/runtime/react-compat.js +14 -0
- package/dist/runtime/renderer-shared.js +10 -0
- package/dist/runtime/renderers/react-client.js +14 -0
- package/dist/runtime/renderers/react-server.js +99 -0
- package/dist/runtime/renderers/react.js +17 -0
- package/dist/runtime/renderers/types.js +1 -0
- package/dist/runtime/root-error-handlers.js +29 -0
- package/dist/runtime/router/history.js +132 -0
- package/dist/runtime/router/index.js +240 -0
- package/dist/runtime/router/utils.js +118 -0
- package/dist/runtime/script.js +110 -0
- package/dist/runtime/scroll.js +40 -0
- package/dist/runtime/slice.js +11 -0
- package/dist/runtime/ssr.js +240 -0
- package/dist/runtime/static-query.js +47 -0
- package/dist/utils.js +4 -0
- package/package.json +105 -3
- package/types/graphql-tada.d.ts +223 -0
- package/types/graphql.d.ts +2 -0
- package/types/index.d.ts +2287 -0
- package/types/node.d.ts +389 -0
- package/types/reporter.d.ts +187 -0
- package/types/router.d.ts +102 -0
- package/types/vendor/gql-tada.d.ts +2529 -0
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import { getDataStore, getNode, nodeTypeOf } from "../datastore/index.js";
|
|
2
|
+
import reporter from "../reporter.js";
|
|
3
|
+
import { isDate } from "./date.js";
|
|
4
|
+
import { slash } from "../utils/paths.js";
|
|
5
|
+
import { rootFree } from "./inline-objects.js";
|
|
6
|
+
import { ObjectTypeComposer } from "graphql-compose";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
// src/node/schema/infer.ts
|
|
9
|
+
const RESERVED = /* @__PURE__ */ new Set([
|
|
10
|
+
"id",
|
|
11
|
+
"parent",
|
|
12
|
+
"children",
|
|
13
|
+
"internal",
|
|
14
|
+
"$loki",
|
|
15
|
+
"__gatsby_resolved"
|
|
16
|
+
]);
|
|
17
|
+
function describeValue(desc, value, nodeId) {
|
|
18
|
+
if (value === null || value === void 0) return;
|
|
19
|
+
switch (typeof value) {
|
|
20
|
+
case "string":
|
|
21
|
+
if (value === "") return;
|
|
22
|
+
if (isDate(value)) desc.date = (desc.date ?? 0) + 1;
|
|
23
|
+
else {
|
|
24
|
+
desc.string = (desc.string ?? 0) + 1;
|
|
25
|
+
if (!desc.example) desc.example = {
|
|
26
|
+
value,
|
|
27
|
+
nodeId
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
return;
|
|
31
|
+
case "number":
|
|
32
|
+
if (Number.isInteger(value) && Math.abs(value) <= 2147483647) desc.int = (desc.int ?? 0) + 1;
|
|
33
|
+
else desc.float = (desc.float ?? 0) + 1;
|
|
34
|
+
return;
|
|
35
|
+
case "boolean":
|
|
36
|
+
desc.boolean = (desc.boolean ?? 0) + 1;
|
|
37
|
+
return;
|
|
38
|
+
case "object":
|
|
39
|
+
if (value instanceof Date) {
|
|
40
|
+
desc.date = (desc.date ?? 0) + 1;
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (Array.isArray(value)) {
|
|
44
|
+
const nonEmpty = value.filter((v) => v !== null && v !== void 0 && v !== "");
|
|
45
|
+
if (nonEmpty.length === 0) return;
|
|
46
|
+
desc.array ??= {
|
|
47
|
+
count: 0,
|
|
48
|
+
item: {}
|
|
49
|
+
};
|
|
50
|
+
desc.array.count++;
|
|
51
|
+
for (const item of nonEmpty) describeValue(desc.array.item, item, nodeId);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (Buffer.isBuffer(value)) return;
|
|
55
|
+
if (Object.keys(value).length === 0) return;
|
|
56
|
+
desc.object ??= {
|
|
57
|
+
count: 0,
|
|
58
|
+
fields: {}
|
|
59
|
+
};
|
|
60
|
+
desc.object.count++;
|
|
61
|
+
describeObject(desc.object.fields, value, nodeId);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function describeObject(fields, obj, nodeId, isNode = false) {
|
|
65
|
+
for (const key in obj) {
|
|
66
|
+
if (isNode && RESERVED.has(key)) continue;
|
|
67
|
+
if (key.startsWith("__gatsby")) continue;
|
|
68
|
+
const value = obj[key];
|
|
69
|
+
if (key.endsWith("___NODE")) {
|
|
70
|
+
if (value === null || value === void 0 || Array.isArray(value) && value.length === 0) continue;
|
|
71
|
+
const name = key.slice(0, -7);
|
|
72
|
+
const d = fields[name] ??= {};
|
|
73
|
+
d.relatedNode ??= {
|
|
74
|
+
count: 0,
|
|
75
|
+
ids: /* @__PURE__ */ new Set(),
|
|
76
|
+
isList: Array.isArray(value),
|
|
77
|
+
key
|
|
78
|
+
};
|
|
79
|
+
d.relatedNode.count++;
|
|
80
|
+
for (const id of [].concat(value)) if (typeof id === "string") d.relatedNode.ids.add(id);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
describeValue(fields[key] ??= {}, value, nodeId);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** the types of the parents of a type's nodes (from the store's indexes when it has them) */
|
|
87
|
+
function collectParentTypes(typeName) {
|
|
88
|
+
const ds = getDataStore();
|
|
89
|
+
const types = /* @__PURE__ */ new Set();
|
|
90
|
+
if (typeof ds.getNodeIdsByType === "function" && typeof ds.getNodeParent === "function") {
|
|
91
|
+
for (const id of ds.getNodeIdsByType(typeName)) {
|
|
92
|
+
const parent = ds.getNodeParent(id);
|
|
93
|
+
const type = parent ? nodeTypeOf(parent) : void 0;
|
|
94
|
+
if (type) types.add(type);
|
|
95
|
+
}
|
|
96
|
+
return types;
|
|
97
|
+
}
|
|
98
|
+
for (const node of ds.iterateNodesByType(typeName)) {
|
|
99
|
+
const type = node.parent ? nodeTypeOf(node.parent) : void 0;
|
|
100
|
+
if (type) types.add(type);
|
|
101
|
+
}
|
|
102
|
+
return types;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Metadata of a type that is not inferred (`@dontInfer`), without reading its nodes. Like Gatsby
|
|
106
|
+
* (since v3), such a type gets no inferred `childOf` (children fields need `@childOf`) and its
|
|
107
|
+
* nodes' media types are not observed (only `@mimeTypes` counts).
|
|
108
|
+
*/
|
|
109
|
+
function countTypeMetadata(typeName) {
|
|
110
|
+
return {
|
|
111
|
+
typeName,
|
|
112
|
+
fields: {},
|
|
113
|
+
nodeCount: getDataStore().countNodes(typeName),
|
|
114
|
+
parentTypes: /* @__PURE__ */ new Set(),
|
|
115
|
+
mediaTypes: /* @__PURE__ */ new Set(),
|
|
116
|
+
countOnly: true
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function collectTypeMetadata(typeName) {
|
|
120
|
+
const meta = {
|
|
121
|
+
typeName,
|
|
122
|
+
fields: {},
|
|
123
|
+
nodeCount: 0,
|
|
124
|
+
parentTypes: /* @__PURE__ */ new Set(),
|
|
125
|
+
mediaTypes: /* @__PURE__ */ new Set()
|
|
126
|
+
};
|
|
127
|
+
const ds = getDataStore();
|
|
128
|
+
for (const node of ds.iterateNodesByType(typeName)) {
|
|
129
|
+
meta.nodeCount++;
|
|
130
|
+
describeObject(meta.fields, node, node.id, true);
|
|
131
|
+
if (node.parent) {
|
|
132
|
+
const parentType = ds.getNodeType ? ds.getNodeType(node.parent) : getNode(node.parent)?.internal.type;
|
|
133
|
+
if (parentType) meta.parentTypes.add(parentType);
|
|
134
|
+
}
|
|
135
|
+
if (node.internal.mediaType) meta.mediaTypes.add(node.internal.mediaType);
|
|
136
|
+
}
|
|
137
|
+
return meta;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* A GraphQL field name for a data key, like Gatsby's `createFieldName`: invalid characters become
|
|
141
|
+
* `_`, a leading digit gets a `_` prefix, and names starting with `__` (reserved for GraphQL's
|
|
142
|
+
* introspection, e.g. `__typename` in data from a GraphQL API) keep one `_` and the other `_` become
|
|
143
|
+
* `x` (`__typename` → `_xtypename`).
|
|
144
|
+
*/
|
|
145
|
+
function createFieldName(key) {
|
|
146
|
+
let name = key.replace(/[^_a-zA-Z0-9]/g, "_");
|
|
147
|
+
if (name.startsWith("__")) name = name.replace(/_/g, (char, index) => index === 0 ? char : "x");
|
|
148
|
+
if (/^[0-9]/.test(name)) name = `_${name}`;
|
|
149
|
+
return name;
|
|
150
|
+
}
|
|
151
|
+
function createTypeName(selector) {
|
|
152
|
+
const keys = selector.split(".");
|
|
153
|
+
const suffix = keys.slice(1).map((k) => {
|
|
154
|
+
const n = createFieldName(k);
|
|
155
|
+
return n.charAt(0).toUpperCase() + n.slice(1);
|
|
156
|
+
}).join("");
|
|
157
|
+
return `${keys[0]}${suffix}`;
|
|
158
|
+
}
|
|
159
|
+
function kinds(desc) {
|
|
160
|
+
const k = [];
|
|
161
|
+
if (desc.string) k.push("string");
|
|
162
|
+
if (desc.date) k.push("date");
|
|
163
|
+
if (desc.int) k.push("int");
|
|
164
|
+
if (desc.float) k.push("float");
|
|
165
|
+
if (desc.boolean) k.push("boolean");
|
|
166
|
+
if (desc.object) k.push("object");
|
|
167
|
+
if (desc.array) k.push("array");
|
|
168
|
+
if (desc.relatedNode) k.push("relatedNode");
|
|
169
|
+
return k;
|
|
170
|
+
}
|
|
171
|
+
function resolveKind(desc, selector, ctx) {
|
|
172
|
+
let k = kinds(desc);
|
|
173
|
+
if (k.includes("int") && k.includes("float")) k = k.filter((x) => x !== "int");
|
|
174
|
+
if (k.length === 2 && k.includes("date") && k.includes("string")) return "string";
|
|
175
|
+
if (k.length > 1) {
|
|
176
|
+
if (!ctx.warned.has(selector)) {
|
|
177
|
+
ctx.warned.add(selector);
|
|
178
|
+
reporter.warn(`There are conflicting field types in your data.\n\n${selector}:\n${k.map((x) => ` - type: ${x}`).join("\n")}\n\nIf you have explicitly defined a type for those nodes, you can ignore this message.`);
|
|
179
|
+
}
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
return k[0];
|
|
183
|
+
}
|
|
184
|
+
function looksLikeRelativeFile(value) {
|
|
185
|
+
if (!value || value.length > 1e3) return false;
|
|
186
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(value) || value.startsWith("/") || value.startsWith("#")) return false;
|
|
187
|
+
if (!/\.[a-z0-9]+$/i.test(value)) return false;
|
|
188
|
+
return value.startsWith("./") || value.startsWith("../") || /^[\w-]+(\/[\w.-]+)*\.[a-z0-9]+$/i.test(value);
|
|
189
|
+
}
|
|
190
|
+
function isFileReference(example) {
|
|
191
|
+
if (!example || !looksLikeRelativeFile(example.value)) return false;
|
|
192
|
+
let node = getNode(example.nodeId);
|
|
193
|
+
while (node && node.internal.type !== "File") node = node.parent ? getNode(node.parent) : void 0;
|
|
194
|
+
if (!node || typeof node.dir !== "string") return false;
|
|
195
|
+
const absolutePath = slash(path.join(node.dir, example.value));
|
|
196
|
+
return fileAbsolutePaths().has(absolutePath);
|
|
197
|
+
}
|
|
198
|
+
/** absolute paths of all File nodes (one scan per inference run instead of one per field) */
|
|
199
|
+
let filePathsMemo;
|
|
200
|
+
function fileAbsolutePaths() {
|
|
201
|
+
const ds = getDataStore();
|
|
202
|
+
const count = ds.countNodes("File");
|
|
203
|
+
if (filePathsMemo && filePathsMemo.ds === ds && filePathsMemo.count === count) return filePathsMemo.paths;
|
|
204
|
+
const paths = /* @__PURE__ */ new Set();
|
|
205
|
+
for (const file of ds.iterateNodesByType("File")) if (typeof file.absolutePath === "string") paths.add(file.absolutePath);
|
|
206
|
+
filePathsMemo = {
|
|
207
|
+
ds,
|
|
208
|
+
count,
|
|
209
|
+
paths
|
|
210
|
+
};
|
|
211
|
+
return paths;
|
|
212
|
+
}
|
|
213
|
+
/** forget memoized data (a new inference run) */
|
|
214
|
+
function resetInferenceMemo() {
|
|
215
|
+
filePathsMemo = void 0;
|
|
216
|
+
}
|
|
217
|
+
function scalarFor(kind) {
|
|
218
|
+
switch (kind) {
|
|
219
|
+
case "string": return "String";
|
|
220
|
+
case "date": return "Date";
|
|
221
|
+
case "int": return "Int";
|
|
222
|
+
case "float": return "Float";
|
|
223
|
+
case "boolean": return "Boolean";
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
function relatedNodeConfig(desc, ctx) {
|
|
227
|
+
const types = /* @__PURE__ */ new Set();
|
|
228
|
+
for (const id of desc.ids) {
|
|
229
|
+
const type = nodeTypeOf(id);
|
|
230
|
+
if (type) types.add(type);
|
|
231
|
+
}
|
|
232
|
+
if (types.size === 0) return void 0;
|
|
233
|
+
const sorted = [...types].sort();
|
|
234
|
+
let type;
|
|
235
|
+
if (sorted.length === 1) type = sorted[0];
|
|
236
|
+
else {
|
|
237
|
+
const unionName = `${sorted.join("")}Union`;
|
|
238
|
+
type = ctx.sc.getOrCreateUTC(unionName, (utc) => {
|
|
239
|
+
utc.setTypes(sorted);
|
|
240
|
+
utc.setResolveType(rootFree((node) => node.internal.type));
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
return {
|
|
244
|
+
type: desc.isList ? [type] : type,
|
|
245
|
+
extensions: { link: {
|
|
246
|
+
by: "id",
|
|
247
|
+
from: desc.key
|
|
248
|
+
} }
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* `selector`: the field's path of GraphQL names (`MarkdownRemark.frontmatter._xauthor`), as Gatsby
|
|
253
|
+
* looks up `mapping` and names inferred object types; `dataPath`: its path of data keys
|
|
254
|
+
* (`MarkdownRemark.frontmatter.__author`), for messages.
|
|
255
|
+
*/
|
|
256
|
+
function fieldConfigFor(desc, selector, dataPath, typeNameForObjects, ctx, existingTC, mappingDataPath = dataPath) {
|
|
257
|
+
const mapped = ctx.mapping[selector] ?? ctx.mapping[mappingDataPath];
|
|
258
|
+
if (mapped) {
|
|
259
|
+
const [linkedType, ...by] = mapped.split(".");
|
|
260
|
+
return {
|
|
261
|
+
type: !!desc.array ? [linkedType] : linkedType,
|
|
262
|
+
extensions: { link: { by: by.length ? by.join(".") : "id" } }
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
const kind = resolveKind(desc, dataPath, ctx);
|
|
266
|
+
if (!kind) return void 0;
|
|
267
|
+
if (kind === "relatedNode") return relatedNodeConfig(desc.relatedNode, ctx);
|
|
268
|
+
if (kind === "object") {
|
|
269
|
+
const tc = existingTC instanceof ObjectTypeComposer ? existingTC : ctx.sc.getOrCreateOTC(typeNameForObjects, (t) => t.setExtension("createdFrom", "inference"));
|
|
270
|
+
addInferredFieldsToTC(tc, desc.object.fields, selector, ctx, dataPath);
|
|
271
|
+
if (tc.getFieldNames().length === 0) return void 0;
|
|
272
|
+
return { type: tc };
|
|
273
|
+
}
|
|
274
|
+
if (kind === "array") {
|
|
275
|
+
const item = fieldConfigFor(desc.array.item, selector, dataPath, typeNameForObjects, ctx, existingTC);
|
|
276
|
+
if (!item) return void 0;
|
|
277
|
+
return {
|
|
278
|
+
...item,
|
|
279
|
+
type: [item.type]
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
if (kind === "string" && !selector.startsWith("File.") && isFileReference(desc.example)) return {
|
|
283
|
+
type: "File",
|
|
284
|
+
extensions: { fileByRelativePath: {} }
|
|
285
|
+
};
|
|
286
|
+
if (kind === "date") return {
|
|
287
|
+
type: "Date",
|
|
288
|
+
extensions: { dateformat: {} }
|
|
289
|
+
};
|
|
290
|
+
return { type: scalarFor(kind) };
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* The data keys that become one GraphQL field (e.g. `__typename` and `_xtypename`, `foo-bar` and
|
|
294
|
+
* `foo_bar`, or `author` and `author___NODE`), by field name. A descriptor holds both the values of
|
|
295
|
+
* a key and its `___NODE` link: they are two candidates, as in Gatsby.
|
|
296
|
+
*/
|
|
297
|
+
function groupByFieldName(fields) {
|
|
298
|
+
const groups = /* @__PURE__ */ new Map();
|
|
299
|
+
const add = (candidate) => {
|
|
300
|
+
const fieldName = createFieldName(candidate.key);
|
|
301
|
+
const group = groups.get(fieldName);
|
|
302
|
+
if (group) group.push(candidate);
|
|
303
|
+
else groups.set(fieldName, [candidate]);
|
|
304
|
+
};
|
|
305
|
+
for (const [key, desc] of Object.entries(fields)) {
|
|
306
|
+
if (!desc.relatedNode) {
|
|
307
|
+
add({
|
|
308
|
+
key,
|
|
309
|
+
unsanitizedKey: key,
|
|
310
|
+
desc
|
|
311
|
+
});
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
const { relatedNode, ...values } = desc;
|
|
315
|
+
add({
|
|
316
|
+
key,
|
|
317
|
+
unsanitizedKey: relatedNode.key,
|
|
318
|
+
desc: { relatedNode }
|
|
319
|
+
});
|
|
320
|
+
if (kinds(values).length) add({
|
|
321
|
+
key,
|
|
322
|
+
unsanitizedKey: key,
|
|
323
|
+
desc: values
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
return groups;
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Whether a key has a value Gatsby's example object keeps (buildExampleValue): one type (Int and
|
|
330
|
+
* Float, or Date and String, count as one), an object with such a field, an array of such items.
|
|
331
|
+
* Only those keys compete for a field: one that is always null or has conflicting types does not
|
|
332
|
+
* take the field from another key.
|
|
333
|
+
*/
|
|
334
|
+
function hasExampleValue(desc) {
|
|
335
|
+
let k = kinds(desc);
|
|
336
|
+
if (k.includes("int") && k.includes("float")) k = k.filter((x) => x !== "int");
|
|
337
|
+
if (k.length === 2 && k.includes("date") && k.includes("string")) return true;
|
|
338
|
+
if (k.length !== 1) return false;
|
|
339
|
+
if (k[0] === "object") return Object.values(desc.object.fields).some(hasExampleValue);
|
|
340
|
+
if (k[0] === "array") return hasExampleValue(desc.array.item);
|
|
341
|
+
return true;
|
|
342
|
+
}
|
|
343
|
+
/** like Gatsby's resolveMultipleFields: a `___NODE` link, else the unsanitized key, else the first */
|
|
344
|
+
function pickField(fieldName, candidates) {
|
|
345
|
+
return candidates.find((c) => c.unsanitizedKey.endsWith("___NODE")) ?? candidates.find((c) => c.unsanitizedKey === fieldName) ?? [...candidates].sort((a, b) => a.unsanitizedKey < b.unsanitizedKey ? -1 : a.unsanitizedKey > b.unsanitizedKey ? 1 : 0)[0];
|
|
346
|
+
}
|
|
347
|
+
function addInferredFieldsToTC(tc, fields, selectorPrefix, ctx, dataPathPrefix = selectorPrefix) {
|
|
348
|
+
for (const [fieldName, all] of groupByFieldName(fields)) {
|
|
349
|
+
const selector = `${selectorPrefix}.${fieldName}`;
|
|
350
|
+
let candidates = all;
|
|
351
|
+
if (all.length > 1) {
|
|
352
|
+
candidates = all.filter((c) => hasExampleValue(c.desc));
|
|
353
|
+
for (const c of all) if (!candidates.includes(c)) resolveKind(c.desc, `${dataPathPrefix}.${c.unsanitizedKey}`, ctx);
|
|
354
|
+
if (candidates.length === 0) continue;
|
|
355
|
+
}
|
|
356
|
+
const { key, unsanitizedKey, desc } = candidates.length > 1 ? pickField(fieldName, candidates) : candidates[0];
|
|
357
|
+
if (candidates.length > 1 && !ctx.warned.has(`fields:${selector}`)) {
|
|
358
|
+
ctx.warned.add(`fields:${selector}`);
|
|
359
|
+
const names = candidates.map((c) => `\`${c.unsanitizedKey}\``).join(", ");
|
|
360
|
+
reporter.warn(`Multiple node fields resolve to the same GraphQL field \`${selector}\` - [${names}]. The field uses \`${unsanitizedKey}\`.`);
|
|
361
|
+
}
|
|
362
|
+
const dataPath = `${dataPathPrefix}.${unsanitizedKey}`;
|
|
363
|
+
const mappingDataPath = `${dataPathPrefix}.${key}`;
|
|
364
|
+
if (tc.hasField(fieldName)) {
|
|
365
|
+
const ftc = tc.getFieldTC(fieldName);
|
|
366
|
+
if (ftc instanceof ObjectTypeComposer && desc.object && ftc.getExtension("infer") !== false) addInferredFieldsToTC(ftc, desc.object.fields, selector, ctx, dataPath);
|
|
367
|
+
else if (ftc instanceof ObjectTypeComposer && desc.array?.item.object && ftc.getExtension("infer") !== false) addInferredFieldsToTC(ftc, desc.array.item.object.fields, selector, ctx, dataPath);
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
const config = fieldConfigFor(desc, selector, dataPath, createTypeName(selector), ctx, void 0, mappingDataPath);
|
|
371
|
+
if (!config) continue;
|
|
372
|
+
if (fieldName !== key) config.extensions = {
|
|
373
|
+
...config.extensions,
|
|
374
|
+
proxy: { from: unsanitizedKey }
|
|
375
|
+
};
|
|
376
|
+
tc.addFields({ [fieldName]: config });
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
function inferTypeFields(sc, tc, meta, mapping) {
|
|
380
|
+
const ctx = {
|
|
381
|
+
sc,
|
|
382
|
+
mapping,
|
|
383
|
+
rootTypeName: tc.getTypeName(),
|
|
384
|
+
warned: /* @__PURE__ */ new Set()
|
|
385
|
+
};
|
|
386
|
+
addInferredFieldsToTC(tc, meta.fields, tc.getTypeName(), ctx);
|
|
387
|
+
}
|
|
388
|
+
export { addInferredFieldsToTC, collectParentTypes, collectTypeMetadata, countTypeMetadata, createFieldName, createTypeName, inferTypeFields, resetInferenceMemo };
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { isAbstractType, isInterfaceType, isObjectType } from "../gql.js";
|
|
2
|
+
// src/node/schema/inline-objects.ts
|
|
3
|
+
/**
|
|
4
|
+
* Inline objects of nodes (e.g. `frontmatter`) → id of the node they belong to, so resolvers
|
|
5
|
+
* such as `@fileByRelativePath` can find the root node of a nested object
|
|
6
|
+
* (`nodeModel.findRootNodeAncestor`).
|
|
7
|
+
*/
|
|
8
|
+
const inlineObjectRoots = /* @__PURE__ */ new WeakMap();
|
|
9
|
+
const trackedNodes = /* @__PURE__ */ new WeakSet();
|
|
10
|
+
/**
|
|
11
|
+
* Resolvers known not to look up the root node of an object (no `findRootNodeAncestor`), e.g.
|
|
12
|
+
* `@link` by id or `@dateformat` without `from`/`fromNode`
|
|
13
|
+
*/
|
|
14
|
+
const rootFreeResolvers = /* @__PURE__ */ new WeakSet();
|
|
15
|
+
/**
|
|
16
|
+
* Built-in resolvers that read nothing of their source but its own field (`source[fieldName]`):
|
|
17
|
+
* `@link` and `@dateformat` without `from` and without a resolver of their own field. The query
|
|
18
|
+
* engine may pass them a node without its other (large) fields.
|
|
19
|
+
*/
|
|
20
|
+
const ownFieldResolvers = /* @__PURE__ */ new WeakSet();
|
|
21
|
+
function ownField(resolve) {
|
|
22
|
+
ownFieldResolvers.add(resolve);
|
|
23
|
+
return resolve;
|
|
24
|
+
}
|
|
25
|
+
/** whether a field resolver (or no resolver: the default one) reads only its own field */
|
|
26
|
+
function readsOwnFieldOnly(resolve) {
|
|
27
|
+
return resolve == null || typeof resolve === "function" && ownFieldResolvers.has(resolve);
|
|
28
|
+
}
|
|
29
|
+
/** marks a built-in resolver as one that never calls `findRootNodeAncestor` */
|
|
30
|
+
function rootFree(resolve) {
|
|
31
|
+
rootFreeResolvers.add(resolve);
|
|
32
|
+
return resolve;
|
|
33
|
+
}
|
|
34
|
+
/** whether a field resolver (or no resolver: the default one) never looks up root nodes */
|
|
35
|
+
function isRootFree(resolve) {
|
|
36
|
+
return resolve == null || typeof resolve === "function" && rootFreeResolvers.has(resolve);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Resolvers that never change their arguments object and pass it only to resolvers that don't
|
|
40
|
+
* (goatly's own, e.g. `@dateformat` and `@link`): the query executor gives all their calls
|
|
41
|
+
* in a query the same object instead of computing the arguments for every node of a list.
|
|
42
|
+
*/
|
|
43
|
+
const argsSharingResolvers = /* @__PURE__ */ new WeakSet();
|
|
44
|
+
function sharedArgs(resolve) {
|
|
45
|
+
argsSharingResolvers.add(resolve);
|
|
46
|
+
return resolve;
|
|
47
|
+
}
|
|
48
|
+
/** whether a field resolver's calls can share one arguments object (see `sharedArgs`) */
|
|
49
|
+
function sharesArgs(resolve) {
|
|
50
|
+
return typeof resolve === "function" && argsSharingResolvers.has(resolve);
|
|
51
|
+
}
|
|
52
|
+
/** marks a wrapping resolver when the resolver it calls (none: the default one) shares them */
|
|
53
|
+
function sharedArgsIf(resolve, inner) {
|
|
54
|
+
return inner == null || sharesArgs(inner) ? sharedArgs(resolve) : resolve;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Synchronous variants of goatly's own resolvers (`@link`, `@dateformat`, `x(id: { eq })`).
|
|
58
|
+
* The resolvers in the schema return a promise, as Gatsby's do (plugins chain on them, graphql-js
|
|
59
|
+
* orders sibling fields' errors by it). The variant returns its value when everything it reads
|
|
60
|
+
* is synchronous: the query executor and the query engine, which handle both, call it instead.
|
|
61
|
+
*/
|
|
62
|
+
const syncVariants = /* @__PURE__ */ new WeakMap();
|
|
63
|
+
function withSyncVariant(resolve, sync) {
|
|
64
|
+
syncVariants.set(resolve, sync);
|
|
65
|
+
return resolve;
|
|
66
|
+
}
|
|
67
|
+
/** the synchronous variant of a resolver, or the resolver itself */
|
|
68
|
+
function syncVariantOf(resolve) {
|
|
69
|
+
return (resolve && syncVariants.get(resolve)) ?? resolve;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Whether inline objects are tracked: until a schema is built, and with any resolver that may
|
|
73
|
+
* look up root nodes (a custom resolver, `@fileByRelativePath`, `fromNode`, ...). Without them
|
|
74
|
+
* nothing reads `inlineObjectRoots`, and tracking would walk every node read (e.g. every post of
|
|
75
|
+
* a list query).
|
|
76
|
+
*/
|
|
77
|
+
let rootsNeeded = true;
|
|
78
|
+
/**
|
|
79
|
+
* Whether a schema of this process needed them: tracking stays on (a develop session's queries
|
|
80
|
+
* may still run on that schema while the next one is built)
|
|
81
|
+
*/
|
|
82
|
+
let neededBefore = false;
|
|
83
|
+
/**
|
|
84
|
+
* Called when a schema is built (in the main process, the query workers and develop): with
|
|
85
|
+
* `undefined` while it is built.
|
|
86
|
+
*/
|
|
87
|
+
function updateInlineRootsNeeded(schema) {
|
|
88
|
+
if (!schema) {
|
|
89
|
+
rootsNeeded = true;
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
rootsNeeded = neededBefore || schemaMayFindRootNodes(schema);
|
|
93
|
+
neededBefore = rootsNeeded;
|
|
94
|
+
}
|
|
95
|
+
/** (tests: like a new process) */
|
|
96
|
+
function resetInlineRootsNeeded() {
|
|
97
|
+
rootsNeeded = true;
|
|
98
|
+
neededBefore = false;
|
|
99
|
+
}
|
|
100
|
+
function schemaMayFindRootNodes(schema) {
|
|
101
|
+
for (const type of Object.values(schema.getTypeMap())) {
|
|
102
|
+
if (type.name.startsWith("__")) continue;
|
|
103
|
+
if (isAbstractType(type) && !isRootFree(type.resolveType)) return true;
|
|
104
|
+
if (!isObjectType(type) && !isInterfaceType(type)) continue;
|
|
105
|
+
if (isObjectType(type) && !isRootFree(type.isTypeOf)) return true;
|
|
106
|
+
for (const field of Object.values(type.getFields())) if (!isRootFree(field.resolve)) return true;
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
function trackInlineObjects(node) {
|
|
111
|
+
if (!rootsNeeded || !node || typeof node !== "object" || trackedNodes.has(node)) return;
|
|
112
|
+
trackedNodes.add(node);
|
|
113
|
+
const walk = (value) => {
|
|
114
|
+
if (value === null || typeof value !== "object") return;
|
|
115
|
+
if (Array.isArray(value)) {
|
|
116
|
+
for (const v of value) walk(v);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (inlineObjectRoots.has(value)) return;
|
|
120
|
+
inlineObjectRoots.set(value, node.id);
|
|
121
|
+
for (const [k, v] of Object.entries(value)) if (k !== "internal") walk(v);
|
|
122
|
+
};
|
|
123
|
+
for (const [k, v] of Object.entries(node)) {
|
|
124
|
+
if (k === "internal" || k === "children" || k === "parent" || k === "id") continue;
|
|
125
|
+
walk(v);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
export { inlineObjectRoots, isRootFree, ownField, readsOwnFieldOnly, resetInlineRootsNeeded, rootFree, sharedArgs, sharedArgsIf, sharesArgs, syncVariantOf, trackInlineObjects, updateInlineRootsNeeded, withSyncVariant };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/node/schema/naming.ts
|
|
2
|
+
const HAS_UNICODE_WORD = /[a-z][A-Z]|[A-Z]{2}[a-z]|[0-9][a-zA-Z]|[a-zA-Z][0-9]|[^a-zA-Z0-9 ]/;
|
|
3
|
+
const UNICODE_WORDS = /[A-Z]?[a-z]+(?=[^a-zA-Z]|[A-Z]|$)|[A-Z]+(?=[^a-zA-Z]|[A-Z][a-z]|$)|[A-Z]?[a-z]+|[A-Z]+|\d+/g;
|
|
4
|
+
const ASCII_WORDS = /[^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]+/g;
|
|
5
|
+
function words(input) {
|
|
6
|
+
return (HAS_UNICODE_WORD.test(input) ? input.match(UNICODE_WORDS) : input.match(ASCII_WORDS)) ?? [];
|
|
7
|
+
}
|
|
8
|
+
function camelCase(input) {
|
|
9
|
+
return words(input.replace(/['’]/g, "")).map((w, i) => {
|
|
10
|
+
const lower = w.toLowerCase();
|
|
11
|
+
return i === 0 ? lower : lower.charAt(0).toUpperCase() + lower.slice(1);
|
|
12
|
+
}).join("");
|
|
13
|
+
}
|
|
14
|
+
const fieldNames = {
|
|
15
|
+
query: (typeName) => camelCase(typeName),
|
|
16
|
+
queryAll: (typeName) => camelCase(`all ${typeName}`),
|
|
17
|
+
convenienceChild: (typeName) => camelCase(`child ${typeName}`),
|
|
18
|
+
convenienceChildren: (typeName) => camelCase(`children ${typeName}`)
|
|
19
|
+
};
|
|
20
|
+
export { camelCase, fieldNames, words };
|