goatly 0.1.0 → 0.1.1
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 +853 -1
- package/bin/goatly.js +91 -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 +539 -0
- package/dist/node/build/assets.js +110 -0
- package/dist/node/build/incremental.js +208 -0
- package/dist/node/build/index.js +752 -0
- package/dist/node/build/memory-governor.js +448 -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 +54 -0
- package/dist/node/build/partitioned-state.js +258 -0
- package/dist/node/build/query-worker.js +536 -0
- package/dist/node/build/query-workers.js +832 -0
- package/dist/node/build/render-worker.js +131 -0
- package/dist/node/build/state.js +24 -0
- package/dist/node/build/timing-breakdown.js +155 -0
- package/dist/node/build/worker-pool.js +236 -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/create-node-id.js +51 -0
- package/dist/node/utils/env.js +24 -0
- package/dist/node/utils/fs-retry.js +74 -0
- package/dist/node/utils/memory-signals.js +410 -0
- package/dist/node/utils/memory.js +400 -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 +281 -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 +299 -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 +380 -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,273 @@
|
|
|
1
|
+
import { connectionReadFields, fieldSelectorToPath, getValueAt, isLazyStore, queryResultNode, queryResultNodes, resolveFieldPath, runIdQuery, runQuery, runQueryIds } from "./query-engine.js";
|
|
2
|
+
import { rootFree, sharedArgs, withSyncVariant } from "./inline-objects.js";
|
|
3
|
+
import { tagResolver } from "./resolver-kinds.js";
|
|
4
|
+
// src/node/schema/resolvers.ts
|
|
5
|
+
/** Same pagination semantics as Gatsby's `paginate()` */
|
|
6
|
+
function paginate(all, { skip = 0, limit }) {
|
|
7
|
+
const totalCount = all.length;
|
|
8
|
+
const items = skip || limit !== void 0 && limit !== null ? all.slice(skip, limit != null ? skip + limit : void 0) : all;
|
|
9
|
+
const pageCount = limit ? Math.ceil(skip / limit) + Math.ceil((totalCount - skip) / limit) : skip ? 2 : 1;
|
|
10
|
+
const currentPage = limit ? Math.ceil(skip / limit) + 1 : skip ? 2 : 1;
|
|
11
|
+
const hasPreviousPage = currentPage > 1;
|
|
12
|
+
const hasNextPage = limit ? skip + limit < totalCount : false;
|
|
13
|
+
return {
|
|
14
|
+
totalCount,
|
|
15
|
+
edges: items.map((node, i, arr) => ({
|
|
16
|
+
node,
|
|
17
|
+
next: arr[i + 1],
|
|
18
|
+
previous: arr[i - 1]
|
|
19
|
+
})),
|
|
20
|
+
nodes: items,
|
|
21
|
+
pageInfo: {
|
|
22
|
+
currentPage,
|
|
23
|
+
hasPreviousPage,
|
|
24
|
+
hasNextPage,
|
|
25
|
+
itemCount: items.length,
|
|
26
|
+
pageCount,
|
|
27
|
+
perPage: limit,
|
|
28
|
+
totalCount
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function findOneResolver(typeName) {
|
|
33
|
+
const query = (args, info) => ({
|
|
34
|
+
type: info.schema.getType(typeName),
|
|
35
|
+
query: { filter: Object.keys(args).length ? args : void 0 },
|
|
36
|
+
firstOnly: true
|
|
37
|
+
});
|
|
38
|
+
const found = (result, context) => {
|
|
39
|
+
const node = result[0] ?? null;
|
|
40
|
+
if (node) context.nodeModel?.trackInlineObjectsInRootNode?.(node);
|
|
41
|
+
return node;
|
|
42
|
+
};
|
|
43
|
+
const resolveFindOne = async function resolveFindOne(_source, args, context, info) {
|
|
44
|
+
return found(await runQuery(query(args, info), {
|
|
45
|
+
schema: info.schema,
|
|
46
|
+
context
|
|
47
|
+
}), context);
|
|
48
|
+
};
|
|
49
|
+
tagResolver(resolveFindOne, {
|
|
50
|
+
k: "findOne",
|
|
51
|
+
type: typeName
|
|
52
|
+
});
|
|
53
|
+
return withSyncVariant(resolveFindOne, function resolveFindOneSync(_source, args, context, info) {
|
|
54
|
+
const q = query(args, info);
|
|
55
|
+
const rc = {
|
|
56
|
+
schema: info.schema,
|
|
57
|
+
context
|
|
58
|
+
};
|
|
59
|
+
const byId = runIdQuery(q, rc);
|
|
60
|
+
return byId ? found(byId, context) : runQuery(q, rc).then((result) => found(result, context));
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/** connections with more nodes than this are resolved node by node (lazy datastores) */
|
|
64
|
+
const LAZY_LIST_THRESHOLD = 1e3;
|
|
65
|
+
/**
|
|
66
|
+
* `paginate` for node ids (lazy datastores): only the returned page of nodes is loaded, large
|
|
67
|
+
* pages are resolved one node at a time while GraphQL consumes the list.
|
|
68
|
+
*/
|
|
69
|
+
function paginateIds(ids, { skip = 0, limit }, context, reads) {
|
|
70
|
+
const totalCount = ids.length;
|
|
71
|
+
const pageIds = skip || limit !== void 0 && limit !== null ? ids.slice(skip, limit != null ? skip + limit : void 0) : ids;
|
|
72
|
+
const pageInfo = {
|
|
73
|
+
...paginate([], {
|
|
74
|
+
skip,
|
|
75
|
+
limit
|
|
76
|
+
}).pageInfo,
|
|
77
|
+
hasNextPage: limit ? skip + limit < totalCount : false,
|
|
78
|
+
itemCount: pageIds.length,
|
|
79
|
+
pageCount: limit ? Math.ceil(skip / limit) + Math.ceil((totalCount - skip) / limit) : skip ? 2 : 1,
|
|
80
|
+
totalCount
|
|
81
|
+
};
|
|
82
|
+
const track = (node) => {
|
|
83
|
+
context?.nodeModel?.trackInlineObjectsInRootNode?.(node);
|
|
84
|
+
return node;
|
|
85
|
+
};
|
|
86
|
+
if (pageIds.length <= LAZY_LIST_THRESHOLD) {
|
|
87
|
+
const items = queryResultNodes(pageIds, reads).map(track);
|
|
88
|
+
return {
|
|
89
|
+
totalCount,
|
|
90
|
+
edges: items.map((node, i, arr) => ({
|
|
91
|
+
node,
|
|
92
|
+
next: arr[i + 1],
|
|
93
|
+
previous: arr[i - 1]
|
|
94
|
+
})),
|
|
95
|
+
nodes: items,
|
|
96
|
+
pageInfo
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
const nodeAt = (i) => {
|
|
100
|
+
const id = pageIds[i];
|
|
101
|
+
const node = id === void 0 ? void 0 : queryResultNode(id, reads);
|
|
102
|
+
return node ? track(node) : void 0;
|
|
103
|
+
};
|
|
104
|
+
return {
|
|
105
|
+
totalCount,
|
|
106
|
+
pageInfo,
|
|
107
|
+
get nodes() {
|
|
108
|
+
return (function* () {
|
|
109
|
+
for (let i = 0; i < pageIds.length; i++) {
|
|
110
|
+
const node = nodeAt(i);
|
|
111
|
+
if (node) yield node;
|
|
112
|
+
}
|
|
113
|
+
})();
|
|
114
|
+
},
|
|
115
|
+
get edges() {
|
|
116
|
+
return (function* () {
|
|
117
|
+
for (let i = 0; i < pageIds.length; i++) {
|
|
118
|
+
const node = nodeAt(i);
|
|
119
|
+
if (!node) continue;
|
|
120
|
+
yield {
|
|
121
|
+
node,
|
|
122
|
+
get next() {
|
|
123
|
+
return nodeAt(i + 1);
|
|
124
|
+
},
|
|
125
|
+
get previous() {
|
|
126
|
+
return nodeAt(i - 1);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
})();
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function findManyPaginatedResolver(typeName) {
|
|
135
|
+
return tagResolver(resolveFindManyOf(typeName), {
|
|
136
|
+
k: "findMany",
|
|
137
|
+
type: typeName
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
function resolveFindManyOf(typeName) {
|
|
141
|
+
return async function resolveFindMany(_source, args, context, info) {
|
|
142
|
+
if (isLazyStore()) {
|
|
143
|
+
const ids = await runQueryIds({
|
|
144
|
+
type: info.schema.getType(typeName),
|
|
145
|
+
query: {
|
|
146
|
+
filter: args.filter,
|
|
147
|
+
sort: args.sort
|
|
148
|
+
}
|
|
149
|
+
}, {
|
|
150
|
+
schema: info.schema,
|
|
151
|
+
context
|
|
152
|
+
});
|
|
153
|
+
const direct = info.parentType.getFields()[info.fieldName]?.resolve === resolveFindMany;
|
|
154
|
+
return paginateIds(ids, {
|
|
155
|
+
skip: args.skip ?? 0,
|
|
156
|
+
limit: args.limit
|
|
157
|
+
}, context, direct ? connectionReadFields(info, typeName) : void 0);
|
|
158
|
+
}
|
|
159
|
+
const type = info.schema.getType(typeName);
|
|
160
|
+
const result = paginate(await runQuery({
|
|
161
|
+
type,
|
|
162
|
+
query: {
|
|
163
|
+
filter: args.filter,
|
|
164
|
+
sort: args.sort
|
|
165
|
+
}
|
|
166
|
+
}, {
|
|
167
|
+
schema: info.schema,
|
|
168
|
+
context
|
|
169
|
+
}), {
|
|
170
|
+
skip: args.skip ?? 0,
|
|
171
|
+
limit: args.limit
|
|
172
|
+
});
|
|
173
|
+
if (context.nodeModel?.trackInlineObjectsInRootNode) for (const n of result.nodes) context.nodeModel.trackInlineObjectsInRootNode(n);
|
|
174
|
+
return result;
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
async function valueFor(node, field, info, context) {
|
|
178
|
+
const path = fieldSelectorToPath(field).split(".");
|
|
179
|
+
const type = info.schema.getType(node.internal?.type);
|
|
180
|
+
if (!type) return getValueAt(node, path);
|
|
181
|
+
return resolveFieldPath(node, type, path, {
|
|
182
|
+
schema: info.schema,
|
|
183
|
+
context
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
function createConnectionResolvers(_typeName) {
|
|
187
|
+
const numeric = async (source, args, context, info) => {
|
|
188
|
+
const values = [];
|
|
189
|
+
for (const { node } of source.edges) {
|
|
190
|
+
let v = await valueFor(node, args.field, info, context);
|
|
191
|
+
for (const x of Array.isArray(v) ? v : [v]) {
|
|
192
|
+
v = typeof x === "number" ? x : Number(x);
|
|
193
|
+
if (x !== null && x !== void 0 && !Number.isNaN(v)) values.push(v);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return values;
|
|
197
|
+
};
|
|
198
|
+
return {
|
|
199
|
+
async distinct(source, args, context, info) {
|
|
200
|
+
const values = /* @__PURE__ */ new Set();
|
|
201
|
+
for (const { node } of source.edges) {
|
|
202
|
+
const value = await valueFor(node, args.field, info, context);
|
|
203
|
+
for (const v of Array.isArray(value) ? value.flat(1e400) : [value]) {
|
|
204
|
+
if (v === null || v === void 0) continue;
|
|
205
|
+
values.add(v instanceof Date ? v.toISOString() : String(v));
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return [...values].sort();
|
|
209
|
+
},
|
|
210
|
+
async max(source, args, context, info) {
|
|
211
|
+
const values = await numeric(source, args, context, info);
|
|
212
|
+
return values.length ? Math.max(...values) : null;
|
|
213
|
+
},
|
|
214
|
+
async min(source, args, context, info) {
|
|
215
|
+
const values = await numeric(source, args, context, info);
|
|
216
|
+
return values.length ? Math.min(...values) : null;
|
|
217
|
+
},
|
|
218
|
+
async sum(source, args, context, info) {
|
|
219
|
+
const values = await numeric(source, args, context, info);
|
|
220
|
+
return values.length ? values.reduce((a, b) => a + b, 0) : null;
|
|
221
|
+
},
|
|
222
|
+
async group(source, args, context, info) {
|
|
223
|
+
const fieldPath = fieldSelectorToPath(args.field);
|
|
224
|
+
const groups = /* @__PURE__ */ new Map();
|
|
225
|
+
for (const { node } of source.edges) {
|
|
226
|
+
const value = await valueFor(node, args.field, info, context);
|
|
227
|
+
const values = Array.isArray(value) ? value.flat(1e400) : [value];
|
|
228
|
+
for (const v of new Set(values)) {
|
|
229
|
+
if (v === null || v === void 0) continue;
|
|
230
|
+
const key = v instanceof Date ? v.toISOString() : String(v);
|
|
231
|
+
let list = groups.get(key);
|
|
232
|
+
if (!list) {
|
|
233
|
+
list = [];
|
|
234
|
+
groups.set(key, list);
|
|
235
|
+
}
|
|
236
|
+
list.push(node);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return [...groups.keys()].sort().map((fieldValue) => ({
|
|
240
|
+
...paginate(groups.get(fieldValue), {
|
|
241
|
+
skip: args.skip ?? 0,
|
|
242
|
+
limit: args.limit
|
|
243
|
+
}),
|
|
244
|
+
field: fieldPath,
|
|
245
|
+
fieldValue
|
|
246
|
+
}));
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
function createChildrenField(typeName) {
|
|
251
|
+
return {
|
|
252
|
+
type: () => `[${typeName}]`,
|
|
253
|
+
description: `Returns all children nodes filtered by type ${typeName}`,
|
|
254
|
+
resolve: sharedArgs(rootFree((source, _args, context) => context.nodeModel.getNodesByIds({
|
|
255
|
+
ids: source.children,
|
|
256
|
+
type: typeName
|
|
257
|
+
})))
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
function createChildField(typeName) {
|
|
261
|
+
return {
|
|
262
|
+
type: () => typeName,
|
|
263
|
+
description: `Returns the first child node of type ${typeName} or null if there are no children of given type on this node`,
|
|
264
|
+
resolve: sharedArgs(rootFree((source, _args, context) => {
|
|
265
|
+
const result = context.nodeModel.getNodesByIds({
|
|
266
|
+
ids: source.children,
|
|
267
|
+
type: typeName
|
|
268
|
+
});
|
|
269
|
+
return result.length ? result[0] : null;
|
|
270
|
+
}))
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
export { createChildField, createChildrenField, createConnectionResolvers, findManyPaginatedResolver, findOneResolver, paginate, paginateIds };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// src/node/schema/type-builders.ts
|
|
2
|
+
/** `schema.buildObjectType` & friends, passed to every Node API as `schema`. */
|
|
3
|
+
const buildObjectType = (config) => ({
|
|
4
|
+
kind: "OBJECT",
|
|
5
|
+
config
|
|
6
|
+
});
|
|
7
|
+
const buildUnionType = (config) => ({
|
|
8
|
+
kind: "UNION",
|
|
9
|
+
config
|
|
10
|
+
});
|
|
11
|
+
const buildInterfaceType = (config) => ({
|
|
12
|
+
kind: "INTERFACE",
|
|
13
|
+
config
|
|
14
|
+
});
|
|
15
|
+
const buildInputObjectType = (config) => ({
|
|
16
|
+
kind: "INPUT_OBJECT",
|
|
17
|
+
config
|
|
18
|
+
});
|
|
19
|
+
const buildEnumType = (config) => ({
|
|
20
|
+
kind: "ENUM",
|
|
21
|
+
config
|
|
22
|
+
});
|
|
23
|
+
const buildScalarType = (config) => ({
|
|
24
|
+
kind: "SCALAR",
|
|
25
|
+
config
|
|
26
|
+
});
|
|
27
|
+
const typeBuilders = {
|
|
28
|
+
buildObjectType,
|
|
29
|
+
buildUnionType,
|
|
30
|
+
buildInterfaceType,
|
|
31
|
+
buildInputObjectType,
|
|
32
|
+
buildEnumType,
|
|
33
|
+
buildScalarType
|
|
34
|
+
};
|
|
35
|
+
function isGatsbyType(value) {
|
|
36
|
+
return !!value && typeof value === "object" && typeof value.kind === "string" && "config" in value && [
|
|
37
|
+
"OBJECT",
|
|
38
|
+
"UNION",
|
|
39
|
+
"INTERFACE",
|
|
40
|
+
"INPUT_OBJECT",
|
|
41
|
+
"ENUM",
|
|
42
|
+
"SCALAR"
|
|
43
|
+
].includes(value.kind);
|
|
44
|
+
}
|
|
45
|
+
export { buildEnumType, buildInputObjectType, buildInterfaceType, buildObjectType, buildScalarType, buildUnionType, isGatsbyType, typeBuilders };
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { GraphQLList, getNamedType, getNullableType, isEnumType, isScalarType } from "../gql.js";
|
|
2
|
+
import { createConnectionResolvers } from "./resolvers.js";
|
|
3
|
+
import { rootFree } from "./inline-objects.js";
|
|
4
|
+
import { EnumTypeComposer, InputTypeComposer, InterfaceTypeComposer, ObjectTypeComposer, ScalarTypeComposer, UnionTypeComposer } from "graphql-compose";
|
|
5
|
+
// src/node/schema/types.ts
|
|
6
|
+
function getPageInfo(sc) {
|
|
7
|
+
return sc.getOrCreateOTC("PageInfo", (tc) => {
|
|
8
|
+
tc.addFields({
|
|
9
|
+
currentPage: "Int!",
|
|
10
|
+
hasPreviousPage: "Boolean!",
|
|
11
|
+
hasNextPage: "Boolean!",
|
|
12
|
+
itemCount: "Int!",
|
|
13
|
+
pageCount: "Int!",
|
|
14
|
+
perPage: "Int",
|
|
15
|
+
totalCount: "Int!"
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
const OPERATOR_INPUTS = {
|
|
20
|
+
String: {
|
|
21
|
+
eq: "String",
|
|
22
|
+
ne: "String",
|
|
23
|
+
in: "[String]",
|
|
24
|
+
nin: "[String]",
|
|
25
|
+
regex: "String",
|
|
26
|
+
glob: "String"
|
|
27
|
+
},
|
|
28
|
+
ID: {
|
|
29
|
+
eq: "ID",
|
|
30
|
+
ne: "ID",
|
|
31
|
+
in: "[ID]",
|
|
32
|
+
nin: "[ID]"
|
|
33
|
+
},
|
|
34
|
+
Int: {
|
|
35
|
+
eq: "Int",
|
|
36
|
+
ne: "Int",
|
|
37
|
+
gt: "Int",
|
|
38
|
+
gte: "Int",
|
|
39
|
+
lt: "Int",
|
|
40
|
+
lte: "Int",
|
|
41
|
+
in: "[Int]",
|
|
42
|
+
nin: "[Int]"
|
|
43
|
+
},
|
|
44
|
+
Float: {
|
|
45
|
+
eq: "Float",
|
|
46
|
+
ne: "Float",
|
|
47
|
+
gt: "Float",
|
|
48
|
+
gte: "Float",
|
|
49
|
+
lt: "Float",
|
|
50
|
+
lte: "Float",
|
|
51
|
+
in: "[Float]",
|
|
52
|
+
nin: "[Float]"
|
|
53
|
+
},
|
|
54
|
+
Boolean: {
|
|
55
|
+
eq: "Boolean",
|
|
56
|
+
ne: "Boolean",
|
|
57
|
+
in: "[Boolean]",
|
|
58
|
+
nin: "[Boolean]"
|
|
59
|
+
},
|
|
60
|
+
Date: {
|
|
61
|
+
eq: "Date",
|
|
62
|
+
ne: "Date",
|
|
63
|
+
gt: "Date",
|
|
64
|
+
gte: "Date",
|
|
65
|
+
lt: "Date",
|
|
66
|
+
lte: "Date",
|
|
67
|
+
in: "[Date]",
|
|
68
|
+
nin: "[Date]"
|
|
69
|
+
},
|
|
70
|
+
JSON: {
|
|
71
|
+
eq: "JSON",
|
|
72
|
+
ne: "JSON",
|
|
73
|
+
in: "[JSON]",
|
|
74
|
+
nin: "[JSON]",
|
|
75
|
+
regex: "JSON",
|
|
76
|
+
glob: "JSON"
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
function getQueryOperatorInput(sc, type) {
|
|
80
|
+
const name = type.name;
|
|
81
|
+
const fields = OPERATOR_INPUTS[name];
|
|
82
|
+
if (fields) return sc.getOrCreateITC(`${name}QueryOperatorInput`, (itc) => itc.addFields(fields));
|
|
83
|
+
if (isEnumType(type) || isScalarType(type)) return sc.getOrCreateITC(`${name}QueryOperatorInput`, (itc) => itc.addFields({
|
|
84
|
+
eq: name,
|
|
85
|
+
ne: name,
|
|
86
|
+
in: `[${name}]`,
|
|
87
|
+
nin: `[${name}]`
|
|
88
|
+
}));
|
|
89
|
+
}
|
|
90
|
+
function isComposite(tc) {
|
|
91
|
+
return tc instanceof ObjectTypeComposer || tc instanceof InterfaceTypeComposer;
|
|
92
|
+
}
|
|
93
|
+
function fieldIsList(tc, fieldName) {
|
|
94
|
+
return getNullableType(tc.getFieldType(fieldName)) instanceof GraphQLList;
|
|
95
|
+
}
|
|
96
|
+
function searchable(tc, fieldName) {
|
|
97
|
+
return tc.getFieldExtension(fieldName, "searchable") !== "NON_SEARCHABLE";
|
|
98
|
+
}
|
|
99
|
+
/** `${Type}FilterInput` */
|
|
100
|
+
function getFilterInput(sc, tc) {
|
|
101
|
+
const name = `${tc.getTypeName()}FilterInput`;
|
|
102
|
+
if (sc.has(name)) return sc.getITC(name);
|
|
103
|
+
const itc = sc.createInputTC(name);
|
|
104
|
+
sc.set(name, itc);
|
|
105
|
+
const fields = {};
|
|
106
|
+
for (const fieldName of tc.getFieldNames()) {
|
|
107
|
+
if (!searchable(tc, fieldName)) continue;
|
|
108
|
+
let fieldTC;
|
|
109
|
+
try {
|
|
110
|
+
fieldTC = tc.getFieldTC(fieldName);
|
|
111
|
+
} catch {
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (fieldTC instanceof UnionTypeComposer) continue;
|
|
115
|
+
if (isComposite(fieldTC)) {
|
|
116
|
+
const sub = getFilterInput(sc, fieldTC);
|
|
117
|
+
fields[fieldName] = fieldIsList(tc, fieldName) ? sc.getOrCreateITC(`${fieldTC.getTypeName()}FilterListInput`, (l) => l.addFields({ elemMatch: sub })) : sub;
|
|
118
|
+
} else if (fieldTC instanceof ScalarTypeComposer || fieldTC instanceof EnumTypeComposer) {
|
|
119
|
+
const named = getNamedType(tc.getFieldType(fieldName));
|
|
120
|
+
const op = fieldName === "id" && named.name === "ID" ? getQueryOperatorInput(sc, sc.getSTC("String").getType()) : getQueryOperatorInput(sc, named);
|
|
121
|
+
if (op) fields[fieldName] = op;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
itc.addFields(fields);
|
|
125
|
+
return itc;
|
|
126
|
+
}
|
|
127
|
+
function nestedInput(sc, tc, postfix, leaf) {
|
|
128
|
+
const name = `${tc.getTypeName()}${postfix}`;
|
|
129
|
+
if (sc.has(name)) return sc.getITC(name);
|
|
130
|
+
const itc = sc.createInputTC(name);
|
|
131
|
+
sc.set(name, itc);
|
|
132
|
+
const fields = {};
|
|
133
|
+
for (const fieldName of tc.getFieldNames()) {
|
|
134
|
+
if (tc.getFieldExtension(fieldName, "sortable") === "NOT_SORTABLE") continue;
|
|
135
|
+
let fieldTC;
|
|
136
|
+
try {
|
|
137
|
+
fieldTC = tc.getFieldTC(fieldName);
|
|
138
|
+
} catch {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (fieldTC instanceof UnionTypeComposer) continue;
|
|
142
|
+
if (isComposite(fieldTC)) fields[fieldName] = nestedInput(sc, fieldTC, postfix, leaf);
|
|
143
|
+
else fields[fieldName] = leaf();
|
|
144
|
+
}
|
|
145
|
+
itc.addFields(fields);
|
|
146
|
+
return itc;
|
|
147
|
+
}
|
|
148
|
+
function getSortOrderEnum(sc) {
|
|
149
|
+
return sc.getOrCreateETC("SortOrderEnum", (etc) => etc.setFields({
|
|
150
|
+
ASC: { value: "ASC" },
|
|
151
|
+
DESC: { value: "DESC" }
|
|
152
|
+
}));
|
|
153
|
+
}
|
|
154
|
+
function getFieldSelectorEnum(sc) {
|
|
155
|
+
return sc.getOrCreateETC("FieldSelectorEnum", (etc) => etc.setFields({ SELECT: { value: "SELECT" } }));
|
|
156
|
+
}
|
|
157
|
+
/** `${Type}SortInput` (Gatsby 5 nested sort input) */
|
|
158
|
+
function getSortInput(sc, tc) {
|
|
159
|
+
return nestedInput(sc, tc, "SortInput", () => getSortOrderEnum(sc));
|
|
160
|
+
}
|
|
161
|
+
/** `${Type}FieldSelector` */
|
|
162
|
+
function getFieldSelector(sc, tc) {
|
|
163
|
+
return nestedInput(sc, tc, "FieldSelector", () => getFieldSelectorEnum(sc));
|
|
164
|
+
}
|
|
165
|
+
function removeEmptyInputs(sc) {
|
|
166
|
+
let changed = true;
|
|
167
|
+
while (changed) {
|
|
168
|
+
changed = false;
|
|
169
|
+
sc.forEach((type) => {
|
|
170
|
+
if (!(type instanceof InputTypeComposer)) return;
|
|
171
|
+
for (const fieldName of type.getFieldNames()) {
|
|
172
|
+
const ftc = type.getFieldTC(fieldName);
|
|
173
|
+
if (ftc instanceof InputTypeComposer && ftc.getFieldNames().length === 0) {
|
|
174
|
+
type.removeField(fieldName);
|
|
175
|
+
changed = true;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
function getEdge(sc, tc) {
|
|
182
|
+
return sc.getOrCreateOTC(`${tc.getTypeName()}Edge`, (edge) => {
|
|
183
|
+
edge.addFields({
|
|
184
|
+
next: tc,
|
|
185
|
+
node: tc.getTypeNonNull(),
|
|
186
|
+
previous: tc
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
function createPagination(sc, tc, typeName, extraFields = {}) {
|
|
191
|
+
const fieldSelector = getFieldSelector(sc, tc).getTypeNonNull();
|
|
192
|
+
const resolvers = createConnectionResolvers(tc.getTypeName());
|
|
193
|
+
return sc.getOrCreateOTC(typeName, (conn) => {
|
|
194
|
+
conn.addFields({
|
|
195
|
+
totalCount: "Int!",
|
|
196
|
+
edges: getEdge(sc, tc).getTypeNonNull().getTypePlural().getTypeNonNull(),
|
|
197
|
+
nodes: tc.getTypeNonNull().getTypePlural().getTypeNonNull(),
|
|
198
|
+
pageInfo: getPageInfo(sc).getTypeNonNull(),
|
|
199
|
+
distinct: {
|
|
200
|
+
type: "[String!]!",
|
|
201
|
+
args: { field: fieldSelector },
|
|
202
|
+
resolve: rootFree(resolvers.distinct)
|
|
203
|
+
},
|
|
204
|
+
max: {
|
|
205
|
+
type: "Float",
|
|
206
|
+
args: { field: fieldSelector },
|
|
207
|
+
resolve: rootFree(resolvers.max)
|
|
208
|
+
},
|
|
209
|
+
min: {
|
|
210
|
+
type: "Float",
|
|
211
|
+
args: { field: fieldSelector },
|
|
212
|
+
resolve: rootFree(resolvers.min)
|
|
213
|
+
},
|
|
214
|
+
sum: {
|
|
215
|
+
type: "Float",
|
|
216
|
+
args: { field: fieldSelector },
|
|
217
|
+
resolve: rootFree(resolvers.sum)
|
|
218
|
+
},
|
|
219
|
+
group: {
|
|
220
|
+
type: () => getGroup(sc, tc).getTypeNonNull().getTypePlural().getTypeNonNull(),
|
|
221
|
+
args: {
|
|
222
|
+
skip: "Int",
|
|
223
|
+
limit: "Int",
|
|
224
|
+
field: fieldSelector
|
|
225
|
+
},
|
|
226
|
+
resolve: rootFree(resolvers.group)
|
|
227
|
+
},
|
|
228
|
+
...extraFields
|
|
229
|
+
});
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
function getGroup(sc, tc) {
|
|
233
|
+
return createPagination(sc, tc, `${tc.getTypeName()}GroupConnection`, {
|
|
234
|
+
field: "String!",
|
|
235
|
+
fieldValue: "String"
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
function getConnection(sc, tc) {
|
|
239
|
+
return createPagination(sc, tc, `${tc.getTypeName()}Connection`);
|
|
240
|
+
}
|
|
241
|
+
export { getConnection, getEdge, getFieldSelector, getFieldSelectorEnum, getFilterInput, getGroup, getPageInfo, getQueryOperatorInput, getSortInput, getSortOrderEnum, removeEmptyInputs };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { loadConfigFile, normalizeConfig } from "./config/index.js";
|
|
2
|
+
import reporter from "./reporter.js";
|
|
3
|
+
import { pick } from "../runtime/router/utils.js";
|
|
4
|
+
import { matchNotFoundEntries, notFoundLookupPath } from "../runtime/not-found.js";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import http from "node:http";
|
|
8
|
+
import sirv from "sirv";
|
|
9
|
+
// src/node/serve.ts
|
|
10
|
+
/** a path with a segment that starts with a dot (but `.well-known`) */
|
|
11
|
+
const DOT_PATH_RE = /(^|[\\/])\.(?!well-known([\\/]|$))[^\\/]/;
|
|
12
|
+
/**
|
|
13
|
+
* `gatsby serve` – serves `public/` like a static host would: unknown paths get the 404 page of
|
|
14
|
+
* their section (`notFoundPages`) or 404.html.
|
|
15
|
+
*/
|
|
16
|
+
async function serve(options) {
|
|
17
|
+
const publicDir = path.join(options.directory, "public");
|
|
18
|
+
if (!fs.existsSync(publicDir)) reporter.panic(`No "public" directory found. Run "goatly build" first.`);
|
|
19
|
+
const { config: raw } = await loadConfigFile(options.directory);
|
|
20
|
+
const config = normalizeConfig(typeof raw === "function" ? {} : raw);
|
|
21
|
+
const prefix = options.prefixPaths ? config.pathPrefix : "";
|
|
22
|
+
const assets = sirv(publicDir, {
|
|
23
|
+
dev: true,
|
|
24
|
+
etag: true,
|
|
25
|
+
extensions: ["html"],
|
|
26
|
+
setHeaders(res, pathname) {
|
|
27
|
+
if (pathname.startsWith("/assets/")) res.setHeader("Cache-Control", "public, max-age=31536000, immutable");
|
|
28
|
+
else res.setHeader("Cache-Control", "public, max-age=0, must-revalidate");
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
const servePublic = (req, res, next) => {
|
|
32
|
+
let pathname = (req.url ?? "/").split("?")[0];
|
|
33
|
+
try {
|
|
34
|
+
pathname = decodeURIComponent(pathname);
|
|
35
|
+
} catch {}
|
|
36
|
+
if (DOT_PATH_RE.test(pathname)) return next();
|
|
37
|
+
assets(req, res, next);
|
|
38
|
+
};
|
|
39
|
+
const notFound = path.join(publicDir, "404.html");
|
|
40
|
+
let matchPaths = [];
|
|
41
|
+
try {
|
|
42
|
+
matchPaths = JSON.parse(fs.readFileSync(path.join(options.directory, ".cache", "match-paths.json"), "utf8"));
|
|
43
|
+
} catch {}
|
|
44
|
+
const matchRoutes = matchPaths.map((m) => ({
|
|
45
|
+
path: m.matchPath,
|
|
46
|
+
page: m.path
|
|
47
|
+
}));
|
|
48
|
+
let notFoundPages = [];
|
|
49
|
+
try {
|
|
50
|
+
const manifest = JSON.parse(fs.readFileSync(path.join(options.directory, ".cache", "not-found-pages.json"), "utf8"));
|
|
51
|
+
if (manifest?.version === 1 && Array.isArray(manifest.pages)) notFoundPages = manifest.pages;
|
|
52
|
+
} catch {}
|
|
53
|
+
const server = http.createServer((req, res) => {
|
|
54
|
+
let url = req.url ?? "/";
|
|
55
|
+
if (prefix) {
|
|
56
|
+
if (!url.startsWith(prefix)) {
|
|
57
|
+
res.statusCode = 404;
|
|
58
|
+
res.end("Not found");
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
url = url.slice(prefix.length) || "/";
|
|
62
|
+
req.url = url;
|
|
63
|
+
}
|
|
64
|
+
servePublic(req, res, () => {
|
|
65
|
+
let matched = null;
|
|
66
|
+
try {
|
|
67
|
+
if (matchRoutes.length) matched = pick(matchRoutes, decodeURIComponent(url.split("?")[0]));
|
|
68
|
+
} catch {}
|
|
69
|
+
if (matched) {
|
|
70
|
+
const file = path.join(publicDir, matched.route.page.replace(/^\//, ""), matched.route.page.endsWith(".html") ? "" : "index.html");
|
|
71
|
+
if (fs.existsSync(file)) {
|
|
72
|
+
res.setHeader("Content-Type", "text/html; charset=utf-8");
|
|
73
|
+
fs.createReadStream(file).pipe(res);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
res.statusCode = 404;
|
|
78
|
+
const lookup = notFoundLookupPath(url);
|
|
79
|
+
const file = [...matchNotFoundEntries(notFoundPages, lookup).map((e) => path.join(publicDir, e.file)), notFound].find((f) => fs.existsSync(f));
|
|
80
|
+
if (file) {
|
|
81
|
+
res.setHeader("Content-Type", "text/html; charset=utf-8");
|
|
82
|
+
fs.createReadStream(file).pipe(res);
|
|
83
|
+
} else res.end("Not found");
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
await new Promise((resolve) => server.listen(options.port, options.host, resolve));
|
|
87
|
+
reporter.info(`You can now view your site in the browser running on http://${options.host}:${options.port}${prefix}/`);
|
|
88
|
+
return server;
|
|
89
|
+
}
|
|
90
|
+
export { serve };
|