protoc-gen-pothos 0.6.2 → 0.7.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/protoc-gen-pothos.cjs +514 -265
- package/dist/protoc-gen-pothos.cjs.map +1 -1
- package/dist/protoc-gen-pothos.js +493 -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 +35 -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 +177 -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,350 @@ 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_protoplugin3 = require("@bufbuild/protoplugin");
|
|
28
28
|
|
|
29
29
|
// src/plugin.ts
|
|
30
|
-
var
|
|
30
|
+
var import_protoplugin2 = 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.0";
|
|
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);
|
|
48
246
|
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
247
|
function pothosRefName(type) {
|
|
55
248
|
return `${type.typeName}$Ref`;
|
|
56
249
|
}
|
|
57
250
|
__name(pothosRefName, "pothosRefName");
|
|
58
|
-
function shapeType(type) {
|
|
59
|
-
return import_ts_poet.code`${shapeTypeName(type)}`;
|
|
60
|
-
}
|
|
61
|
-
__name(shapeType, "shapeType");
|
|
62
251
|
function shapeTypeName(type) {
|
|
63
252
|
return `${type.typeName}$Shape`;
|
|
64
253
|
}
|
|
65
254
|
__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(
|
|
255
|
+
function resolveImportOrLocal(field, opts, symbolName, localPrintable) {
|
|
256
|
+
const fromFile = (0, import_codegen_core.filename)(field.parent, opts);
|
|
257
|
+
const toFile = (0, import_codegen_core.filename)(field.type, opts);
|
|
258
|
+
if (fromFile === toFile) return localPrintable();
|
|
259
|
+
const importPath = `./${toFile.replace(/\.ts$/, "")}`;
|
|
260
|
+
return code`${(0, import_protoplugin.createImportSymbol)(symbolName, importPath)}`;
|
|
261
|
+
}
|
|
262
|
+
__name(resolveImportOrLocal, "resolveImportOrLocal");
|
|
263
|
+
function fieldTypeRefPrintable(field, opts) {
|
|
264
|
+
if (field instanceof import_codegen_core.ObjectOneofField) return pothosRefPrintable(field.type);
|
|
265
|
+
return resolveImportOrLocal(field, opts, pothosRefName(field.type), () => pothosRefPrintable(field.type));
|
|
266
|
+
}
|
|
267
|
+
__name(fieldTypeRefPrintable, "fieldTypeRefPrintable");
|
|
268
|
+
function shapeTypePrintable(type) {
|
|
269
|
+
return code`${shapeTypeName(type)}`;
|
|
270
|
+
}
|
|
271
|
+
__name(shapeTypePrintable, "shapeTypePrintable");
|
|
272
|
+
function fieldTypeShapePrintable(field, opts) {
|
|
273
|
+
return resolveImportOrLocal(field, opts, shapeTypeName(field.type), () => shapeTypePrintable(field.type));
|
|
274
|
+
}
|
|
275
|
+
__name(fieldTypeShapePrintable, "fieldTypeShapePrintable");
|
|
276
|
+
function toProtoFuncName(type) {
|
|
277
|
+
return `${type.typeName}$toProto`;
|
|
278
|
+
}
|
|
279
|
+
__name(toProtoFuncName, "toProtoFuncName");
|
|
280
|
+
function toProtoFuncPrintable(field, opts) {
|
|
281
|
+
return resolveImportOrLocal(field, opts, toProtoFuncName(field.type), () => code`${toProtoFuncName(field.type)}`);
|
|
282
|
+
}
|
|
283
|
+
__name(toProtoFuncPrintable, "toProtoFuncPrintable");
|
|
284
|
+
function pothosRefPrintable(type) {
|
|
285
|
+
return code`${pothosRefName(type)}`;
|
|
286
|
+
}
|
|
287
|
+
__name(pothosRefPrintable, "pothosRefPrintable");
|
|
288
|
+
function pothosBuilderPrintable(opts) {
|
|
289
|
+
return code`${(0, import_protoplugin.createImportSymbol)("builder", opts.pothos.builderPath)}`;
|
|
290
|
+
}
|
|
291
|
+
__name(pothosBuilderPrintable, "pothosBuilderPrintable");
|
|
292
|
+
function protoTypeSymbol(proto, opts) {
|
|
293
|
+
const chunks = [
|
|
294
|
+
proto.name
|
|
295
|
+
];
|
|
296
|
+
let current = proto;
|
|
297
|
+
while (current.parent != null) {
|
|
298
|
+
current = current.parent;
|
|
299
|
+
chunks.unshift(current.name);
|
|
300
|
+
}
|
|
301
|
+
const symbolName = chunks.join("_");
|
|
302
|
+
const importPath = protoImportPath(proto, opts);
|
|
303
|
+
return (0, import_protoplugin.createImportSymbol)(symbolName, importPath);
|
|
304
|
+
}
|
|
305
|
+
__name(protoTypeSymbol, "protoTypeSymbol");
|
|
306
|
+
function protoSchemaSymbol(proto, opts) {
|
|
307
|
+
const chunks = [
|
|
308
|
+
proto.name
|
|
309
|
+
];
|
|
310
|
+
let current = proto;
|
|
311
|
+
while (current.parent != null) {
|
|
312
|
+
current = current.parent;
|
|
313
|
+
chunks.unshift(current.name);
|
|
314
|
+
}
|
|
315
|
+
const symbolName = `${chunks.join("_")}Schema`;
|
|
316
|
+
const importPath = protoImportPath(proto, opts);
|
|
317
|
+
return (0, import_protoplugin.createImportSymbol)(symbolName, importPath);
|
|
318
|
+
}
|
|
319
|
+
__name(protoSchemaSymbol, "protoSchemaSymbol");
|
|
320
|
+
function protobufCreateSymbol() {
|
|
321
|
+
return (0, import_protoplugin.createImportSymbol)("create", "@bufbuild/protobuf");
|
|
322
|
+
}
|
|
323
|
+
__name(protobufCreateSymbol, "protobufCreateSymbol");
|
|
324
|
+
function protobufIsMessageSymbol() {
|
|
325
|
+
return (0, import_protoplugin.createImportSymbol)("isMessage", "@bufbuild/protobuf");
|
|
326
|
+
}
|
|
327
|
+
__name(protobufIsMessageSymbol, "protobufIsMessageSymbol");
|
|
328
|
+
function protobufMessageShapeSymbol() {
|
|
329
|
+
return (0, import_protoplugin.createImportSymbol)("MessageShape", "@bufbuild/protobuf");
|
|
330
|
+
}
|
|
331
|
+
__name(protobufMessageShapeSymbol, "protobufMessageShapeSymbol");
|
|
332
|
+
function protoRefTypePrintable(proto, opts) {
|
|
333
|
+
switch (opts.protobuf) {
|
|
334
|
+
case "ts-proto":
|
|
335
|
+
case "protobuf-es-v1": {
|
|
336
|
+
return code`${protoTypeSymbol(proto, opts)}`;
|
|
337
|
+
}
|
|
338
|
+
case "protobuf-es": {
|
|
339
|
+
return code`${protobufMessageShapeSymbol()}<typeof ${protoSchemaSymbol(proto, opts)}>`;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
__name(protoRefTypePrintable, "protoRefTypePrintable");
|
|
344
|
+
function protoImportPath(t, o) {
|
|
345
|
+
let importPath;
|
|
346
|
+
switch (o.protobuf) {
|
|
347
|
+
case "ts-proto": {
|
|
348
|
+
importPath = t.file.name;
|
|
349
|
+
break;
|
|
350
|
+
}
|
|
351
|
+
case "protobuf-es-v1":
|
|
352
|
+
case "protobuf-es": {
|
|
353
|
+
const { dir, name } = path.parse(t.file.name);
|
|
354
|
+
importPath = `${dir}/${name}_pb`;
|
|
355
|
+
break;
|
|
356
|
+
}
|
|
357
|
+
default: {
|
|
358
|
+
o.protobuf;
|
|
359
|
+
throw new Error(`unexpected protobuf option: ${o.protobuf}`);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return `${o.importPrefix ? `${o.importPrefix}/` : "./"}${importPath}`.replace(/(?<!:)\/\//, "/");
|
|
363
|
+
}
|
|
364
|
+
__name(protoImportPath, "protoImportPath");
|
|
87
365
|
|
|
88
366
|
// src/dslgen/printers/enumType.ts
|
|
89
367
|
function createEnumTypeCode(type, registry, opts) {
|
|
90
368
|
const typeOpts = {
|
|
91
369
|
description: type.description,
|
|
92
|
-
values:
|
|
370
|
+
values: code`{${joinCode(type.values.filter((v) => !v.isIgnored() && !v.isUnespecified()).map((ev) => code`${ev.name}: ${literalOf(compactForCodegen({
|
|
93
371
|
description: ev.description,
|
|
94
372
|
deprecationReason: ev.deprecationReason,
|
|
95
373
|
value: ev.number,
|
|
@@ -97,41 +375,38 @@ function createEnumTypeCode(type, registry, opts) {
|
|
|
97
375
|
}))},`))}} as const`,
|
|
98
376
|
extensions: (0, import_codegen_core2.protobufGraphQLExtensions)(type, registry)
|
|
99
377
|
};
|
|
100
|
-
const protoTypeExpr = (
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
378
|
+
const protoTypeExpr = protoTypeSymbol(type.proto, opts);
|
|
379
|
+
const enumRefSymbol = (0, import_protoplugin.createImportSymbol)("EnumRef", "@pothos/core");
|
|
380
|
+
const refTypeExpr = code`${enumRefSymbol}<${protoTypeExpr}, ${protoTypeExpr}>`;
|
|
381
|
+
return code`
|
|
382
|
+
export const ${pothosRefPrintable(type)}: ${refTypeExpr} =
|
|
383
|
+
${pothosBuilderPrintable(opts)}.enumType(${literalOf(type.typeName)}, ${literalOf(compactForCodegen(typeOpts))});
|
|
105
384
|
`;
|
|
106
385
|
}
|
|
107
386
|
__name(createEnumTypeCode, "createEnumTypeCode");
|
|
108
387
|
|
|
109
388
|
// src/dslgen/printers/inputObjectType.ts
|
|
110
|
-
var
|
|
111
|
-
var import_ts_poet7 = require("ts-poet");
|
|
389
|
+
var import_codegen_core5 = require("@proto-graphql/codegen-core");
|
|
112
390
|
|
|
113
391
|
// src/dslgen/printers/field.ts
|
|
114
|
-
var
|
|
115
|
-
var import_ts_poet6 = require("ts-poet");
|
|
392
|
+
var import_codegen_core4 = require("@proto-graphql/codegen-core");
|
|
116
393
|
|
|
117
394
|
// 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
395
|
function createEnumResolverCode(valueExpr, field, opts) {
|
|
121
396
|
const createBlockStmtCodes = /* @__PURE__ */ __name((valueExpr2) => {
|
|
122
397
|
const chunks = [];
|
|
123
398
|
if (field.type.unspecifiedValue != null) {
|
|
124
|
-
const escapeCode = field.isNullable() && !field.isList() ?
|
|
125
|
-
chunks.push(
|
|
126
|
-
if (${valueExpr2} === ${(
|
|
399
|
+
const escapeCode = field.isNullable() && !field.isList() ? code`return null;` : code`throw new Error("${field.name} is required field. But got unspecified.");`;
|
|
400
|
+
chunks.push(code`
|
|
401
|
+
if (${valueExpr2} === ${protoTypeSymbol(field.type.proto, opts)}.${enumValueJsName(field.type, field.type.unspecifiedValue, opts)}) {
|
|
127
402
|
${escapeCode}
|
|
128
403
|
}
|
|
129
404
|
`);
|
|
130
405
|
}
|
|
131
406
|
for (const ev of field.type.valuesWithIgnored) {
|
|
132
407
|
if (!ev.isIgnored()) continue;
|
|
133
|
-
chunks.push(
|
|
134
|
-
if (${valueExpr2} === ${(
|
|
408
|
+
chunks.push(code`
|
|
409
|
+
if (${valueExpr2} === ${protoTypeSymbol(field.type.proto, opts)}.${enumValueJsName(field.type, ev, opts)}) {
|
|
135
410
|
throw new Error("${ev.name} is ignored in GraphQL schema");
|
|
136
411
|
}
|
|
137
412
|
`);
|
|
@@ -139,51 +414,42 @@ function createEnumResolverCode(valueExpr, field, opts) {
|
|
|
139
414
|
return chunks;
|
|
140
415
|
}, "createBlockStmtCodes");
|
|
141
416
|
if (field.isList()) {
|
|
142
|
-
const stmts = createBlockStmtCodes(
|
|
417
|
+
const stmts = createBlockStmtCodes(code`item`);
|
|
143
418
|
if (stmts.length === 0) {
|
|
144
|
-
return
|
|
419
|
+
return code`return ${valueExpr}`;
|
|
145
420
|
}
|
|
146
|
-
return
|
|
147
|
-
${stmts}
|
|
421
|
+
return code`return ${valueExpr}.map(item => {
|
|
422
|
+
${joinCode(stmts)}
|
|
148
423
|
return item;
|
|
149
424
|
})`;
|
|
150
425
|
}
|
|
151
|
-
return
|
|
152
|
-
${createBlockStmtCodes(valueExpr)}
|
|
426
|
+
return code`
|
|
427
|
+
${joinCode(createBlockStmtCodes(valueExpr))}
|
|
153
428
|
return ${valueExpr};
|
|
154
429
|
`;
|
|
155
430
|
}
|
|
156
431
|
__name(createEnumResolverCode, "createEnumResolverCode");
|
|
157
|
-
function enumValueJsName(
|
|
432
|
+
function enumValueJsName(_et, ev, opts) {
|
|
158
433
|
switch (opts.protobuf) {
|
|
159
434
|
case "ts-proto":
|
|
160
435
|
return ev.proto.name;
|
|
161
|
-
case "protobuf-es":
|
|
436
|
+
case "protobuf-es-v1":
|
|
437
|
+
case "protobuf-es":
|
|
162
438
|
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
439
|
}
|
|
172
440
|
}
|
|
173
441
|
__name(enumValueJsName, "enumValueJsName");
|
|
174
442
|
|
|
175
443
|
// src/dslgen/printers/fieldResolver/nonNullResolver.ts
|
|
176
|
-
var import_ts_poet4 = require("ts-poet");
|
|
177
444
|
function createNonNullResolverCode(valueExpr) {
|
|
178
|
-
return
|
|
445
|
+
return code`
|
|
179
446
|
return ${valueExpr}!;
|
|
180
447
|
`;
|
|
181
448
|
}
|
|
182
449
|
__name(createNonNullResolverCode, "createNonNullResolverCode");
|
|
183
450
|
|
|
184
451
|
// src/dslgen/printers/fieldResolver/oneofUnionResolver.ts
|
|
185
|
-
var
|
|
186
|
-
var import_ts_poet5 = require("ts-poet");
|
|
452
|
+
var import_codegen_core3 = require("@proto-graphql/codegen-core");
|
|
187
453
|
function createOneofUnionResolverCode(sourceExpr, field, opts) {
|
|
188
454
|
const createBlockStmtCode = /* @__PURE__ */ __name((sourceExpr2, { nullable, list }) => {
|
|
189
455
|
switch (opts.protobuf) {
|
|
@@ -192,26 +458,19 @@ function createOneofUnionResolverCode(sourceExpr, field, opts) {
|
|
|
192
458
|
nullable
|
|
193
459
|
});
|
|
194
460
|
}
|
|
461
|
+
case "protobuf-es-v1":
|
|
195
462
|
case "protobuf-es": {
|
|
196
463
|
return createBlockStmtCodeForProtobufEs(sourceExpr2, field, opts, {
|
|
197
464
|
nullable,
|
|
198
465
|
list
|
|
199
466
|
});
|
|
200
467
|
}
|
|
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
468
|
}
|
|
210
469
|
}, "createBlockStmtCode");
|
|
211
|
-
if (field instanceof
|
|
212
|
-
return
|
|
470
|
+
if (field instanceof import_codegen_core3.ObjectField && field.isList()) {
|
|
471
|
+
return code`
|
|
213
472
|
return ${sourceExpr}.map(item => {
|
|
214
|
-
${createBlockStmtCode(
|
|
473
|
+
${createBlockStmtCode(code`item`, {
|
|
215
474
|
nullable: false,
|
|
216
475
|
list: true
|
|
217
476
|
})}
|
|
@@ -226,15 +485,13 @@ function createOneofUnionResolverCode(sourceExpr, field, opts) {
|
|
|
226
485
|
__name(createOneofUnionResolverCode, "createOneofUnionResolverCode");
|
|
227
486
|
function createBlockStmtCodeForTsProto(sourceExpr, field, opts, { nullable }) {
|
|
228
487
|
const createFieldExpr = /* @__PURE__ */ __name((memberField) => {
|
|
229
|
-
if (field instanceof
|
|
230
|
-
return
|
|
488
|
+
if (field instanceof import_codegen_core3.ObjectOneofField) {
|
|
489
|
+
return code`${sourceExpr}.${(0, import_codegen_core3.tsFieldName)(memberField.proto, opts).toString()}`;
|
|
231
490
|
}
|
|
232
|
-
return
|
|
491
|
+
return code`${sourceExpr}?.${(0, import_codegen_core3.tsFieldName)(memberField.proto, opts).toString()}`;
|
|
233
492
|
}, "createFieldExpr");
|
|
234
|
-
return
|
|
235
|
-
const value = ${
|
|
236
|
-
on: "??"
|
|
237
|
-
})};
|
|
493
|
+
return code`
|
|
494
|
+
const value = ${joinCode(field.type.fields.map(createFieldExpr), " ?? ")};
|
|
238
495
|
if (value == null) {
|
|
239
496
|
${nullable ? "return null" : `throw new Error("${field.name} should not be null")`};
|
|
240
497
|
}
|
|
@@ -244,25 +501,21 @@ function createBlockStmtCodeForTsProto(sourceExpr, field, opts, { nullable }) {
|
|
|
244
501
|
__name(createBlockStmtCodeForTsProto, "createBlockStmtCodeForTsProto");
|
|
245
502
|
function createBlockStmtCodeForProtobufEs(sourceExpr, field, opts, { nullable, list }) {
|
|
246
503
|
let valueExpr;
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
break;
|
|
255
|
-
}
|
|
256
|
-
default: {
|
|
257
|
-
field;
|
|
258
|
-
throw "unreachable";
|
|
259
|
-
}
|
|
504
|
+
if (field instanceof import_codegen_core3.ObjectOneofField) {
|
|
505
|
+
valueExpr = code`${sourceExpr}.${(0, import_codegen_core3.tsFieldName)(field.proto, opts).toString()}.value`;
|
|
506
|
+
} else if (field instanceof import_codegen_core3.ObjectField) {
|
|
507
|
+
valueExpr = code`${sourceExpr}${list ? "" : "?"}.${(0, import_codegen_core3.tsFieldName)(field.type.oneofUnionType.proto, opts).toString()}.value`;
|
|
508
|
+
} else {
|
|
509
|
+
field;
|
|
510
|
+
throw new Error("unreachable");
|
|
260
511
|
}
|
|
512
|
+
const memberTypeSymbols = field.type.fields.map((f) => protoTypeSymbol(f.type.proto, opts));
|
|
513
|
+
const typeAssertion = memberTypeSymbols.length > 0 ? code`${joinCode(memberTypeSymbols.map((s) => code`${s}`), " | ")} | undefined` : code`undefined`;
|
|
261
514
|
if (nullable) {
|
|
262
|
-
return
|
|
515
|
+
return code`return (${valueExpr} ?? null) as ${typeAssertion} | null;`;
|
|
263
516
|
}
|
|
264
|
-
return
|
|
265
|
-
const value = ${valueExpr};
|
|
517
|
+
return code`
|
|
518
|
+
const value = ${valueExpr} as ${typeAssertion};
|
|
266
519
|
if (value == null) {
|
|
267
520
|
throw new Error("${field.name} should not be null");
|
|
268
521
|
}
|
|
@@ -272,40 +525,44 @@ function createBlockStmtCodeForProtobufEs(sourceExpr, field, opts, { nullable, l
|
|
|
272
525
|
__name(createBlockStmtCodeForProtobufEs, "createBlockStmtCodeForProtobufEs");
|
|
273
526
|
|
|
274
527
|
// src/dslgen/printers/field.ts
|
|
528
|
+
function createGetFieldValueCodePrintable(source, proto, opts) {
|
|
529
|
+
return code`${source}.${(0, import_codegen_core4.tsFieldName)(proto, opts)}`;
|
|
530
|
+
}
|
|
531
|
+
__name(createGetFieldValueCodePrintable, "createGetFieldValueCodePrintable");
|
|
275
532
|
function createFieldRefCode(field, registry, opts) {
|
|
276
|
-
const isInput = field instanceof
|
|
277
|
-
const baseType = field.type instanceof
|
|
278
|
-
const sourceExpr =
|
|
533
|
+
const isInput = field instanceof import_codegen_core4.InputObjectField;
|
|
534
|
+
const baseType = field.type instanceof import_codegen_core4.ScalarType ? literalOf(field.type.typeName) : fieldTypeRefPrintable(field, opts);
|
|
535
|
+
const sourceExpr = "source";
|
|
279
536
|
let resolverCode;
|
|
280
537
|
if (!isInput) {
|
|
281
|
-
if (field instanceof
|
|
282
|
-
resolverCode = createOneofUnionResolverCode(sourceExpr
|
|
538
|
+
if (field instanceof import_codegen_core4.ObjectOneofField) {
|
|
539
|
+
resolverCode = createOneofUnionResolverCode(code`${sourceExpr}`, field, opts);
|
|
283
540
|
} else {
|
|
284
|
-
const valueExpr = (
|
|
285
|
-
const nullableInProto = field.type instanceof
|
|
541
|
+
const valueExpr = createGetFieldValueCodePrintable(sourceExpr, field.proto, opts);
|
|
542
|
+
const nullableInProto = field.type instanceof import_codegen_core4.ObjectType || field.type instanceof import_codegen_core4.ScalarType && !field.type.isPrimitive() && !field.type.isWrapperType();
|
|
286
543
|
if (nullableInProto && !field.isNullable()) {
|
|
287
544
|
resolverCode = createNonNullResolverCode(valueExpr);
|
|
288
545
|
}
|
|
289
|
-
if (field.type instanceof
|
|
546
|
+
if (field.type instanceof import_codegen_core4.EnumType && field instanceof import_codegen_core4.ObjectField) {
|
|
290
547
|
resolverCode = createEnumResolverCode(valueExpr, field, opts);
|
|
291
548
|
}
|
|
292
|
-
if (field.type instanceof
|
|
549
|
+
if (field.type instanceof import_codegen_core4.SquashedOneofUnionType && field instanceof import_codegen_core4.ObjectField) {
|
|
293
550
|
resolverCode = createOneofUnionResolverCode(valueExpr, field, opts);
|
|
294
551
|
}
|
|
295
|
-
if (field.type instanceof
|
|
552
|
+
if (field.type instanceof import_codegen_core4.ScalarType && field.type.isBytes()) {
|
|
296
553
|
if (field.isList()) {
|
|
297
|
-
resolverCode =
|
|
554
|
+
resolverCode = code`return ${valueExpr}.map(v => Buffer.from(v));`;
|
|
298
555
|
} else if (field.isNullable()) {
|
|
299
|
-
resolverCode =
|
|
556
|
+
resolverCode = code`return ${valueExpr} == null ? null : Buffer.from(${valueExpr});`;
|
|
300
557
|
} else {
|
|
301
|
-
resolverCode =
|
|
558
|
+
resolverCode = code`return Buffer.from(${valueExpr});`;
|
|
302
559
|
}
|
|
303
560
|
}
|
|
304
561
|
}
|
|
305
562
|
}
|
|
306
563
|
const nullableValue = isInput !== field.isNullable();
|
|
307
564
|
const fieldOpts = {
|
|
308
|
-
type: field.isList() ?
|
|
565
|
+
type: field.isList() ? code`[${baseType}]` : baseType,
|
|
309
566
|
[isInput ? "required" : "nullable"]: field.isList() ? {
|
|
310
567
|
list: nullableValue,
|
|
311
568
|
items: isInput
|
|
@@ -313,15 +570,15 @@ function createFieldRefCode(field, registry, opts) {
|
|
|
313
570
|
} : nullableValue,
|
|
314
571
|
description: field.description,
|
|
315
572
|
deprecationReason: field.deprecationReason,
|
|
316
|
-
resolve: resolverCode ?
|
|
317
|
-
extensions: (0,
|
|
573
|
+
resolve: resolverCode ? code`${sourceExpr} => {${resolverCode}}` : null,
|
|
574
|
+
extensions: (0, import_codegen_core4.protobufGraphQLExtensions)(field, registry)
|
|
318
575
|
};
|
|
319
576
|
const shouldUseFieldFunc = isInput || resolverCode != null;
|
|
320
|
-
return shouldUseFieldFunc ?
|
|
577
|
+
return shouldUseFieldFunc ? code`t.field(${literalOf(compactForCodegen(fieldOpts))})` : code`t.expose(${literalOf((0, import_codegen_core4.tsFieldName)(field.proto, opts))}, ${literalOf(compactForCodegen(fieldOpts))})`;
|
|
321
578
|
}
|
|
322
579
|
__name(createFieldRefCode, "createFieldRefCode");
|
|
323
580
|
function createNoopFieldRefCode(opts) {
|
|
324
|
-
return
|
|
581
|
+
return code`
|
|
325
582
|
t.field({
|
|
326
583
|
type: "Boolean",
|
|
327
584
|
${opts.input ? "required: false" : "nullable: true"},
|
|
@@ -334,192 +591,195 @@ __name(createNoopFieldRefCode, "createNoopFieldRefCode");
|
|
|
334
591
|
|
|
335
592
|
// src/dslgen/printers/inputObjectType.ts
|
|
336
593
|
function createInputObjectTypeCode(type, registry, opts) {
|
|
337
|
-
const
|
|
338
|
-
|
|
339
|
-
|
|
594
|
+
const fields = type.fields.filter((f) => !(type.options.ignoreNonMessageOneofFields && f.proto.oneof != null && !(f.type instanceof import_codegen_core5.InputObjectType)));
|
|
595
|
+
const shapeTypeCode = code`
|
|
596
|
+
export type ${shapeTypePrintable(type)} = {
|
|
597
|
+
${joinCode(fields.map((f) => {
|
|
340
598
|
let typeNode;
|
|
341
|
-
if (f.type instanceof
|
|
342
|
-
typeNode =
|
|
343
|
-
if (f.isList()) typeNode =
|
|
599
|
+
if (f.type instanceof import_codegen_core5.InputObjectType) {
|
|
600
|
+
typeNode = fieldTypeShapePrintable(f, opts);
|
|
601
|
+
if (f.isList()) typeNode = code`Array<${typeNode}>`;
|
|
344
602
|
} else {
|
|
345
|
-
typeNode =
|
|
603
|
+
typeNode = code`${protoTypeSymbol(type.proto, opts)}[${literalOf((0, import_codegen_core5.tsFieldName)(f.proto, opts).toString())}]`;
|
|
346
604
|
}
|
|
347
|
-
return f.isNullable() ?
|
|
605
|
+
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
606
|
}))}
|
|
349
607
|
};
|
|
350
608
|
`;
|
|
351
|
-
const
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
609
|
+
const needsTypeAssertion = opts.protobuf === "protobuf-es";
|
|
610
|
+
const inputObjectRefType = code`${(0, import_protoplugin.createImportSymbol)("InputObjectRef", "@pothos/core")}<${shapeTypePrintable(type)}>`;
|
|
611
|
+
const refCode = code`
|
|
612
|
+
export const ${pothosRefPrintable(type)}: ${inputObjectRefType} =
|
|
613
|
+
${pothosBuilderPrintable(opts)}.inputRef<${shapeTypePrintable(type)}>(${literalOf(type.typeName)}).implement(
|
|
614
|
+
${literalOf(compactForCodegen({
|
|
615
|
+
fields: code`t => ({${fields.length > 0 ? joinCode(fields.map((f) => code`${f.name}: ${createFieldRefCode(f, registry, opts)}`), ", ") : code`_: ${createNoopFieldRefCode({
|
|
357
616
|
input: true
|
|
358
617
|
})}`}})`,
|
|
359
|
-
extensions: (0,
|
|
618
|
+
extensions: (0, import_codegen_core5.protobufGraphQLExtensions)(type, registry),
|
|
619
|
+
description: type.description
|
|
360
620
|
}))}
|
|
361
|
-
);
|
|
621
|
+
)${needsTypeAssertion ? code` as ${inputObjectRefType}` : ""};
|
|
362
622
|
`;
|
|
363
623
|
const codes = [
|
|
364
624
|
shapeTypeCode,
|
|
365
625
|
refCode
|
|
366
626
|
];
|
|
367
|
-
if (opts.protobuf === "protobuf-es") {
|
|
368
|
-
codes.push(createToProtoFuncCode(type, opts));
|
|
627
|
+
if (opts.protobuf === "protobuf-es-v1" || opts.protobuf === "protobuf-es") {
|
|
628
|
+
codes.push(createToProtoFuncCode(type, fields, opts));
|
|
369
629
|
}
|
|
370
|
-
return
|
|
630
|
+
return joinCode(codes, "\n\n");
|
|
371
631
|
}
|
|
372
632
|
__name(createInputObjectTypeCode, "createInputObjectTypeCode");
|
|
373
|
-
function createToProtoFuncCode(type, opts) {
|
|
633
|
+
function createToProtoFuncCode(type, fields, opts) {
|
|
374
634
|
const oneofFields = {};
|
|
375
|
-
for (const f of
|
|
635
|
+
for (const f of fields) {
|
|
376
636
|
if (f.proto.oneof == null) continue;
|
|
377
|
-
if (!(f.type instanceof
|
|
378
|
-
|
|
637
|
+
if (!(f.type instanceof import_codegen_core5.InputObjectType)) {
|
|
638
|
+
continue;
|
|
379
639
|
}
|
|
380
640
|
oneofFields[f.proto.oneof.name] = [
|
|
381
641
|
...oneofFields[f.proto.oneof.name] || [],
|
|
382
642
|
f
|
|
383
643
|
];
|
|
384
644
|
}
|
|
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";
|
|
645
|
+
const protoTypeSym = protoTypeSymbol(type.proto, opts);
|
|
646
|
+
const fieldAssignments = joinCode(fields.filter((f) => f.proto.oneof == null).map((f) => {
|
|
647
|
+
const localName = (0, import_codegen_core5.tsFieldName)(f.proto, opts).toString();
|
|
648
|
+
if (f.type instanceof import_codegen_core5.InputObjectType) {
|
|
649
|
+
const toProtoFunc = toProtoFuncPrintable(f, opts);
|
|
650
|
+
if (f.isList()) {
|
|
651
|
+
return code`${localName}: input?.${f.name}?.map(v => ${toProtoFunc}(v)),`;
|
|
406
652
|
}
|
|
653
|
+
return code`${localName}: input?.${f.name} ? ${toProtoFunc}(input.${f.name}) : undefined,`;
|
|
407
654
|
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
655
|
+
if (f.type instanceof import_codegen_core5.ScalarType || f.type instanceof import_codegen_core5.EnumType) {
|
|
656
|
+
return code`${localName}: input?.${f.name} ?? undefined,`;
|
|
657
|
+
}
|
|
658
|
+
f.type;
|
|
659
|
+
throw new Error("unreachable");
|
|
660
|
+
}), "\n");
|
|
661
|
+
const oneofAssignments = joinCode(Object.values(oneofFields).map((fields2) => {
|
|
662
|
+
const oneofName = (0, import_codegen_core5.tsFieldName)(
|
|
411
663
|
// biome-ignore lint/style/noNonNullAssertion: we know it's not null
|
|
412
|
-
|
|
664
|
+
fields2[0].proto.oneof,
|
|
413
665
|
opts
|
|
414
|
-
)
|
|
415
|
-
|
|
666
|
+
).toString();
|
|
667
|
+
const cases = fields2.map((f) => {
|
|
668
|
+
const caseName = (0, import_codegen_core5.tsFieldName)(f.proto, opts).toString();
|
|
669
|
+
return code`input?.${f.name} ? { case: "${caseName}", value: ${toProtoFuncPrintable(f, opts)}(input.${f.name}) } :`;
|
|
670
|
+
});
|
|
671
|
+
return code`${oneofName}: ${joinCode(cases, " ")} undefined,`;
|
|
672
|
+
}), "\n");
|
|
673
|
+
if (opts.protobuf === "protobuf-es") {
|
|
674
|
+
const protoSchemaSym = protoSchemaSymbol(type.proto, opts);
|
|
675
|
+
return code`
|
|
676
|
+
export function ${toProtoFuncName(type)} (input: ${shapeTypePrintable(type)} | null | undefined): ${protoTypeSym} {
|
|
677
|
+
return ${protobufCreateSymbol()}(${protoSchemaSym}, {
|
|
678
|
+
${fieldAssignments}
|
|
679
|
+
${oneofAssignments}
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
`;
|
|
683
|
+
}
|
|
684
|
+
return code`
|
|
685
|
+
export function ${toProtoFuncName(type)} (input: ${shapeTypePrintable(type)} | null | undefined): ${protoTypeSym} {
|
|
686
|
+
return new ${protoTypeSym}({
|
|
687
|
+
${fieldAssignments}
|
|
688
|
+
${oneofAssignments}
|
|
416
689
|
});
|
|
417
690
|
}
|
|
418
691
|
`;
|
|
419
692
|
}
|
|
420
693
|
__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
694
|
|
|
434
695
|
// src/dslgen/printers/objectType.ts
|
|
435
|
-
var
|
|
436
|
-
var import_ts_poet8 = require("ts-poet");
|
|
696
|
+
var import_codegen_core6 = require("@proto-graphql/codegen-core");
|
|
437
697
|
function createObjectTypeCode(type, registry, opts) {
|
|
438
|
-
const isInterface = type instanceof
|
|
698
|
+
const isInterface = type instanceof import_codegen_core6.InterfaceType;
|
|
439
699
|
const typeOpts = {
|
|
440
700
|
name: type.typeName,
|
|
441
|
-
fields:
|
|
701
|
+
fields: code`t => ({${type.fields.length > 0 ? joinCode(type.fields.map((f) => code`${f.name}: ${createFieldRefCode(f, registry, opts)},`)) : code`_: ${createNoopFieldRefCode({
|
|
442
702
|
input: false
|
|
443
703
|
})}`}})`,
|
|
444
704
|
description: type.description,
|
|
445
705
|
isTypeOf: isInterface ? void 0 : createIsTypeOfFuncCode(type, registry, opts),
|
|
446
|
-
extensions: (0,
|
|
706
|
+
extensions: (0, import_codegen_core6.protobufGraphQLExtensions)(type, registry)
|
|
447
707
|
};
|
|
448
|
-
const buildRefFunc =
|
|
449
|
-
const buildTypeFunc =
|
|
450
|
-
const refFuncTypeArg = isInterface ?
|
|
708
|
+
const buildRefFunc = code`${pothosBuilderPrintable(opts)}.${isInterface ? "interface" : "object"}Ref`;
|
|
709
|
+
const buildTypeFunc = code`${pothosBuilderPrintable(opts)}.${isInterface ? "interface" : "object"}Type`;
|
|
710
|
+
const refFuncTypeArg = isInterface ? code`
|
|
451
711
|
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))});
|
|
712
|
+
${protoRefTypePrintable(type.proto, opts)},
|
|
713
|
+
${joinCode(type.fields.map((f) => literalOf((0, import_codegen_core6.tsFieldName)(f.proto, opts))), "|")}
|
|
714
|
+
>` : protoRefTypePrintable(type.proto, opts);
|
|
715
|
+
return code`
|
|
716
|
+
export const ${pothosRefPrintable(type)} = ${buildRefFunc}<${refFuncTypeArg}>(${literalOf(type.typeName)});
|
|
717
|
+
${buildTypeFunc}(${pothosRefPrintable(type)}, ${literalOf(compactForCodegen(typeOpts))});
|
|
460
718
|
`;
|
|
461
719
|
}
|
|
462
720
|
__name(createObjectTypeCode, "createObjectTypeCode");
|
|
463
|
-
function createIsTypeOfFuncCode(type,
|
|
721
|
+
function createIsTypeOfFuncCode(type, _registry, opts) {
|
|
464
722
|
switch (opts.protobuf) {
|
|
465
723
|
case "ts-proto": {
|
|
466
|
-
return
|
|
724
|
+
return code`
|
|
467
725
|
(source) => {
|
|
468
|
-
return (source as ${(
|
|
469
|
-
=== ${
|
|
726
|
+
return (source as ${protoTypeSymbol(type.proto, opts)} | { $type: string & {} }).$type
|
|
727
|
+
=== ${literalOf(type.proto.typeName)};
|
|
470
728
|
}
|
|
471
729
|
`;
|
|
472
730
|
}
|
|
473
|
-
case "protobuf-es": {
|
|
474
|
-
return
|
|
731
|
+
case "protobuf-es-v1": {
|
|
732
|
+
return code`
|
|
475
733
|
(source) => {
|
|
476
|
-
return source instanceof ${(
|
|
734
|
+
return source instanceof ${protoTypeSymbol(type.proto, opts)}
|
|
477
735
|
}
|
|
478
736
|
`;
|
|
479
737
|
}
|
|
480
|
-
case "
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
throw "unreachable";
|
|
738
|
+
case "protobuf-es": {
|
|
739
|
+
return code`
|
|
740
|
+
(source) => {
|
|
741
|
+
return ${protobufIsMessageSymbol()}(source, ${protoSchemaSymbol(type.proto, opts)})
|
|
742
|
+
}
|
|
743
|
+
`;
|
|
487
744
|
}
|
|
488
745
|
}
|
|
489
746
|
}
|
|
490
747
|
__name(createIsTypeOfFuncCode, "createIsTypeOfFuncCode");
|
|
491
748
|
|
|
492
749
|
// src/dslgen/printers/oneofUnionType.ts
|
|
493
|
-
var
|
|
494
|
-
var import_ts_poet9 = require("ts-poet");
|
|
750
|
+
var import_codegen_core7 = require("@proto-graphql/codegen-core");
|
|
495
751
|
function createOneofUnionTypeCode(type, registry, opts) {
|
|
496
752
|
const typeOpts = {
|
|
497
|
-
types: type.fields.map((f) =>
|
|
753
|
+
types: type.fields.map((f) => fieldTypeRefPrintable(f, opts)),
|
|
498
754
|
description: type.description,
|
|
499
|
-
extensions: (0,
|
|
755
|
+
extensions: (0, import_codegen_core7.protobufGraphQLExtensions)(type, registry)
|
|
500
756
|
};
|
|
501
|
-
return
|
|
502
|
-
export const ${
|
|
503
|
-
${
|
|
757
|
+
return code`
|
|
758
|
+
export const ${pothosRefPrintable(type)} =
|
|
759
|
+
${pothosBuilderPrintable(opts)}.unionType(${literalOf(type.typeName)}, ${literalOf(compactForCodegen(typeOpts))});
|
|
504
760
|
`;
|
|
505
761
|
}
|
|
506
762
|
__name(createOneofUnionTypeCode, "createOneofUnionTypeCode");
|
|
507
763
|
|
|
508
764
|
// src/dslgen/printers/index.ts
|
|
509
|
-
function
|
|
765
|
+
function createTypeDslPrintables(types, registry, opts) {
|
|
510
766
|
return types.flatMap((type) => {
|
|
511
|
-
if (type instanceof
|
|
512
|
-
return
|
|
767
|
+
if (type instanceof import_codegen_core8.ObjectType) {
|
|
768
|
+
return [
|
|
769
|
+
createObjectTypeCode(type, registry, opts)
|
|
770
|
+
];
|
|
513
771
|
}
|
|
514
|
-
if (type instanceof
|
|
515
|
-
return
|
|
772
|
+
if (type instanceof import_codegen_core8.InputObjectType) {
|
|
773
|
+
return [
|
|
774
|
+
createInputObjectTypeCode(type, registry, opts)
|
|
775
|
+
];
|
|
516
776
|
}
|
|
517
|
-
if (type instanceof
|
|
777
|
+
if (type instanceof import_codegen_core8.EnumType) {
|
|
518
778
|
return [
|
|
519
779
|
createEnumTypeCode(type, registry, opts)
|
|
520
780
|
];
|
|
521
781
|
}
|
|
522
|
-
if (type instanceof
|
|
782
|
+
if (type instanceof import_codegen_core8.OneofUnionType || type instanceof import_codegen_core8.SquashedOneofUnionType) {
|
|
523
783
|
return [
|
|
524
784
|
createOneofUnionTypeCode(type, registry, opts)
|
|
525
785
|
];
|
|
@@ -528,11 +788,12 @@ function createTypeDslCodes(types, registry, opts) {
|
|
|
528
788
|
throw "unreachable";
|
|
529
789
|
});
|
|
530
790
|
}
|
|
531
|
-
__name(
|
|
791
|
+
__name(createTypeDslPrintables, "createTypeDslPrintables");
|
|
532
792
|
|
|
533
793
|
// src/printer.ts
|
|
534
794
|
var allowedProtobufs = [
|
|
535
795
|
"ts-proto",
|
|
796
|
+
"protobuf-es-v1",
|
|
536
797
|
"protobuf-es"
|
|
537
798
|
];
|
|
538
799
|
function generateFiles(schema, file) {
|
|
@@ -540,41 +801,26 @@ function generateFiles(schema, file) {
|
|
|
540
801
|
if (!allowedProtobufs.includes(opts.printer.protobuf)) {
|
|
541
802
|
opts.printer.protobuf = "ts-proto";
|
|
542
803
|
}
|
|
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
|
-
}
|
|
804
|
+
const registry = (0, import_codegen_core9.createRegistryFromSchema)(schema);
|
|
805
|
+
const types = (0, import_codegen_core9.collectTypesFromFile)(file, opts.type, schema.allFiles);
|
|
806
|
+
const f = schema.generateFile((0, import_codegen_core9.filenameFromProtoFile)(file, opts.printer));
|
|
807
|
+
const printables = [
|
|
808
|
+
...createTypeDslPrintables(types, registry, opts.printer)
|
|
809
|
+
];
|
|
810
|
+
f.preamble(file);
|
|
811
|
+
if (printables.length === 0) {
|
|
812
|
+
f.print("export {};");
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
for (const p of printables) {
|
|
816
|
+
f.print(...p);
|
|
817
|
+
f.print();
|
|
566
818
|
}
|
|
567
819
|
}
|
|
568
820
|
__name(generateFiles, "generateFiles");
|
|
569
|
-
function createCodes(types, registry, opts) {
|
|
570
|
-
return [
|
|
571
|
-
...createTypeDslCodes(types, registry, opts)
|
|
572
|
-
];
|
|
573
|
-
}
|
|
574
|
-
__name(createCodes, "createCodes");
|
|
575
821
|
|
|
576
822
|
// src/plugin.ts
|
|
577
|
-
var protocGenPothos = (0,
|
|
823
|
+
var protocGenPothos = (0, import_protoplugin2.createEcmaScriptPlugin)({
|
|
578
824
|
name: "protoc-gen-pothos",
|
|
579
825
|
version: `v${version}`,
|
|
580
826
|
generateTs: (0, import_protoc_plugin_helpers.createTsGenerator)({
|
|
@@ -582,12 +828,15 @@ var protocGenPothos = (0, import_protoplugin.createEcmaScriptPlugin)({
|
|
|
582
828
|
dsl: "pothos"
|
|
583
829
|
}),
|
|
584
830
|
parseOptions: import_protoc_plugin_helpers.parsePothosOptions,
|
|
585
|
-
// NOTE: force `target=ts`
|
|
831
|
+
// NOTE: force `target=ts` and apply formatting
|
|
586
832
|
transpile: /* @__PURE__ */ __name((files) => {
|
|
587
|
-
return files
|
|
833
|
+
return files.map((f) => ({
|
|
834
|
+
...f,
|
|
835
|
+
content: formatCode(f.content)
|
|
836
|
+
}));
|
|
588
837
|
}, "transpile")
|
|
589
838
|
});
|
|
590
839
|
|
|
591
840
|
// src/protoc-gen-pothos.ts
|
|
592
|
-
(0,
|
|
841
|
+
(0, import_protoplugin3.runNodeJs)(protocGenPothos);
|
|
593
842
|
//# sourceMappingURL=protoc-gen-pothos.cjs.map
|