@contello/client 1.1.1 → 1.2.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/dist/cli.js +1 -1
- package/dist/{generate-GMV2AJ66.js → generate-U3UYC2OW.js} +35 -3
- package/dist/index.cjs +19 -5
- package/dist/index.js +19 -5
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
var command = process.argv[2];
|
|
5
5
|
switch (command) {
|
|
6
6
|
case "generate":
|
|
7
|
-
await import("./generate-
|
|
7
|
+
await import("./generate-U3UYC2OW.js");
|
|
8
8
|
break;
|
|
9
9
|
default:
|
|
10
10
|
console.error(command ? `unknown command: ${command}` : "usage: contello-client <command>");
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import { mkdirSync, readFileSync, rmSync, writeFileSync } from "fs";
|
|
5
5
|
import { glob } from "fs/promises";
|
|
6
6
|
import { dirname, relative, resolve as resolve2 } from "path";
|
|
7
|
-
import { parse as parse2 } from "graphql";
|
|
7
|
+
import { Source, parse as parse2 } from "graphql";
|
|
8
8
|
|
|
9
9
|
// src/generate/config.ts
|
|
10
10
|
import { resolve } from "path";
|
|
@@ -38,7 +38,12 @@ async function loadConfig(cwd) {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
// src/generate/documents.ts
|
|
41
|
-
import {
|
|
41
|
+
import {
|
|
42
|
+
Kind,
|
|
43
|
+
getLocation,
|
|
44
|
+
print,
|
|
45
|
+
validate
|
|
46
|
+
} from "graphql";
|
|
42
47
|
function collectFragments(documents) {
|
|
43
48
|
const fragments = /* @__PURE__ */ new Map();
|
|
44
49
|
for (const doc of documents) {
|
|
@@ -116,6 +121,32 @@ function sortFragmentsByDependency(fragments) {
|
|
|
116
121
|
}
|
|
117
122
|
return sorted;
|
|
118
123
|
}
|
|
124
|
+
function validateDocuments(schema, fragments, operations) {
|
|
125
|
+
const combined = {
|
|
126
|
+
kind: Kind.DOCUMENT,
|
|
127
|
+
definitions: [...fragments.values(), ...operations]
|
|
128
|
+
};
|
|
129
|
+
const errors = validate(schema, combined);
|
|
130
|
+
if (errors.length === 0) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const formatted = errors.map((err) => {
|
|
134
|
+
const locations = (err.nodes ?? []).map((node) => {
|
|
135
|
+
if (!node.loc) {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
const { source, start } = node.loc;
|
|
139
|
+
const { line, column } = getLocation(source, start);
|
|
140
|
+
return ` at ${source.name}:${line}:${column}`;
|
|
141
|
+
}).filter((s) => s !== null);
|
|
142
|
+
return locations.length > 0 ? ` ${err.message}
|
|
143
|
+
${locations.join("\n")}` : ` ${err.message}`;
|
|
144
|
+
});
|
|
145
|
+
throw new Error(`graphql validation failed:
|
|
146
|
+
|
|
147
|
+
${formatted.join("\n\n")}
|
|
148
|
+
`);
|
|
149
|
+
}
|
|
119
150
|
function generateDocumentString(operation, allFragments) {
|
|
120
151
|
const usedFragments = /* @__PURE__ */ new Map();
|
|
121
152
|
collectUsedFragments(operation, allFragments, usedFragments);
|
|
@@ -802,9 +833,10 @@ async function main() {
|
|
|
802
833
|
console.warn(` ${yellow("\u26A0")} no documents found matching "${project.documents}"`);
|
|
803
834
|
continue;
|
|
804
835
|
}
|
|
805
|
-
const documents = documentPaths.map((p) => parse2(readFileSync(resolve2(cwd, p), "utf-8")));
|
|
836
|
+
const documents = documentPaths.map((p) => parse2(new Source(readFileSync(resolve2(cwd, p), "utf-8"), p)));
|
|
806
837
|
const fragments = collectFragments(documents);
|
|
807
838
|
const operations = collectOperations(documents);
|
|
839
|
+
validateDocuments(schema, fragments, operations);
|
|
808
840
|
const transformedFragments = new Map(
|
|
809
841
|
[...fragments].map(([name, frag]) => [name, transformFragment(schema, frag, fragments)])
|
|
810
842
|
);
|
package/dist/index.cjs
CHANGED
|
@@ -339,15 +339,29 @@ function resolveRefs(refs, flatMap) {
|
|
|
339
339
|
if (!ref._flatId) return [];
|
|
340
340
|
const component = flatMap.get(ref._flatId);
|
|
341
341
|
if (!component) return [];
|
|
342
|
-
|
|
343
|
-
if (Array.isArray(value) && value.length > 0 && value[0]?._flatId !== void 0) {
|
|
344
|
-
component[key] = resolveRefs(value, flatMap);
|
|
345
|
-
}
|
|
346
|
-
}
|
|
342
|
+
resolveNestedRefs(component, flatMap);
|
|
347
343
|
transformResponse(component);
|
|
348
344
|
return [component];
|
|
349
345
|
});
|
|
350
346
|
}
|
|
347
|
+
function resolveNestedRefs(value, flatMap) {
|
|
348
|
+
if (!value || typeof value !== "object") return;
|
|
349
|
+
if (Array.isArray(value)) {
|
|
350
|
+
for (const item of value) {
|
|
351
|
+
resolveNestedRefs(item, flatMap);
|
|
352
|
+
}
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
const obj = value;
|
|
356
|
+
for (const key of Object.keys(obj)) {
|
|
357
|
+
const inner = obj[key];
|
|
358
|
+
if (Array.isArray(inner) && inner.length > 0 && inner[0]?._flatId !== void 0) {
|
|
359
|
+
obj[key] = resolveRefs(inner, flatMap);
|
|
360
|
+
} else if (inner && typeof inner === "object") {
|
|
361
|
+
resolveNestedRefs(inner, flatMap);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
351
365
|
|
|
352
366
|
// src/rpc.ts
|
|
353
367
|
function buildRpc(operations, client) {
|
package/dist/index.js
CHANGED
|
@@ -302,15 +302,29 @@ function resolveRefs(refs, flatMap) {
|
|
|
302
302
|
if (!ref._flatId) return [];
|
|
303
303
|
const component = flatMap.get(ref._flatId);
|
|
304
304
|
if (!component) return [];
|
|
305
|
-
|
|
306
|
-
if (Array.isArray(value) && value.length > 0 && value[0]?._flatId !== void 0) {
|
|
307
|
-
component[key] = resolveRefs(value, flatMap);
|
|
308
|
-
}
|
|
309
|
-
}
|
|
305
|
+
resolveNestedRefs(component, flatMap);
|
|
310
306
|
transformResponse(component);
|
|
311
307
|
return [component];
|
|
312
308
|
});
|
|
313
309
|
}
|
|
310
|
+
function resolveNestedRefs(value, flatMap) {
|
|
311
|
+
if (!value || typeof value !== "object") return;
|
|
312
|
+
if (Array.isArray(value)) {
|
|
313
|
+
for (const item of value) {
|
|
314
|
+
resolveNestedRefs(item, flatMap);
|
|
315
|
+
}
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
const obj = value;
|
|
319
|
+
for (const key of Object.keys(obj)) {
|
|
320
|
+
const inner = obj[key];
|
|
321
|
+
if (Array.isArray(inner) && inner.length > 0 && inner[0]?._flatId !== void 0) {
|
|
322
|
+
obj[key] = resolveRefs(inner, flatMap);
|
|
323
|
+
} else if (inner && typeof inner === "object") {
|
|
324
|
+
resolveNestedRefs(inner, flatMap);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
314
328
|
|
|
315
329
|
// src/rpc.ts
|
|
316
330
|
function buildRpc(operations, client) {
|