protoc-gen-pothos 0.6.2 → 0.7.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/dist/protoc-gen-pothos.cjs +582 -265
- package/dist/protoc-gen-pothos.cjs.map +1 -1
- package/dist/protoc-gen-pothos.js +561 -244
- package/dist/protoc-gen-pothos.js.map +1 -1
- package/dist/src/codegen/__tests__/code-builder.test.d.ts +2 -0
- package/dist/src/codegen/__tests__/code-builder.test.d.ts.map +1 -0
- package/dist/src/codegen/__tests__/helpers.test.d.ts +2 -0
- package/dist/src/codegen/__tests__/helpers.test.d.ts.map +1 -0
- package/dist/src/codegen/__tests__/stringify.test.d.ts +2 -0
- package/dist/src/codegen/__tests__/stringify.test.d.ts.map +1 -0
- package/dist/src/codegen/code-builder.d.ts +11 -0
- package/dist/src/codegen/code-builder.d.ts.map +1 -0
- package/dist/src/codegen/helpers.d.ts +10 -0
- package/dist/src/codegen/helpers.d.ts.map +1 -0
- package/dist/src/codegen/index.d.ts +6 -0
- package/dist/src/codegen/index.d.ts.map +1 -0
- package/dist/src/codegen/stringify.d.ts +2 -0
- package/dist/src/codegen/stringify.d.ts.map +1 -0
- package/dist/src/codegen/types.d.ts +5 -0
- package/dist/src/codegen/types.d.ts.map +1 -0
- package/dist/src/dslgen/index.d.ts +1 -1
- package/dist/src/dslgen/index.d.ts.map +1 -1
- package/dist/src/dslgen/printers/__tests__/test-utils.d.ts +12 -0
- package/dist/src/dslgen/printers/__tests__/test-utils.d.ts.map +1 -0
- package/dist/src/dslgen/printers/enumType.d.ts +2 -2
- package/dist/src/dslgen/printers/enumType.d.ts.map +1 -1
- package/dist/src/dslgen/printers/enumType.test.d.ts +2 -0
- package/dist/src/dslgen/printers/enumType.test.d.ts.map +1 -0
- package/dist/src/dslgen/printers/field.d.ts +3 -3
- package/dist/src/dslgen/printers/field.d.ts.map +1 -1
- package/dist/src/dslgen/printers/fieldResolver/enumFieldResolver.d.ts +3 -3
- package/dist/src/dslgen/printers/fieldResolver/enumFieldResolver.d.ts.map +1 -1
- package/dist/src/dslgen/printers/fieldResolver/nonNullResolver.d.ts +2 -2
- package/dist/src/dslgen/printers/fieldResolver/nonNullResolver.d.ts.map +1 -1
- package/dist/src/dslgen/printers/fieldResolver/oneofUnionResolver.d.ts +2 -2
- package/dist/src/dslgen/printers/fieldResolver/oneofUnionResolver.d.ts.map +1 -1
- package/dist/src/dslgen/printers/index.d.ts +2 -2
- package/dist/src/dslgen/printers/index.d.ts.map +1 -1
- package/dist/src/dslgen/printers/inputObjectType.d.ts +2 -2
- package/dist/src/dslgen/printers/inputObjectType.d.ts.map +1 -1
- package/dist/src/dslgen/printers/inputObjectType.test.d.ts +2 -0
- package/dist/src/dslgen/printers/inputObjectType.test.d.ts.map +1 -0
- package/dist/src/dslgen/printers/objectType.d.ts +2 -2
- package/dist/src/dslgen/printers/objectType.d.ts.map +1 -1
- package/dist/src/dslgen/printers/objectType.test.d.ts +2 -0
- package/dist/src/dslgen/printers/objectType.test.d.ts.map +1 -0
- package/dist/src/dslgen/printers/oneofUnionType.d.ts +2 -2
- package/dist/src/dslgen/printers/oneofUnionType.d.ts.map +1 -1
- package/dist/src/dslgen/printers/oneofUnionType.test.d.ts +2 -0
- package/dist/src/dslgen/printers/oneofUnionType.test.d.ts.map +1 -0
- package/dist/src/dslgen/printers/util.d.ts +41 -7
- package/dist/src/dslgen/printers/util.d.ts.map +1 -1
- package/dist/src/dslgen/printers/util.test.d.ts +2 -0
- package/dist/src/dslgen/printers/util.test.d.ts.map +1 -0
- package/dist/src/plugin.d.ts.map +1 -1
- package/dist/src/printer.d.ts.map +1 -1
- package/docs/README.md +80 -0
- package/docs/buf-setup.md +213 -0
- package/docs/configuration.md +153 -0
- package/docs/generated-code-reference/README.md +13 -0
- package/docs/generated-code-reference/protobuf-es-v1.md +691 -0
- package/docs/generated-code-reference/protobuf-es.md +793 -0
- package/docs/generated-code-reference/ts-proto.md +678 -0
- package/docs/getting-started.md +123 -0
- package/package.json +22 -10
- package/src/codegen/code-builder.ts +65 -0
- package/src/codegen/helpers.ts +134 -0
- package/src/codegen/index.ts +9 -0
- package/src/codegen/stringify.ts +22 -0
- package/src/codegen/types.ts +17 -0
- package/src/dslgen/index.ts +1 -1
- package/src/dslgen/printers/__snapshots__/enumType.test.ts.snap +381 -0
- package/src/dslgen/printers/__snapshots__/inputObjectType.test.ts.snap +1882 -0
- package/src/dslgen/printers/__snapshots__/objectType.test.ts.snap +1857 -0
- package/src/dslgen/printers/__snapshots__/oneofUnionType.test.ts.snap +327 -0
- package/src/dslgen/printers/enumType.ts +22 -13
- package/src/dslgen/printers/field.ts +34 -15
- package/src/dslgen/printers/fieldResolver/enumFieldResolver.ts +19 -25
- package/src/dslgen/printers/fieldResolver/nonNullResolver.ts +2 -2
- package/src/dslgen/printers/fieldResolver/oneofUnionResolver.ts +45 -42
- package/src/dslgen/printers/index.ts +5 -5
- package/src/dslgen/printers/inputObjectType.ts +135 -99
- package/src/dslgen/printers/objectType.ts +37 -27
- package/src/dslgen/printers/oneofUnionType.ts +15 -11
- package/src/dslgen/printers/util.ts +293 -46
- package/src/plugin.ts +6 -2
- package/src/printer.ts +18 -42
- package/dist/src/plugin.test.d.ts +0 -2
- package/dist/src/plugin.test.d.ts.map +0 -1
- package/src/__snapshots__/plugin.test.ts.snap +0 -50903
|
@@ -24,72 +24,418 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
24
24
|
));
|
|
25
25
|
|
|
26
26
|
// src/protoc-gen-pothos.ts
|
|
27
|
-
var
|
|
27
|
+
var import_protoplugin4 = require("@bufbuild/protoplugin");
|
|
28
28
|
|
|
29
29
|
// src/plugin.ts
|
|
30
|
-
var
|
|
30
|
+
var import_protoplugin3 = require("@bufbuild/protoplugin");
|
|
31
31
|
var import_protoc_plugin_helpers = require("@proto-graphql/protoc-plugin-helpers");
|
|
32
32
|
|
|
33
33
|
// package.json
|
|
34
|
-
var version = "0.
|
|
34
|
+
var version = "0.7.1";
|
|
35
|
+
|
|
36
|
+
// src/codegen/stringify.ts
|
|
37
|
+
var import_dprint_node = __toESM(require("dprint-node"), 1);
|
|
38
|
+
function formatCode(code2) {
|
|
39
|
+
return import_dprint_node.default.format("file.ts", code2, {
|
|
40
|
+
lineWidth: 80,
|
|
41
|
+
indentWidth: 2,
|
|
42
|
+
useTabs: false,
|
|
43
|
+
semiColons: "always",
|
|
44
|
+
quoteStyle: "alwaysDouble",
|
|
45
|
+
quoteProps: "asNeeded",
|
|
46
|
+
newLineKind: "lf",
|
|
47
|
+
useBraces: "whenNotSingleLine",
|
|
48
|
+
bracePosition: "sameLineUnlessHanging",
|
|
49
|
+
singleBodyPosition: "maintain",
|
|
50
|
+
nextControlFlowPosition: "sameLine",
|
|
51
|
+
trailingCommas: "onlyMultiLine",
|
|
52
|
+
operatorPosition: "nextLine",
|
|
53
|
+
preferHanging: false,
|
|
54
|
+
preferSingleLine: true,
|
|
55
|
+
"arrowFunction.useParentheses": "force"
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
__name(formatCode, "formatCode");
|
|
35
59
|
|
|
36
60
|
// src/printer.ts
|
|
37
|
-
var
|
|
61
|
+
var import_codegen_core9 = require("@proto-graphql/codegen-core");
|
|
38
62
|
|
|
39
63
|
// src/dslgen/printers/index.ts
|
|
40
|
-
var
|
|
64
|
+
var import_codegen_core8 = require("@proto-graphql/codegen-core");
|
|
41
65
|
|
|
42
66
|
// src/dslgen/printers/enumType.ts
|
|
43
67
|
var import_codegen_core2 = require("@proto-graphql/codegen-core");
|
|
44
|
-
|
|
68
|
+
|
|
69
|
+
// src/codegen/code-builder.ts
|
|
70
|
+
var PRINTABLE_ARRAY_MARKER = /* @__PURE__ */ Symbol.for("proto-graphql:printable-array");
|
|
71
|
+
function isPrintableArray(value) {
|
|
72
|
+
return Array.isArray(value) && PRINTABLE_ARRAY_MARKER in value;
|
|
73
|
+
}
|
|
74
|
+
__name(isPrintableArray, "isPrintableArray");
|
|
75
|
+
function markAsPrintableArray(arr) {
|
|
76
|
+
Object.defineProperty(arr, PRINTABLE_ARRAY_MARKER, {
|
|
77
|
+
value: true,
|
|
78
|
+
enumerable: false,
|
|
79
|
+
writable: false,
|
|
80
|
+
configurable: false
|
|
81
|
+
});
|
|
82
|
+
return arr;
|
|
83
|
+
}
|
|
84
|
+
__name(markAsPrintableArray, "markAsPrintableArray");
|
|
85
|
+
function code(strings, ...values) {
|
|
86
|
+
const result = [];
|
|
87
|
+
for (let i = 0; i < strings.length; i++) {
|
|
88
|
+
if (strings[i] !== "") {
|
|
89
|
+
result.push(strings[i]);
|
|
90
|
+
}
|
|
91
|
+
if (i < values.length) {
|
|
92
|
+
const value = values[i];
|
|
93
|
+
if (value == null) {
|
|
94
|
+
result.push(String(value));
|
|
95
|
+
} else if (typeof value === "string") {
|
|
96
|
+
if (value !== "") {
|
|
97
|
+
result.push(value);
|
|
98
|
+
}
|
|
99
|
+
} else if (typeof value === "number") {
|
|
100
|
+
result.push(String(value));
|
|
101
|
+
} else if (Array.isArray(value)) {
|
|
102
|
+
result.push(...value);
|
|
103
|
+
} else {
|
|
104
|
+
result.push(value);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (result.length === 0) {
|
|
109
|
+
result.push("");
|
|
110
|
+
}
|
|
111
|
+
return markAsPrintableArray(result);
|
|
112
|
+
}
|
|
113
|
+
__name(code, "code");
|
|
114
|
+
|
|
115
|
+
// src/codegen/types.ts
|
|
116
|
+
var import_protoplugin = require("@bufbuild/protoplugin");
|
|
117
|
+
function isImportSymbol(value) {
|
|
118
|
+
return typeof value === "object" && value !== null && "kind" in value && value.kind === "es_symbol";
|
|
119
|
+
}
|
|
120
|
+
__name(isImportSymbol, "isImportSymbol");
|
|
121
|
+
|
|
122
|
+
// src/codegen/helpers.ts
|
|
123
|
+
function joinCode(codes, separator) {
|
|
124
|
+
if (codes.length === 0) {
|
|
125
|
+
return [];
|
|
126
|
+
}
|
|
127
|
+
const result = [];
|
|
128
|
+
for (let i = 0; i < codes.length; i++) {
|
|
129
|
+
result.push(...codes[i]);
|
|
130
|
+
if (separator !== void 0 && i < codes.length - 1) {
|
|
131
|
+
result.push(separator);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return result;
|
|
135
|
+
}
|
|
136
|
+
__name(joinCode, "joinCode");
|
|
137
|
+
function escapeString(str) {
|
|
138
|
+
return str.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t");
|
|
139
|
+
}
|
|
140
|
+
__name(escapeString, "escapeString");
|
|
141
|
+
function compactForCodegen(v) {
|
|
142
|
+
if (typeof v !== "object") return v;
|
|
143
|
+
if (v == null) return v;
|
|
144
|
+
if (Array.isArray(v)) {
|
|
145
|
+
if (isPrintableArray(v)) {
|
|
146
|
+
return v;
|
|
147
|
+
}
|
|
148
|
+
return v.map(compactForCodegen);
|
|
149
|
+
}
|
|
150
|
+
return compactObjForCodegen(v);
|
|
151
|
+
}
|
|
152
|
+
__name(compactForCodegen, "compactForCodegen");
|
|
153
|
+
function compactObjForCodegen(obj) {
|
|
154
|
+
const result = {};
|
|
155
|
+
for (const key of Object.keys(obj)) {
|
|
156
|
+
const v = obj[key];
|
|
157
|
+
if (v != null) {
|
|
158
|
+
result[key] = compactForCodegen(v);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return result;
|
|
162
|
+
}
|
|
163
|
+
__name(compactObjForCodegen, "compactObjForCodegen");
|
|
164
|
+
function literalOf(value) {
|
|
165
|
+
if (value === null) {
|
|
166
|
+
return markAsPrintableArray([
|
|
167
|
+
"null"
|
|
168
|
+
]);
|
|
169
|
+
}
|
|
170
|
+
if (value === void 0) {
|
|
171
|
+
return markAsPrintableArray([
|
|
172
|
+
"undefined"
|
|
173
|
+
]);
|
|
174
|
+
}
|
|
175
|
+
if (typeof value === "string") {
|
|
176
|
+
return markAsPrintableArray([
|
|
177
|
+
`"${escapeString(value)}"`
|
|
178
|
+
]);
|
|
179
|
+
}
|
|
180
|
+
if (typeof value === "number") {
|
|
181
|
+
return markAsPrintableArray([
|
|
182
|
+
String(value)
|
|
183
|
+
]);
|
|
184
|
+
}
|
|
185
|
+
if (typeof value === "boolean") {
|
|
186
|
+
return markAsPrintableArray([
|
|
187
|
+
String(value)
|
|
188
|
+
]);
|
|
189
|
+
}
|
|
190
|
+
if (Array.isArray(value)) {
|
|
191
|
+
if (isPrintableArray(value)) {
|
|
192
|
+
return value;
|
|
193
|
+
}
|
|
194
|
+
if (value.length === 0) {
|
|
195
|
+
return markAsPrintableArray([
|
|
196
|
+
"[]"
|
|
197
|
+
]);
|
|
198
|
+
}
|
|
199
|
+
const elements = [
|
|
200
|
+
"["
|
|
201
|
+
];
|
|
202
|
+
for (let i = 0; i < value.length; i++) {
|
|
203
|
+
if (i > 0) {
|
|
204
|
+
elements.push(", ");
|
|
205
|
+
}
|
|
206
|
+
elements.push(...literalOf(value[i]));
|
|
207
|
+
}
|
|
208
|
+
elements.push("]");
|
|
209
|
+
return markAsPrintableArray(elements);
|
|
210
|
+
}
|
|
211
|
+
if (typeof value === "object") {
|
|
212
|
+
if (isImportSymbol(value)) {
|
|
213
|
+
return markAsPrintableArray([
|
|
214
|
+
value
|
|
215
|
+
]);
|
|
216
|
+
}
|
|
217
|
+
const obj = value;
|
|
218
|
+
const keys = Object.keys(obj);
|
|
219
|
+
if (keys.length === 0) {
|
|
220
|
+
return markAsPrintableArray([
|
|
221
|
+
"{}"
|
|
222
|
+
]);
|
|
223
|
+
}
|
|
224
|
+
const elements = [
|
|
225
|
+
"{ "
|
|
226
|
+
];
|
|
227
|
+
for (let i = 0; i < keys.length; i++) {
|
|
228
|
+
if (i > 0) {
|
|
229
|
+
elements.push(", ");
|
|
230
|
+
}
|
|
231
|
+
const key = keys[i];
|
|
232
|
+
elements.push(`"${escapeString(key)}": `);
|
|
233
|
+
elements.push(...literalOf(obj[key]));
|
|
234
|
+
}
|
|
235
|
+
elements.push(" }");
|
|
236
|
+
return markAsPrintableArray(elements);
|
|
237
|
+
}
|
|
238
|
+
return markAsPrintableArray([
|
|
239
|
+
String(value)
|
|
240
|
+
]);
|
|
241
|
+
}
|
|
242
|
+
__name(literalOf, "literalOf");
|
|
45
243
|
|
|
46
244
|
// src/dslgen/printers/util.ts
|
|
47
245
|
var path = __toESM(require("path"), 1);
|
|
246
|
+
var import_reflect = require("@bufbuild/protobuf/reflect");
|
|
247
|
+
var import_protoplugin2 = require("@bufbuild/protoplugin");
|
|
48
248
|
var import_codegen_core = require("@proto-graphql/codegen-core");
|
|
49
|
-
var import_ts_poet = require("ts-poet");
|
|
50
|
-
function pothosRef(type) {
|
|
51
|
-
return import_ts_poet.code`${pothosRefName(type)}`;
|
|
52
|
-
}
|
|
53
|
-
__name(pothosRef, "pothosRef");
|
|
54
249
|
function pothosRefName(type) {
|
|
55
250
|
return `${type.typeName}$Ref`;
|
|
56
251
|
}
|
|
57
252
|
__name(pothosRefName, "pothosRefName");
|
|
58
|
-
function shapeType(type) {
|
|
59
|
-
return import_ts_poet.code`${shapeTypeName(type)}`;
|
|
60
|
-
}
|
|
61
|
-
__name(shapeType, "shapeType");
|
|
62
253
|
function shapeTypeName(type) {
|
|
63
254
|
return `${type.typeName}$Shape`;
|
|
64
255
|
}
|
|
65
256
|
__name(shapeTypeName, "shapeTypeName");
|
|
66
|
-
function
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return
|
|
72
|
-
}
|
|
73
|
-
__name(
|
|
74
|
-
function
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return
|
|
85
|
-
}
|
|
86
|
-
__name(
|
|
257
|
+
function resolveImportOrLocal(field, opts, symbolName, localPrintable) {
|
|
258
|
+
const fromFile = (0, import_codegen_core.filename)(field.parent, opts);
|
|
259
|
+
const toFile = (0, import_codegen_core.filename)(field.type, opts);
|
|
260
|
+
if (fromFile === toFile) return localPrintable();
|
|
261
|
+
const importPath = `./${toFile.replace(/\.ts$/, "")}`;
|
|
262
|
+
return code`${(0, import_protoplugin.createImportSymbol)(symbolName, importPath)}`;
|
|
263
|
+
}
|
|
264
|
+
__name(resolveImportOrLocal, "resolveImportOrLocal");
|
|
265
|
+
function fieldTypeRefPrintable(field, opts) {
|
|
266
|
+
if (field instanceof import_codegen_core.ObjectOneofField) return pothosRefPrintable(field.type);
|
|
267
|
+
return resolveImportOrLocal(field, opts, pothosRefName(field.type), () => pothosRefPrintable(field.type));
|
|
268
|
+
}
|
|
269
|
+
__name(fieldTypeRefPrintable, "fieldTypeRefPrintable");
|
|
270
|
+
function shapeTypePrintable(type) {
|
|
271
|
+
return code`${shapeTypeName(type)}`;
|
|
272
|
+
}
|
|
273
|
+
__name(shapeTypePrintable, "shapeTypePrintable");
|
|
274
|
+
function fieldTypeShapePrintable(field, opts) {
|
|
275
|
+
return resolveImportOrLocal(field, opts, shapeTypeName(field.type), () => shapeTypePrintable(field.type));
|
|
276
|
+
}
|
|
277
|
+
__name(fieldTypeShapePrintable, "fieldTypeShapePrintable");
|
|
278
|
+
function toProtoFuncName(type) {
|
|
279
|
+
return `${type.typeName}$toProto`;
|
|
280
|
+
}
|
|
281
|
+
__name(toProtoFuncName, "toProtoFuncName");
|
|
282
|
+
function toProtoFuncPrintable(field, opts) {
|
|
283
|
+
return resolveImportOrLocal(field, opts, toProtoFuncName(field.type), () => code`${toProtoFuncName(field.type)}`);
|
|
284
|
+
}
|
|
285
|
+
__name(toProtoFuncPrintable, "toProtoFuncPrintable");
|
|
286
|
+
function pothosRefPrintable(type) {
|
|
287
|
+
return code`${pothosRefName(type)}`;
|
|
288
|
+
}
|
|
289
|
+
__name(pothosRefPrintable, "pothosRefPrintable");
|
|
290
|
+
function pothosBuilderPrintable(opts) {
|
|
291
|
+
return code`${(0, import_protoplugin.createImportSymbol)("builder", opts.pothos.builderPath)}`;
|
|
292
|
+
}
|
|
293
|
+
__name(pothosBuilderPrintable, "pothosBuilderPrintable");
|
|
294
|
+
function protoTypeSymbol(proto, opts) {
|
|
295
|
+
const chunks = [
|
|
296
|
+
proto.name
|
|
297
|
+
];
|
|
298
|
+
let current = proto;
|
|
299
|
+
while (current.parent != null) {
|
|
300
|
+
current = current.parent;
|
|
301
|
+
chunks.unshift(current.name);
|
|
302
|
+
}
|
|
303
|
+
const symbolName = chunks.join("_");
|
|
304
|
+
const importPath = protoImportPath(proto, opts);
|
|
305
|
+
return (0, import_protoplugin.createImportSymbol)(symbolName, importPath);
|
|
306
|
+
}
|
|
307
|
+
__name(protoTypeSymbol, "protoTypeSymbol");
|
|
308
|
+
function protoSchemaSymbol(proto, opts) {
|
|
309
|
+
const symbolName = resolveProtobufEsDescName(proto);
|
|
310
|
+
const importPath = protoImportPath(proto, opts);
|
|
311
|
+
return (0, import_protoplugin.createImportSymbol)(symbolName, importPath);
|
|
312
|
+
}
|
|
313
|
+
__name(protoSchemaSymbol, "protoSchemaSymbol");
|
|
314
|
+
function protobufEsSalt(i) {
|
|
315
|
+
if (i === 0) return "";
|
|
316
|
+
if (i === 1) return "$";
|
|
317
|
+
return `$${i - 1}`;
|
|
318
|
+
}
|
|
319
|
+
__name(protobufEsSalt, "protobufEsSalt");
|
|
320
|
+
function idealShapeName(desc, i) {
|
|
321
|
+
return (0, import_protoplugin2.safeIdentifier)(protobufEsIdentifierForNested(desc) + protobufEsSalt(i));
|
|
322
|
+
}
|
|
323
|
+
__name(idealShapeName, "idealShapeName");
|
|
324
|
+
function protobufEsIdentifierForNested(desc) {
|
|
325
|
+
const pkg = desc.file.proto.package;
|
|
326
|
+
const offset = pkg.length > 0 ? pkg.length + 1 : 0;
|
|
327
|
+
return desc.typeName.substring(offset).replace(/\./g, "_");
|
|
328
|
+
}
|
|
329
|
+
__name(protobufEsIdentifierForNested, "protobufEsIdentifierForNested");
|
|
330
|
+
function idealDescName(desc, i) {
|
|
331
|
+
const salt = protobufEsSalt(i);
|
|
332
|
+
if (desc.kind === "file") {
|
|
333
|
+
const name = `file_${desc.name.replace(/[^a-zA-Z0-9_]+/g, "_")}`;
|
|
334
|
+
return (0, import_protoplugin2.safeIdentifier)(name + salt);
|
|
335
|
+
}
|
|
336
|
+
if (desc.kind === "enum" || desc.kind === "message") {
|
|
337
|
+
return (0, import_protoplugin2.safeIdentifier)(protobufEsIdentifierForNested(desc) + "Schema" + salt);
|
|
338
|
+
}
|
|
339
|
+
return (0, import_protoplugin2.safeIdentifier)(protobufEsIdentifierForNested(desc) + salt);
|
|
340
|
+
}
|
|
341
|
+
__name(idealDescName, "idealDescName");
|
|
342
|
+
var descNameCache = /* @__PURE__ */ new WeakMap();
|
|
343
|
+
function resolveProtobufEsDescName(proto) {
|
|
344
|
+
const file = proto.file;
|
|
345
|
+
const cached = descNameCache.get(file);
|
|
346
|
+
if (cached) {
|
|
347
|
+
const name = cached.get(proto);
|
|
348
|
+
if (name !== void 0) return name;
|
|
349
|
+
}
|
|
350
|
+
const taken = /* @__PURE__ */ new Set();
|
|
351
|
+
const descNames = /* @__PURE__ */ new Map();
|
|
352
|
+
for (const desc of (0, import_reflect.nestedTypes)(file)) {
|
|
353
|
+
if (desc.kind !== "enum" && desc.kind !== "message") continue;
|
|
354
|
+
let name;
|
|
355
|
+
for (let i = 0; ; i++) {
|
|
356
|
+
name = idealShapeName(desc, i);
|
|
357
|
+
if (!taken.has(name)) break;
|
|
358
|
+
}
|
|
359
|
+
taken.add(name);
|
|
360
|
+
}
|
|
361
|
+
{
|
|
362
|
+
let fileName;
|
|
363
|
+
for (let i = 0; ; i++) {
|
|
364
|
+
fileName = idealDescName(file, i);
|
|
365
|
+
if (!taken.has(fileName)) break;
|
|
366
|
+
}
|
|
367
|
+
taken.add(fileName);
|
|
368
|
+
}
|
|
369
|
+
for (const desc of (0, import_reflect.nestedTypes)(file)) {
|
|
370
|
+
let name;
|
|
371
|
+
for (let i = 0; ; i++) {
|
|
372
|
+
name = idealDescName(desc, i);
|
|
373
|
+
if (!taken.has(name)) break;
|
|
374
|
+
}
|
|
375
|
+
taken.add(name);
|
|
376
|
+
if (desc.kind === "message" || desc.kind === "enum") {
|
|
377
|
+
descNames.set(desc, name);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
descNameCache.set(file, descNames);
|
|
381
|
+
const result = descNames.get(proto);
|
|
382
|
+
if (result === void 0) {
|
|
383
|
+
throw new Error(`Unable to determine protobuf-es desc name for ${proto.toString()}`);
|
|
384
|
+
}
|
|
385
|
+
return result;
|
|
386
|
+
}
|
|
387
|
+
__name(resolveProtobufEsDescName, "resolveProtobufEsDescName");
|
|
388
|
+
function protobufCreateSymbol() {
|
|
389
|
+
return (0, import_protoplugin.createImportSymbol)("create", "@bufbuild/protobuf");
|
|
390
|
+
}
|
|
391
|
+
__name(protobufCreateSymbol, "protobufCreateSymbol");
|
|
392
|
+
function protobufIsMessageSymbol() {
|
|
393
|
+
return (0, import_protoplugin.createImportSymbol)("isMessage", "@bufbuild/protobuf");
|
|
394
|
+
}
|
|
395
|
+
__name(protobufIsMessageSymbol, "protobufIsMessageSymbol");
|
|
396
|
+
function protobufMessageShapeSymbol() {
|
|
397
|
+
return (0, import_protoplugin.createImportSymbol)("MessageShape", "@bufbuild/protobuf");
|
|
398
|
+
}
|
|
399
|
+
__name(protobufMessageShapeSymbol, "protobufMessageShapeSymbol");
|
|
400
|
+
function protoRefTypePrintable(proto, opts) {
|
|
401
|
+
switch (opts.protobuf) {
|
|
402
|
+
case "ts-proto":
|
|
403
|
+
case "protobuf-es-v1": {
|
|
404
|
+
return code`${protoTypeSymbol(proto, opts)}`;
|
|
405
|
+
}
|
|
406
|
+
case "protobuf-es": {
|
|
407
|
+
return code`${protobufMessageShapeSymbol()}<typeof ${protoSchemaSymbol(proto, opts)}>`;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
__name(protoRefTypePrintable, "protoRefTypePrintable");
|
|
412
|
+
function protoImportPath(t, o) {
|
|
413
|
+
let importPath;
|
|
414
|
+
switch (o.protobuf) {
|
|
415
|
+
case "ts-proto": {
|
|
416
|
+
importPath = t.file.name;
|
|
417
|
+
break;
|
|
418
|
+
}
|
|
419
|
+
case "protobuf-es-v1":
|
|
420
|
+
case "protobuf-es": {
|
|
421
|
+
const { dir, name } = path.parse(t.file.name);
|
|
422
|
+
importPath = `${dir}/${name}_pb`;
|
|
423
|
+
break;
|
|
424
|
+
}
|
|
425
|
+
default: {
|
|
426
|
+
o.protobuf;
|
|
427
|
+
throw new Error(`unexpected protobuf option: ${o.protobuf}`);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return `${o.importPrefix ? `${o.importPrefix}/` : "./"}${importPath}`.replace(/(?<!:)\/\//, "/");
|
|
431
|
+
}
|
|
432
|
+
__name(protoImportPath, "protoImportPath");
|
|
87
433
|
|
|
88
434
|
// src/dslgen/printers/enumType.ts
|
|
89
435
|
function createEnumTypeCode(type, registry, opts) {
|
|
90
436
|
const typeOpts = {
|
|
91
437
|
description: type.description,
|
|
92
|
-
values:
|
|
438
|
+
values: code`{${joinCode(type.values.filter((v) => !v.isIgnored() && !v.isUnespecified()).map((ev) => code`${ev.name}: ${literalOf(compactForCodegen({
|
|
93
439
|
description: ev.description,
|
|
94
440
|
deprecationReason: ev.deprecationReason,
|
|
95
441
|
value: ev.number,
|
|
@@ -97,41 +443,38 @@ function createEnumTypeCode(type, registry, opts) {
|
|
|
97
443
|
}))},`))}} as const`,
|
|
98
444
|
extensions: (0, import_codegen_core2.protobufGraphQLExtensions)(type, registry)
|
|
99
445
|
};
|
|
100
|
-
const protoTypeExpr = (
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
446
|
+
const protoTypeExpr = protoTypeSymbol(type.proto, opts);
|
|
447
|
+
const enumRefSymbol = (0, import_protoplugin.createImportSymbol)("EnumRef", "@pothos/core");
|
|
448
|
+
const refTypeExpr = code`${enumRefSymbol}<${protoTypeExpr}, ${protoTypeExpr}>`;
|
|
449
|
+
return code`
|
|
450
|
+
export const ${pothosRefPrintable(type)}: ${refTypeExpr} =
|
|
451
|
+
${pothosBuilderPrintable(opts)}.enumType(${literalOf(type.typeName)}, ${literalOf(compactForCodegen(typeOpts))});
|
|
105
452
|
`;
|
|
106
453
|
}
|
|
107
454
|
__name(createEnumTypeCode, "createEnumTypeCode");
|
|
108
455
|
|
|
109
456
|
// src/dslgen/printers/inputObjectType.ts
|
|
110
|
-
var
|
|
111
|
-
var import_ts_poet7 = require("ts-poet");
|
|
457
|
+
var import_codegen_core5 = require("@proto-graphql/codegen-core");
|
|
112
458
|
|
|
113
459
|
// src/dslgen/printers/field.ts
|
|
114
|
-
var
|
|
115
|
-
var import_ts_poet6 = require("ts-poet");
|
|
460
|
+
var import_codegen_core4 = require("@proto-graphql/codegen-core");
|
|
116
461
|
|
|
117
462
|
// src/dslgen/printers/fieldResolver/enumFieldResolver.ts
|
|
118
|
-
var import_codegen_core3 = require("@proto-graphql/codegen-core");
|
|
119
|
-
var import_ts_poet3 = require("ts-poet");
|
|
120
463
|
function createEnumResolverCode(valueExpr, field, opts) {
|
|
121
464
|
const createBlockStmtCodes = /* @__PURE__ */ __name((valueExpr2) => {
|
|
122
465
|
const chunks = [];
|
|
123
466
|
if (field.type.unspecifiedValue != null) {
|
|
124
|
-
const escapeCode = field.isNullable() && !field.isList() ?
|
|
125
|
-
chunks.push(
|
|
126
|
-
if (${valueExpr2} === ${(
|
|
467
|
+
const escapeCode = field.isNullable() && !field.isList() ? code`return null;` : code`throw new Error("${field.name} is required field. But got unspecified.");`;
|
|
468
|
+
chunks.push(code`
|
|
469
|
+
if (${valueExpr2} === ${protoTypeSymbol(field.type.proto, opts)}.${enumValueJsName(field.type, field.type.unspecifiedValue, opts)}) {
|
|
127
470
|
${escapeCode}
|
|
128
471
|
}
|
|
129
472
|
`);
|
|
130
473
|
}
|
|
131
474
|
for (const ev of field.type.valuesWithIgnored) {
|
|
132
475
|
if (!ev.isIgnored()) continue;
|
|
133
|
-
chunks.push(
|
|
134
|
-
if (${valueExpr2} === ${(
|
|
476
|
+
chunks.push(code`
|
|
477
|
+
if (${valueExpr2} === ${protoTypeSymbol(field.type.proto, opts)}.${enumValueJsName(field.type, ev, opts)}) {
|
|
135
478
|
throw new Error("${ev.name} is ignored in GraphQL schema");
|
|
136
479
|
}
|
|
137
480
|
`);
|
|
@@ -139,51 +482,42 @@ function createEnumResolverCode(valueExpr, field, opts) {
|
|
|
139
482
|
return chunks;
|
|
140
483
|
}, "createBlockStmtCodes");
|
|
141
484
|
if (field.isList()) {
|
|
142
|
-
const stmts = createBlockStmtCodes(
|
|
485
|
+
const stmts = createBlockStmtCodes(code`item`);
|
|
143
486
|
if (stmts.length === 0) {
|
|
144
|
-
return
|
|
487
|
+
return code`return ${valueExpr}`;
|
|
145
488
|
}
|
|
146
|
-
return
|
|
147
|
-
${stmts}
|
|
489
|
+
return code`return ${valueExpr}.map(item => {
|
|
490
|
+
${joinCode(stmts)}
|
|
148
491
|
return item;
|
|
149
492
|
})`;
|
|
150
493
|
}
|
|
151
|
-
return
|
|
152
|
-
${createBlockStmtCodes(valueExpr)}
|
|
494
|
+
return code`
|
|
495
|
+
${joinCode(createBlockStmtCodes(valueExpr))}
|
|
153
496
|
return ${valueExpr};
|
|
154
497
|
`;
|
|
155
498
|
}
|
|
156
499
|
__name(createEnumResolverCode, "createEnumResolverCode");
|
|
157
|
-
function enumValueJsName(
|
|
500
|
+
function enumValueJsName(_et, ev, opts) {
|
|
158
501
|
switch (opts.protobuf) {
|
|
159
502
|
case "ts-proto":
|
|
160
503
|
return ev.proto.name;
|
|
161
|
-
case "protobuf-es":
|
|
504
|
+
case "protobuf-es-v1":
|
|
505
|
+
case "protobuf-es":
|
|
162
506
|
return ev.proto.localName;
|
|
163
|
-
}
|
|
164
|
-
case "protobufjs":
|
|
165
|
-
case "google-protobuf":
|
|
166
|
-
throw new Error(`Unsupported protobuf: ${opts.protobuf}`);
|
|
167
|
-
default: {
|
|
168
|
-
opts;
|
|
169
|
-
throw "unreachable";
|
|
170
|
-
}
|
|
171
507
|
}
|
|
172
508
|
}
|
|
173
509
|
__name(enumValueJsName, "enumValueJsName");
|
|
174
510
|
|
|
175
511
|
// src/dslgen/printers/fieldResolver/nonNullResolver.ts
|
|
176
|
-
var import_ts_poet4 = require("ts-poet");
|
|
177
512
|
function createNonNullResolverCode(valueExpr) {
|
|
178
|
-
return
|
|
513
|
+
return code`
|
|
179
514
|
return ${valueExpr}!;
|
|
180
515
|
`;
|
|
181
516
|
}
|
|
182
517
|
__name(createNonNullResolverCode, "createNonNullResolverCode");
|
|
183
518
|
|
|
184
519
|
// src/dslgen/printers/fieldResolver/oneofUnionResolver.ts
|
|
185
|
-
var
|
|
186
|
-
var import_ts_poet5 = require("ts-poet");
|
|
520
|
+
var import_codegen_core3 = require("@proto-graphql/codegen-core");
|
|
187
521
|
function createOneofUnionResolverCode(sourceExpr, field, opts) {
|
|
188
522
|
const createBlockStmtCode = /* @__PURE__ */ __name((sourceExpr2, { nullable, list }) => {
|
|
189
523
|
switch (opts.protobuf) {
|
|
@@ -192,26 +526,19 @@ function createOneofUnionResolverCode(sourceExpr, field, opts) {
|
|
|
192
526
|
nullable
|
|
193
527
|
});
|
|
194
528
|
}
|
|
529
|
+
case "protobuf-es-v1":
|
|
195
530
|
case "protobuf-es": {
|
|
196
531
|
return createBlockStmtCodeForProtobufEs(sourceExpr2, field, opts, {
|
|
197
532
|
nullable,
|
|
198
533
|
list
|
|
199
534
|
});
|
|
200
535
|
}
|
|
201
|
-
case "google-protobuf":
|
|
202
|
-
case "protobufjs": {
|
|
203
|
-
throw new Error(`Unsupported protobuf: ${opts.protobuf}`);
|
|
204
|
-
}
|
|
205
|
-
default: {
|
|
206
|
-
opts;
|
|
207
|
-
throw "unreachable";
|
|
208
|
-
}
|
|
209
536
|
}
|
|
210
537
|
}, "createBlockStmtCode");
|
|
211
|
-
if (field instanceof
|
|
212
|
-
return
|
|
538
|
+
if (field instanceof import_codegen_core3.ObjectField && field.isList()) {
|
|
539
|
+
return code`
|
|
213
540
|
return ${sourceExpr}.map(item => {
|
|
214
|
-
${createBlockStmtCode(
|
|
541
|
+
${createBlockStmtCode(code`item`, {
|
|
215
542
|
nullable: false,
|
|
216
543
|
list: true
|
|
217
544
|
})}
|
|
@@ -226,15 +553,13 @@ function createOneofUnionResolverCode(sourceExpr, field, opts) {
|
|
|
226
553
|
__name(createOneofUnionResolverCode, "createOneofUnionResolverCode");
|
|
227
554
|
function createBlockStmtCodeForTsProto(sourceExpr, field, opts, { nullable }) {
|
|
228
555
|
const createFieldExpr = /* @__PURE__ */ __name((memberField) => {
|
|
229
|
-
if (field instanceof
|
|
230
|
-
return
|
|
556
|
+
if (field instanceof import_codegen_core3.ObjectOneofField) {
|
|
557
|
+
return code`${sourceExpr}.${(0, import_codegen_core3.tsFieldName)(memberField.proto, opts).toString()}`;
|
|
231
558
|
}
|
|
232
|
-
return
|
|
559
|
+
return code`${sourceExpr}?.${(0, import_codegen_core3.tsFieldName)(memberField.proto, opts).toString()}`;
|
|
233
560
|
}, "createFieldExpr");
|
|
234
|
-
return
|
|
235
|
-
const value = ${
|
|
236
|
-
on: "??"
|
|
237
|
-
})};
|
|
561
|
+
return code`
|
|
562
|
+
const value = ${joinCode(field.type.fields.map(createFieldExpr), " ?? ")};
|
|
238
563
|
if (value == null) {
|
|
239
564
|
${nullable ? "return null" : `throw new Error("${field.name} should not be null")`};
|
|
240
565
|
}
|
|
@@ -244,25 +569,21 @@ function createBlockStmtCodeForTsProto(sourceExpr, field, opts, { nullable }) {
|
|
|
244
569
|
__name(createBlockStmtCodeForTsProto, "createBlockStmtCodeForTsProto");
|
|
245
570
|
function createBlockStmtCodeForProtobufEs(sourceExpr, field, opts, { nullable, list }) {
|
|
246
571
|
let valueExpr;
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
break;
|
|
255
|
-
}
|
|
256
|
-
default: {
|
|
257
|
-
field;
|
|
258
|
-
throw "unreachable";
|
|
259
|
-
}
|
|
572
|
+
if (field instanceof import_codegen_core3.ObjectOneofField) {
|
|
573
|
+
valueExpr = code`${sourceExpr}.${(0, import_codegen_core3.tsFieldName)(field.proto, opts).toString()}.value`;
|
|
574
|
+
} else if (field instanceof import_codegen_core3.ObjectField) {
|
|
575
|
+
valueExpr = code`${sourceExpr}${list ? "" : "?"}.${(0, import_codegen_core3.tsFieldName)(field.type.oneofUnionType.proto, opts).toString()}.value`;
|
|
576
|
+
} else {
|
|
577
|
+
field;
|
|
578
|
+
throw new Error("unreachable");
|
|
260
579
|
}
|
|
580
|
+
const memberTypeSymbols = field.type.fields.map((f) => protoTypeSymbol(f.type.proto, opts));
|
|
581
|
+
const typeAssertion = memberTypeSymbols.length > 0 ? code`${joinCode(memberTypeSymbols.map((s) => code`${s}`), " | ")} | undefined` : code`undefined`;
|
|
261
582
|
if (nullable) {
|
|
262
|
-
return
|
|
583
|
+
return code`return (${valueExpr} ?? null) as ${typeAssertion} | null;`;
|
|
263
584
|
}
|
|
264
|
-
return
|
|
265
|
-
const value = ${valueExpr};
|
|
585
|
+
return code`
|
|
586
|
+
const value = ${valueExpr} as ${typeAssertion};
|
|
266
587
|
if (value == null) {
|
|
267
588
|
throw new Error("${field.name} should not be null");
|
|
268
589
|
}
|
|
@@ -272,40 +593,44 @@ function createBlockStmtCodeForProtobufEs(sourceExpr, field, opts, { nullable, l
|
|
|
272
593
|
__name(createBlockStmtCodeForProtobufEs, "createBlockStmtCodeForProtobufEs");
|
|
273
594
|
|
|
274
595
|
// src/dslgen/printers/field.ts
|
|
596
|
+
function createGetFieldValueCodePrintable(source, proto, opts) {
|
|
597
|
+
return code`${source}.${(0, import_codegen_core4.tsFieldName)(proto, opts)}`;
|
|
598
|
+
}
|
|
599
|
+
__name(createGetFieldValueCodePrintable, "createGetFieldValueCodePrintable");
|
|
275
600
|
function createFieldRefCode(field, registry, opts) {
|
|
276
|
-
const isInput = field instanceof
|
|
277
|
-
const baseType = field.type instanceof
|
|
278
|
-
const sourceExpr =
|
|
601
|
+
const isInput = field instanceof import_codegen_core4.InputObjectField;
|
|
602
|
+
const baseType = field.type instanceof import_codegen_core4.ScalarType ? literalOf(field.type.typeName) : fieldTypeRefPrintable(field, opts);
|
|
603
|
+
const sourceExpr = "source";
|
|
279
604
|
let resolverCode;
|
|
280
605
|
if (!isInput) {
|
|
281
|
-
if (field instanceof
|
|
282
|
-
resolverCode = createOneofUnionResolverCode(sourceExpr
|
|
606
|
+
if (field instanceof import_codegen_core4.ObjectOneofField) {
|
|
607
|
+
resolverCode = createOneofUnionResolverCode(code`${sourceExpr}`, field, opts);
|
|
283
608
|
} else {
|
|
284
|
-
const valueExpr = (
|
|
285
|
-
const nullableInProto = field.type instanceof
|
|
609
|
+
const valueExpr = createGetFieldValueCodePrintable(sourceExpr, field.proto, opts);
|
|
610
|
+
const nullableInProto = field.type instanceof import_codegen_core4.ObjectType || field.type instanceof import_codegen_core4.ScalarType && !field.type.isPrimitive() && !field.type.isWrapperType();
|
|
286
611
|
if (nullableInProto && !field.isNullable()) {
|
|
287
612
|
resolverCode = createNonNullResolverCode(valueExpr);
|
|
288
613
|
}
|
|
289
|
-
if (field.type instanceof
|
|
614
|
+
if (field.type instanceof import_codegen_core4.EnumType && field instanceof import_codegen_core4.ObjectField) {
|
|
290
615
|
resolverCode = createEnumResolverCode(valueExpr, field, opts);
|
|
291
616
|
}
|
|
292
|
-
if (field.type instanceof
|
|
617
|
+
if (field.type instanceof import_codegen_core4.SquashedOneofUnionType && field instanceof import_codegen_core4.ObjectField) {
|
|
293
618
|
resolverCode = createOneofUnionResolverCode(valueExpr, field, opts);
|
|
294
619
|
}
|
|
295
|
-
if (field.type instanceof
|
|
620
|
+
if (field.type instanceof import_codegen_core4.ScalarType && field.type.isBytes()) {
|
|
296
621
|
if (field.isList()) {
|
|
297
|
-
resolverCode =
|
|
622
|
+
resolverCode = code`return ${valueExpr}.map(v => Buffer.from(v));`;
|
|
298
623
|
} else if (field.isNullable()) {
|
|
299
|
-
resolverCode =
|
|
624
|
+
resolverCode = code`return ${valueExpr} == null ? null : Buffer.from(${valueExpr});`;
|
|
300
625
|
} else {
|
|
301
|
-
resolverCode =
|
|
626
|
+
resolverCode = code`return Buffer.from(${valueExpr});`;
|
|
302
627
|
}
|
|
303
628
|
}
|
|
304
629
|
}
|
|
305
630
|
}
|
|
306
631
|
const nullableValue = isInput !== field.isNullable();
|
|
307
632
|
const fieldOpts = {
|
|
308
|
-
type: field.isList() ?
|
|
633
|
+
type: field.isList() ? code`[${baseType}]` : baseType,
|
|
309
634
|
[isInput ? "required" : "nullable"]: field.isList() ? {
|
|
310
635
|
list: nullableValue,
|
|
311
636
|
items: isInput
|
|
@@ -313,15 +638,15 @@ function createFieldRefCode(field, registry, opts) {
|
|
|
313
638
|
} : nullableValue,
|
|
314
639
|
description: field.description,
|
|
315
640
|
deprecationReason: field.deprecationReason,
|
|
316
|
-
resolve: resolverCode ?
|
|
317
|
-
extensions: (0,
|
|
641
|
+
resolve: resolverCode ? code`${sourceExpr} => {${resolverCode}}` : null,
|
|
642
|
+
extensions: (0, import_codegen_core4.protobufGraphQLExtensions)(field, registry)
|
|
318
643
|
};
|
|
319
644
|
const shouldUseFieldFunc = isInput || resolverCode != null;
|
|
320
|
-
return shouldUseFieldFunc ?
|
|
645
|
+
return shouldUseFieldFunc ? code`t.field(${literalOf(compactForCodegen(fieldOpts))})` : code`t.expose(${literalOf((0, import_codegen_core4.tsFieldName)(field.proto, opts))}, ${literalOf(compactForCodegen(fieldOpts))})`;
|
|
321
646
|
}
|
|
322
647
|
__name(createFieldRefCode, "createFieldRefCode");
|
|
323
648
|
function createNoopFieldRefCode(opts) {
|
|
324
|
-
return
|
|
649
|
+
return code`
|
|
325
650
|
t.field({
|
|
326
651
|
type: "Boolean",
|
|
327
652
|
${opts.input ? "required: false" : "nullable: true"},
|
|
@@ -334,192 +659,195 @@ __name(createNoopFieldRefCode, "createNoopFieldRefCode");
|
|
|
334
659
|
|
|
335
660
|
// src/dslgen/printers/inputObjectType.ts
|
|
336
661
|
function createInputObjectTypeCode(type, registry, opts) {
|
|
337
|
-
const
|
|
338
|
-
|
|
339
|
-
|
|
662
|
+
const fields = type.fields.filter((f) => !(type.options.ignoreNonMessageOneofFields && f.proto.oneof != null && !(f.type instanceof import_codegen_core5.InputObjectType)));
|
|
663
|
+
const shapeTypeCode = code`
|
|
664
|
+
export type ${shapeTypePrintable(type)} = {
|
|
665
|
+
${joinCode(fields.map((f) => {
|
|
340
666
|
let typeNode;
|
|
341
|
-
if (f.type instanceof
|
|
342
|
-
typeNode =
|
|
343
|
-
if (f.isList()) typeNode =
|
|
667
|
+
if (f.type instanceof import_codegen_core5.InputObjectType) {
|
|
668
|
+
typeNode = fieldTypeShapePrintable(f, opts);
|
|
669
|
+
if (f.isList()) typeNode = code`Array<${typeNode}>`;
|
|
344
670
|
} else {
|
|
345
|
-
typeNode =
|
|
671
|
+
typeNode = code`${protoTypeSymbol(type.proto, opts)}[${literalOf((0, import_codegen_core5.tsFieldName)(f.proto, opts).toString())}]`;
|
|
346
672
|
}
|
|
347
|
-
return f.isNullable() ?
|
|
673
|
+
return f.isNullable() ? code`${f.name}?: ${typeNode} | null,` : code`${f.name}: ${f.type instanceof import_codegen_core5.ScalarType && f.type.isCustomScalar() ? code`NonNullable<${typeNode}>` : typeNode},`;
|
|
348
674
|
}))}
|
|
349
675
|
};
|
|
350
676
|
`;
|
|
351
|
-
const
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
677
|
+
const needsTypeAssertion = opts.protobuf === "protobuf-es";
|
|
678
|
+
const inputObjectRefType = code`${(0, import_protoplugin.createImportSymbol)("InputObjectRef", "@pothos/core")}<${shapeTypePrintable(type)}>`;
|
|
679
|
+
const refCode = code`
|
|
680
|
+
export const ${pothosRefPrintable(type)}: ${inputObjectRefType} =
|
|
681
|
+
${pothosBuilderPrintable(opts)}.inputRef<${shapeTypePrintable(type)}>(${literalOf(type.typeName)}).implement(
|
|
682
|
+
${literalOf(compactForCodegen({
|
|
683
|
+
fields: code`t => ({${fields.length > 0 ? joinCode(fields.map((f) => code`${f.name}: ${createFieldRefCode(f, registry, opts)}`), ", ") : code`_: ${createNoopFieldRefCode({
|
|
357
684
|
input: true
|
|
358
685
|
})}`}})`,
|
|
359
|
-
extensions: (0,
|
|
686
|
+
extensions: (0, import_codegen_core5.protobufGraphQLExtensions)(type, registry),
|
|
687
|
+
description: type.description
|
|
360
688
|
}))}
|
|
361
|
-
);
|
|
689
|
+
)${needsTypeAssertion ? code` as ${inputObjectRefType}` : ""};
|
|
362
690
|
`;
|
|
363
691
|
const codes = [
|
|
364
692
|
shapeTypeCode,
|
|
365
693
|
refCode
|
|
366
694
|
];
|
|
367
|
-
if (opts.protobuf === "protobuf-es") {
|
|
368
|
-
codes.push(createToProtoFuncCode(type, opts));
|
|
695
|
+
if (opts.protobuf === "protobuf-es-v1" || opts.protobuf === "protobuf-es") {
|
|
696
|
+
codes.push(createToProtoFuncCode(type, fields, opts));
|
|
369
697
|
}
|
|
370
|
-
return
|
|
698
|
+
return joinCode(codes, "\n\n");
|
|
371
699
|
}
|
|
372
700
|
__name(createInputObjectTypeCode, "createInputObjectTypeCode");
|
|
373
|
-
function createToProtoFuncCode(type, opts) {
|
|
701
|
+
function createToProtoFuncCode(type, fields, opts) {
|
|
374
702
|
const oneofFields = {};
|
|
375
|
-
for (const f of
|
|
703
|
+
for (const f of fields) {
|
|
376
704
|
if (f.proto.oneof == null) continue;
|
|
377
|
-
if (!(f.type instanceof
|
|
378
|
-
|
|
705
|
+
if (!(f.type instanceof import_codegen_core5.InputObjectType)) {
|
|
706
|
+
continue;
|
|
379
707
|
}
|
|
380
708
|
oneofFields[f.proto.oneof.name] = [
|
|
381
709
|
...oneofFields[f.proto.oneof.name] || [],
|
|
382
710
|
f
|
|
383
711
|
];
|
|
384
712
|
}
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
const toProtoFunc = fieldToProtoFunc(f, opts);
|
|
393
|
-
if (f.isList()) {
|
|
394
|
-
return import_ts_poet7.code`${localName}: input?.${f.name}?.map(v => ${toProtoFunc}(v)),`;
|
|
395
|
-
}
|
|
396
|
-
return import_ts_poet7.code`${localName}: input?.${f.name} ? ${toProtoFunc}(input.${f.name}) : undefined,`;
|
|
397
|
-
}
|
|
398
|
-
case f.type instanceof import_codegen_core6.ScalarType:
|
|
399
|
-
case f.type instanceof import_codegen_core6.EnumType: {
|
|
400
|
-
const localName = (0, import_codegen_core6.tsFieldName)(f.proto, opts);
|
|
401
|
-
return import_ts_poet7.code`${localName}: input?.${f.name} ?? undefined,`;
|
|
402
|
-
}
|
|
403
|
-
default: {
|
|
404
|
-
f.type;
|
|
405
|
-
throw "unreachable";
|
|
713
|
+
const protoTypeSym = protoTypeSymbol(type.proto, opts);
|
|
714
|
+
const fieldAssignments = joinCode(fields.filter((f) => f.proto.oneof == null).map((f) => {
|
|
715
|
+
const localName = (0, import_codegen_core5.tsFieldName)(f.proto, opts).toString();
|
|
716
|
+
if (f.type instanceof import_codegen_core5.InputObjectType) {
|
|
717
|
+
const toProtoFunc = toProtoFuncPrintable(f, opts);
|
|
718
|
+
if (f.isList()) {
|
|
719
|
+
return code`${localName}: input?.${f.name}?.map(v => ${toProtoFunc}(v)),`;
|
|
406
720
|
}
|
|
721
|
+
return code`${localName}: input?.${f.name} ? ${toProtoFunc}(input.${f.name}) : undefined,`;
|
|
722
|
+
}
|
|
723
|
+
if (f.type instanceof import_codegen_core5.ScalarType || f.type instanceof import_codegen_core5.EnumType) {
|
|
724
|
+
return code`${localName}: input?.${f.name} ?? undefined,`;
|
|
407
725
|
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
726
|
+
f.type;
|
|
727
|
+
throw new Error("unreachable");
|
|
728
|
+
}), "\n");
|
|
729
|
+
const oneofAssignments = joinCode(Object.values(oneofFields).map((fields2) => {
|
|
730
|
+
const oneofName = (0, import_codegen_core5.tsFieldName)(
|
|
411
731
|
// biome-ignore lint/style/noNonNullAssertion: we know it's not null
|
|
412
|
-
|
|
732
|
+
fields2[0].proto.oneof,
|
|
413
733
|
opts
|
|
414
|
-
)
|
|
415
|
-
|
|
734
|
+
).toString();
|
|
735
|
+
const cases = fields2.map((f) => {
|
|
736
|
+
const caseName = (0, import_codegen_core5.tsFieldName)(f.proto, opts).toString();
|
|
737
|
+
return code`input?.${f.name} ? { case: "${caseName}", value: ${toProtoFuncPrintable(f, opts)}(input.${f.name}) } :`;
|
|
738
|
+
});
|
|
739
|
+
return code`${oneofName}: ${joinCode(cases, " ")} undefined,`;
|
|
740
|
+
}), "\n");
|
|
741
|
+
if (opts.protobuf === "protobuf-es") {
|
|
742
|
+
const protoSchemaSym = protoSchemaSymbol(type.proto, opts);
|
|
743
|
+
return code`
|
|
744
|
+
export function ${toProtoFuncName(type)} (input: ${shapeTypePrintable(type)} | null | undefined): ${protoTypeSym} {
|
|
745
|
+
return ${protobufCreateSymbol()}(${protoSchemaSym}, {
|
|
746
|
+
${fieldAssignments}
|
|
747
|
+
${oneofAssignments}
|
|
748
|
+
});
|
|
749
|
+
}
|
|
750
|
+
`;
|
|
751
|
+
}
|
|
752
|
+
return code`
|
|
753
|
+
export function ${toProtoFuncName(type)} (input: ${shapeTypePrintable(type)} | null | undefined): ${protoTypeSym} {
|
|
754
|
+
return new ${protoTypeSym}({
|
|
755
|
+
${fieldAssignments}
|
|
756
|
+
${oneofAssignments}
|
|
416
757
|
});
|
|
417
758
|
}
|
|
418
759
|
`;
|
|
419
760
|
}
|
|
420
761
|
__name(createToProtoFuncCode, "createToProtoFuncCode");
|
|
421
|
-
function toProtoFuncName(type) {
|
|
422
|
-
return `${type.typeName}$toProto`;
|
|
423
|
-
}
|
|
424
|
-
__name(toProtoFuncName, "toProtoFuncName");
|
|
425
|
-
function fieldToProtoFunc(field, opts) {
|
|
426
|
-
const importPath = (0, import_codegen_core6.generatedGraphQLTypeImportPath)(field, opts);
|
|
427
|
-
if (importPath == null) return import_ts_poet7.code`${toProtoFuncName(field.type)}`;
|
|
428
|
-
const imported = (0, import_ts_poet7.imp)(`IMPORTED_PLACEHOLDER@${importPath}`);
|
|
429
|
-
imported.symbol = toProtoFuncName(field.type);
|
|
430
|
-
return import_ts_poet7.code`${imported}`;
|
|
431
|
-
}
|
|
432
|
-
__name(fieldToProtoFunc, "fieldToProtoFunc");
|
|
433
762
|
|
|
434
763
|
// src/dslgen/printers/objectType.ts
|
|
435
|
-
var
|
|
436
|
-
var import_ts_poet8 = require("ts-poet");
|
|
764
|
+
var import_codegen_core6 = require("@proto-graphql/codegen-core");
|
|
437
765
|
function createObjectTypeCode(type, registry, opts) {
|
|
438
|
-
const isInterface = type instanceof
|
|
766
|
+
const isInterface = type instanceof import_codegen_core6.InterfaceType;
|
|
439
767
|
const typeOpts = {
|
|
440
768
|
name: type.typeName,
|
|
441
|
-
fields:
|
|
769
|
+
fields: code`t => ({${type.fields.length > 0 ? joinCode(type.fields.map((f) => code`${f.name}: ${createFieldRefCode(f, registry, opts)},`)) : code`_: ${createNoopFieldRefCode({
|
|
442
770
|
input: false
|
|
443
771
|
})}`}})`,
|
|
444
772
|
description: type.description,
|
|
445
773
|
isTypeOf: isInterface ? void 0 : createIsTypeOfFuncCode(type, registry, opts),
|
|
446
|
-
extensions: (0,
|
|
774
|
+
extensions: (0, import_codegen_core6.protobufGraphQLExtensions)(type, registry)
|
|
447
775
|
};
|
|
448
|
-
const buildRefFunc =
|
|
449
|
-
const buildTypeFunc =
|
|
450
|
-
const refFuncTypeArg = isInterface ?
|
|
776
|
+
const buildRefFunc = code`${pothosBuilderPrintable(opts)}.${isInterface ? "interface" : "object"}Ref`;
|
|
777
|
+
const buildTypeFunc = code`${pothosBuilderPrintable(opts)}.${isInterface ? "interface" : "object"}Type`;
|
|
778
|
+
const refFuncTypeArg = isInterface ? code`
|
|
451
779
|
Pick<
|
|
452
|
-
${(
|
|
453
|
-
${
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
export const ${pothosRef(type)} = ${buildRefFunc}<${refFuncTypeArg}>(${(0, import_ts_poet8.literalOf)(type.typeName)});
|
|
459
|
-
${buildTypeFunc}(${pothosRef(type)}, ${(0, import_ts_poet8.literalOf)((0, import_codegen_core7.compact)(typeOpts))});
|
|
780
|
+
${protoRefTypePrintable(type.proto, opts)},
|
|
781
|
+
${joinCode(type.fields.map((f) => literalOf((0, import_codegen_core6.tsFieldName)(f.proto, opts))), "|")}
|
|
782
|
+
>` : protoRefTypePrintable(type.proto, opts);
|
|
783
|
+
return code`
|
|
784
|
+
export const ${pothosRefPrintable(type)} = ${buildRefFunc}<${refFuncTypeArg}>(${literalOf(type.typeName)});
|
|
785
|
+
${buildTypeFunc}(${pothosRefPrintable(type)}, ${literalOf(compactForCodegen(typeOpts))});
|
|
460
786
|
`;
|
|
461
787
|
}
|
|
462
788
|
__name(createObjectTypeCode, "createObjectTypeCode");
|
|
463
|
-
function createIsTypeOfFuncCode(type,
|
|
789
|
+
function createIsTypeOfFuncCode(type, _registry, opts) {
|
|
464
790
|
switch (opts.protobuf) {
|
|
465
791
|
case "ts-proto": {
|
|
466
|
-
return
|
|
792
|
+
return code`
|
|
467
793
|
(source) => {
|
|
468
|
-
return (source as ${(
|
|
469
|
-
=== ${
|
|
794
|
+
return (source as ${protoTypeSymbol(type.proto, opts)} | { $type: string & {} }).$type
|
|
795
|
+
=== ${literalOf(type.proto.typeName)};
|
|
470
796
|
}
|
|
471
797
|
`;
|
|
472
798
|
}
|
|
473
|
-
case "protobuf-es": {
|
|
474
|
-
return
|
|
799
|
+
case "protobuf-es-v1": {
|
|
800
|
+
return code`
|
|
475
801
|
(source) => {
|
|
476
|
-
return source instanceof ${(
|
|
802
|
+
return source instanceof ${protoTypeSymbol(type.proto, opts)}
|
|
477
803
|
}
|
|
478
804
|
`;
|
|
479
805
|
}
|
|
480
|
-
case "
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
throw "unreachable";
|
|
806
|
+
case "protobuf-es": {
|
|
807
|
+
return code`
|
|
808
|
+
(source) => {
|
|
809
|
+
return ${protobufIsMessageSymbol()}(source, ${protoSchemaSymbol(type.proto, opts)})
|
|
810
|
+
}
|
|
811
|
+
`;
|
|
487
812
|
}
|
|
488
813
|
}
|
|
489
814
|
}
|
|
490
815
|
__name(createIsTypeOfFuncCode, "createIsTypeOfFuncCode");
|
|
491
816
|
|
|
492
817
|
// src/dslgen/printers/oneofUnionType.ts
|
|
493
|
-
var
|
|
494
|
-
var import_ts_poet9 = require("ts-poet");
|
|
818
|
+
var import_codegen_core7 = require("@proto-graphql/codegen-core");
|
|
495
819
|
function createOneofUnionTypeCode(type, registry, opts) {
|
|
496
820
|
const typeOpts = {
|
|
497
|
-
types: type.fields.map((f) =>
|
|
821
|
+
types: type.fields.map((f) => fieldTypeRefPrintable(f, opts)),
|
|
498
822
|
description: type.description,
|
|
499
|
-
extensions: (0,
|
|
823
|
+
extensions: (0, import_codegen_core7.protobufGraphQLExtensions)(type, registry)
|
|
500
824
|
};
|
|
501
|
-
return
|
|
502
|
-
export const ${
|
|
503
|
-
${
|
|
825
|
+
return code`
|
|
826
|
+
export const ${pothosRefPrintable(type)} =
|
|
827
|
+
${pothosBuilderPrintable(opts)}.unionType(${literalOf(type.typeName)}, ${literalOf(compactForCodegen(typeOpts))});
|
|
504
828
|
`;
|
|
505
829
|
}
|
|
506
830
|
__name(createOneofUnionTypeCode, "createOneofUnionTypeCode");
|
|
507
831
|
|
|
508
832
|
// src/dslgen/printers/index.ts
|
|
509
|
-
function
|
|
833
|
+
function createTypeDslPrintables(types, registry, opts) {
|
|
510
834
|
return types.flatMap((type) => {
|
|
511
|
-
if (type instanceof
|
|
512
|
-
return
|
|
835
|
+
if (type instanceof import_codegen_core8.ObjectType) {
|
|
836
|
+
return [
|
|
837
|
+
createObjectTypeCode(type, registry, opts)
|
|
838
|
+
];
|
|
513
839
|
}
|
|
514
|
-
if (type instanceof
|
|
515
|
-
return
|
|
840
|
+
if (type instanceof import_codegen_core8.InputObjectType) {
|
|
841
|
+
return [
|
|
842
|
+
createInputObjectTypeCode(type, registry, opts)
|
|
843
|
+
];
|
|
516
844
|
}
|
|
517
|
-
if (type instanceof
|
|
845
|
+
if (type instanceof import_codegen_core8.EnumType) {
|
|
518
846
|
return [
|
|
519
847
|
createEnumTypeCode(type, registry, opts)
|
|
520
848
|
];
|
|
521
849
|
}
|
|
522
|
-
if (type instanceof
|
|
850
|
+
if (type instanceof import_codegen_core8.OneofUnionType || type instanceof import_codegen_core8.SquashedOneofUnionType) {
|
|
523
851
|
return [
|
|
524
852
|
createOneofUnionTypeCode(type, registry, opts)
|
|
525
853
|
];
|
|
@@ -528,11 +856,12 @@ function createTypeDslCodes(types, registry, opts) {
|
|
|
528
856
|
throw "unreachable";
|
|
529
857
|
});
|
|
530
858
|
}
|
|
531
|
-
__name(
|
|
859
|
+
__name(createTypeDslPrintables, "createTypeDslPrintables");
|
|
532
860
|
|
|
533
861
|
// src/printer.ts
|
|
534
862
|
var allowedProtobufs = [
|
|
535
863
|
"ts-proto",
|
|
864
|
+
"protobuf-es-v1",
|
|
536
865
|
"protobuf-es"
|
|
537
866
|
];
|
|
538
867
|
function generateFiles(schema, file) {
|
|
@@ -540,41 +869,26 @@ function generateFiles(schema, file) {
|
|
|
540
869
|
if (!allowedProtobufs.includes(opts.printer.protobuf)) {
|
|
541
870
|
opts.printer.protobuf = "ts-proto";
|
|
542
871
|
}
|
|
543
|
-
const registry = (0,
|
|
544
|
-
const types = (0,
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
], registry, opts.printer), "protoc-gen-pothos", file);
|
|
558
|
-
f.print(code10.trimEnd());
|
|
559
|
-
}
|
|
560
|
-
break;
|
|
561
|
-
}
|
|
562
|
-
default: {
|
|
563
|
-
const _exhaustiveCheck = opts.printer.fileLayout;
|
|
564
|
-
throw "unreachable";
|
|
565
|
-
}
|
|
872
|
+
const registry = (0, import_codegen_core9.createRegistryFromSchema)(schema);
|
|
873
|
+
const types = (0, import_codegen_core9.collectTypesFromFile)(file, opts.type, schema.allFiles);
|
|
874
|
+
const f = schema.generateFile((0, import_codegen_core9.filenameFromProtoFile)(file, opts.printer));
|
|
875
|
+
const printables = [
|
|
876
|
+
...createTypeDslPrintables(types, registry, opts.printer)
|
|
877
|
+
];
|
|
878
|
+
f.preamble(file);
|
|
879
|
+
if (printables.length === 0) {
|
|
880
|
+
f.print("export {};");
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
for (const p of printables) {
|
|
884
|
+
f.print(...p);
|
|
885
|
+
f.print();
|
|
566
886
|
}
|
|
567
887
|
}
|
|
568
888
|
__name(generateFiles, "generateFiles");
|
|
569
|
-
function createCodes(types, registry, opts) {
|
|
570
|
-
return [
|
|
571
|
-
...createTypeDslCodes(types, registry, opts)
|
|
572
|
-
];
|
|
573
|
-
}
|
|
574
|
-
__name(createCodes, "createCodes");
|
|
575
889
|
|
|
576
890
|
// src/plugin.ts
|
|
577
|
-
var protocGenPothos = (0,
|
|
891
|
+
var protocGenPothos = (0, import_protoplugin3.createEcmaScriptPlugin)({
|
|
578
892
|
name: "protoc-gen-pothos",
|
|
579
893
|
version: `v${version}`,
|
|
580
894
|
generateTs: (0, import_protoc_plugin_helpers.createTsGenerator)({
|
|
@@ -582,12 +896,15 @@ var protocGenPothos = (0, import_protoplugin.createEcmaScriptPlugin)({
|
|
|
582
896
|
dsl: "pothos"
|
|
583
897
|
}),
|
|
584
898
|
parseOptions: import_protoc_plugin_helpers.parsePothosOptions,
|
|
585
|
-
// NOTE: force `target=ts`
|
|
899
|
+
// NOTE: force `target=ts` and apply formatting
|
|
586
900
|
transpile: /* @__PURE__ */ __name((files) => {
|
|
587
|
-
return files
|
|
901
|
+
return files.map((f) => ({
|
|
902
|
+
...f,
|
|
903
|
+
content: formatCode(f.content)
|
|
904
|
+
}));
|
|
588
905
|
}, "transpile")
|
|
589
906
|
});
|
|
590
907
|
|
|
591
908
|
// src/protoc-gen-pothos.ts
|
|
592
|
-
(0,
|
|
909
|
+
(0, import_protoplugin4.runNodeJs)(protocGenPothos);
|
|
593
910
|
//# sourceMappingURL=protoc-gen-pothos.cjs.map
|