protoc-gen-pothos 0.2.2 → 0.3.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/CHANGELOG.md +26 -0
- package/lib/dslgen/printers/enumType.js +29 -64
- package/lib/dslgen/printers/field.js +46 -95
- package/lib/dslgen/printers/fieldResolver/enumFieldResolver.js +39 -46
- package/lib/dslgen/printers/fieldResolver/nonNullResolver.js +7 -8
- package/lib/dslgen/printers/fieldResolver/oneofUnionResolver.js +21 -41
- package/lib/dslgen/printers/index.js +9 -44
- package/lib/dslgen/printers/inputObjectType.js +39 -82
- package/lib/dslgen/printers/objectType.js +36 -89
- package/lib/dslgen/printers/oneofUnionType.js +13 -62
- package/lib/dslgen/printers/util.js +59 -86
- package/lib/printer.js +10 -40
- package/lib/process.js +1 -1
- package/module/dslgen/printers/enumType.js +28 -60
- package/module/dslgen/printers/field.js +48 -94
- package/module/dslgen/printers/fieldResolver/enumFieldResolver.js +37 -41
- package/module/dslgen/printers/fieldResolver/nonNullResolver.js +5 -3
- package/module/dslgen/printers/fieldResolver/oneofUnionResolver.js +19 -36
- package/module/dslgen/printers/index.js +11 -41
- package/module/dslgen/printers/inputObjectType.js +40 -80
- package/module/dslgen/printers/objectType.js +37 -87
- package/module/dslgen/printers/oneofUnionType.js +13 -59
- package/module/dslgen/printers/util.js +36 -78
- package/module/printer.js +12 -39
- package/module/process.js +1 -1
- package/package.json +6 -5
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
3
|
+
exports.createInputObjectTypeCode = void 0;
|
|
7
4
|
const codegen_core_1 = require("@proto-graphql/codegen-core");
|
|
8
|
-
const
|
|
5
|
+
const ts_poet_1 = require("ts-poet");
|
|
9
6
|
const field_1 = require("./field");
|
|
10
7
|
const util_1 = require("./util");
|
|
11
8
|
/**
|
|
12
9
|
* @example
|
|
13
10
|
* ```ts
|
|
14
|
-
* export
|
|
15
|
-
*
|
|
11
|
+
* export type HelloInput$Shape = {
|
|
12
|
+
* // ...
|
|
13
|
+
* }
|
|
14
|
+
* export const HelloInput$Ref: pothos.InputObjectRef<Hello$SHape> = builder.inputRef("HelloInput").implement({
|
|
16
15
|
* description: "...",
|
|
17
16
|
* fields: (t) => ({
|
|
18
17
|
* // ...
|
|
@@ -20,81 +19,39 @@ const util_1 = require("./util");
|
|
|
20
19
|
* })
|
|
21
20
|
* ```
|
|
22
21
|
*/
|
|
23
|
-
function
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
])),
|
|
34
|
-
];
|
|
35
|
-
}
|
|
36
|
-
exports.createInputObjectTypeDslStmts = createInputObjectTypeDslStmts;
|
|
37
|
-
/**
|
|
38
|
-
* @example
|
|
39
|
-
* ```ts
|
|
40
|
-
* definition(t) {
|
|
41
|
-
* // ...
|
|
42
|
-
* }
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
45
|
-
function createInputObjectTypeFieldsMethodExpr(type) {
|
|
46
|
-
return typescript_1.default.factory.createArrowFunction(undefined, undefined, [typescript_1.default.factory.createParameterDeclaration(undefined, undefined, undefined, "t", undefined, undefined, undefined)], undefined, typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.EqualsGreaterThanToken), typescript_1.default.factory.createParenthesizedExpression(typescript_1.default.factory.createObjectLiteralExpression(type.fields.length > 0
|
|
47
|
-
? type.fields.map((f) => typescript_1.default.factory.createPropertyAssignment(f.name, (0, field_1.createFieldDefinitionExpr)(f)))
|
|
48
|
-
: [typescript_1.default.factory.createPropertyAssignment("_", (0, field_1.createNoopFieldDefinitionExpr)({ input: true }))], true)));
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* @example
|
|
52
|
-
* ```ts
|
|
53
|
-
* {
|
|
54
|
-
* message?: _$hello$hello_pb$Hello["message"] | null,
|
|
55
|
-
* // ...
|
|
56
|
-
* }
|
|
57
|
-
* ```
|
|
58
|
-
*/
|
|
59
|
-
function createInputObjectTypeShapeDecl(type) {
|
|
60
|
-
return typescript_1.default.factory.createTypeAliasDeclaration(undefined, [typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.ExportKeyword)], createInputObjectTypeShapeIdent(type), undefined, typescript_1.default.factory.createTypeLiteralNode(type.fields.map((field) => {
|
|
61
|
-
let typeNode = typescript_1.default.factory.createIndexedAccessTypeNode(typescript_1.default.factory.createTypeReferenceNode((0, util_1.createQualifiedName)(type.protoTypeFullName)), typescript_1.default.factory.createLiteralTypeNode(typescript_1.default.factory.createStringLiteral(field.protoJsName)));
|
|
62
|
-
if (field.type instanceof codegen_core_1.InputObjectType) {
|
|
63
|
-
typeNode = typescript_1.default.factory.createTypeReferenceNode(createInputObjectTypeShapeIdent(field.type));
|
|
64
|
-
if (field.isList()) {
|
|
65
|
-
typeNode = typescript_1.default.factory.createTypeReferenceNode("Array", [typeNode]);
|
|
66
|
-
}
|
|
22
|
+
function createInputObjectTypeCode(type, opts) {
|
|
23
|
+
const shapeTypeCode = (0, ts_poet_1.code) `
|
|
24
|
+
export type ${(0, util_1.shapeType)(type)} = {
|
|
25
|
+
${(0, ts_poet_1.joinCode)(type.fields.map((f) => {
|
|
26
|
+
let typeNode;
|
|
27
|
+
if (f.type instanceof codegen_core_1.InputObjectType) {
|
|
28
|
+
// @ts-expect-error f should be inferred as InputObjectField<InputObjectType>
|
|
29
|
+
typeNode = (0, ts_poet_1.code) `${(0, util_1.fieldTypeShape)(f, opts)}`;
|
|
30
|
+
if (f.isList())
|
|
31
|
+
typeNode = (0, ts_poet_1.code) `Array<${typeNode}>`;
|
|
67
32
|
}
|
|
68
|
-
|
|
69
|
-
typeNode =
|
|
70
|
-
typeNode,
|
|
71
|
-
typescript_1.default.factory.createLiteralTypeNode(typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.NullKeyword)),
|
|
72
|
-
]);
|
|
33
|
+
else {
|
|
34
|
+
typeNode = (0, ts_poet_1.code) `${(0, codegen_core_1.protoType)(type.proto, opts)}[${(0, ts_poet_1.literalOf)(f.proto.jsonName)}]`;
|
|
73
35
|
}
|
|
74
|
-
return
|
|
75
|
-
}))
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
typescript_1.default.factory.createPropertyAssignment("protobufMessage", typescript_1.default.factory.createObjectLiteralExpression([
|
|
95
|
-
typescript_1.default.factory.createPropertyAssignment("fullName", typescript_1.default.factory.createStringLiteral(type.proto.fullName.toString())),
|
|
96
|
-
typescript_1.default.factory.createPropertyAssignment("name", typescript_1.default.factory.createStringLiteral(type.proto.name)),
|
|
97
|
-
typescript_1.default.factory.createPropertyAssignment("package", typescript_1.default.factory.createStringLiteral(type.proto.file.package)),
|
|
98
|
-
], true)),
|
|
99
|
-
], true);
|
|
36
|
+
return f.isNullable() ? (0, ts_poet_1.code) `${f.name}?: ${typeNode} | null,` : (0, ts_poet_1.code) `${f.name}: ${typeNode},`;
|
|
37
|
+
}))}
|
|
38
|
+
};
|
|
39
|
+
`;
|
|
40
|
+
const refCode = (0, ts_poet_1.code) `
|
|
41
|
+
export const ${(0, util_1.pothosRef)(type)}: ${(0, ts_poet_1.imp)("InputObjectRef@@pothos/core")}<${(0, util_1.shapeType)(type)}> =
|
|
42
|
+
${(0, util_1.pothosBuilder)(type, opts)}.inputRef<${(0, util_1.shapeType)(type)}>(${(0, ts_poet_1.literalOf)(type.typeName)}).implement(
|
|
43
|
+
${(0, ts_poet_1.literalOf)((0, codegen_core_1.compact)({
|
|
44
|
+
description: type.description,
|
|
45
|
+
fields: (0, ts_poet_1.code) `t => ({${type.fields.length > 0
|
|
46
|
+
? type.fields.map((f) => (0, ts_poet_1.code) `${f.name}: ${(0, field_1.createFieldRefCode)(f, opts)},`)
|
|
47
|
+
: (0, ts_poet_1.code) `_: ${(0, field_1.createNoopFieldRefCode)({ input: true })}`}})`,
|
|
48
|
+
extensions: (0, codegen_core_1.protobufGraphQLExtensions)(type),
|
|
49
|
+
}))}
|
|
50
|
+
);
|
|
51
|
+
`;
|
|
52
|
+
return (0, ts_poet_1.code) `
|
|
53
|
+
${shapeTypeCode}
|
|
54
|
+
${refCode}
|
|
55
|
+
`;
|
|
100
56
|
}
|
|
57
|
+
exports.createInputObjectTypeCode = createInputObjectTypeCode;
|
|
@@ -1,103 +1,50 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
3
|
+
exports.createObjectTypeCode = void 0;
|
|
7
4
|
const codegen_core_1 = require("@proto-graphql/codegen-core");
|
|
8
|
-
const
|
|
5
|
+
const ts_poet_1 = require("ts-poet");
|
|
9
6
|
const field_1 = require("./field");
|
|
10
7
|
const util_1 = require("./util");
|
|
11
8
|
/**
|
|
12
9
|
* @example
|
|
13
10
|
* ```ts
|
|
14
|
-
* export const Hello = builder.objectRef<_$hello$hello_pb.Hello>("Hello")
|
|
15
|
-
* builder.objectType(Hello, {
|
|
11
|
+
* export const Hello$Ref = builder.objectRef<_$hello$hello_pb.Hello>("Hello")
|
|
12
|
+
* builder.objectType(Hello$Ref, {
|
|
16
13
|
* name: "Hello",
|
|
17
14
|
* // ...
|
|
18
15
|
* })
|
|
19
16
|
* ```
|
|
20
17
|
*/
|
|
21
|
-
function
|
|
22
|
-
const isInterface =
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
*/
|
|
53
|
-
function createObjectTypeFieldsFuncExpr(objType) {
|
|
54
|
-
return typescript_1.default.factory.createArrowFunction(undefined, undefined, [typescript_1.default.factory.createParameterDeclaration(undefined, undefined, undefined, "t", undefined, undefined, undefined)], undefined, typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.EqualsGreaterThanToken), typescript_1.default.factory.createParenthesizedExpression(typescript_1.default.factory.createObjectLiteralExpression(objType.fields.length > 0
|
|
55
|
-
? objType.fields.map((f) => typescript_1.default.factory.createPropertyAssignment(f.name, (0, field_1.createFieldDefinitionExpr)(f)))
|
|
56
|
-
: [typescript_1.default.factory.createPropertyAssignment("_", (0, field_1.createNoopFieldDefinitionExpr)({ input: false }))], true)));
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* @example
|
|
60
|
-
* ```ts
|
|
61
|
-
* isTypeOf(data) {
|
|
62
|
-
* return data instanceof _$hello$hello_pb.Hello;
|
|
63
|
-
*
|
|
64
|
-
* (source) =>
|
|
65
|
-
* // eslint-disable-next-line @typescript-eslint/ban-types
|
|
66
|
-
* (source as _$hello$hello_pb.Hello | { $type: string & {} }).$type === "hello.Hello",
|
|
67
|
-
* }
|
|
68
|
-
* ```
|
|
69
|
-
*/
|
|
70
|
-
function createIsTypeOfMethodExpr(objType) {
|
|
71
|
-
return typescript_1.default.factory.createArrowFunction(undefined, undefined, [typescript_1.default.factory.createParameterDeclaration(undefined, undefined, undefined, "source", undefined, undefined, undefined)], undefined, typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.EqualsGreaterThanToken), typescript_1.default.factory.createBlock([
|
|
72
|
-
typescript_1.default.factory.createReturnStatement(typescript_1.default.factory.createBinaryExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createParenthesizedExpression(typescript_1.default.factory.createAsExpression(typescript_1.default.factory.createIdentifier("source"), typescript_1.default.factory.createUnionTypeNode([
|
|
73
|
-
typescript_1.default.factory.createTypeReferenceNode((0, util_1.createQualifiedName)(objType.protoTypeFullName)),
|
|
74
|
-
typescript_1.default.factory.createTypeLiteralNode([
|
|
75
|
-
typescript_1.default.factory.createPropertySignature(undefined, "$type", undefined, typescript_1.default.factory.createIntersectionTypeNode([
|
|
76
|
-
typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.StringKeyword),
|
|
77
|
-
typescript_1.default.factory.createTypeLiteralNode([]),
|
|
78
|
-
])),
|
|
79
|
-
]),
|
|
80
|
-
]))), "$type"), typescript_1.default.SyntaxKind.EqualsEqualsEqualsToken, typescript_1.default.factory.createStringLiteral(objType.proto.fullName.toString()))),
|
|
81
|
-
], true));
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* @example
|
|
85
|
-
* ```ts
|
|
86
|
-
* {
|
|
87
|
-
* protobufMessage: {
|
|
88
|
-
* fullName: "...",
|
|
89
|
-
* name: "...",
|
|
90
|
-
* package: "...",
|
|
91
|
-
* },
|
|
92
|
-
* }
|
|
93
|
-
* ```
|
|
94
|
-
*/
|
|
95
|
-
function createExtensionsObjectLiteralExpr(objType) {
|
|
96
|
-
return typescript_1.default.factory.createObjectLiteralExpression([
|
|
97
|
-
typescript_1.default.factory.createPropertyAssignment("protobufMessage", typescript_1.default.factory.createObjectLiteralExpression([
|
|
98
|
-
typescript_1.default.factory.createPropertyAssignment("fullName", typescript_1.default.factory.createStringLiteral(objType.proto.fullName.toString())),
|
|
99
|
-
typescript_1.default.factory.createPropertyAssignment("name", typescript_1.default.factory.createStringLiteral(objType.proto.name)),
|
|
100
|
-
typescript_1.default.factory.createPropertyAssignment("package", typescript_1.default.factory.createStringLiteral(objType.proto.file.package)),
|
|
101
|
-
], true)),
|
|
102
|
-
], true);
|
|
18
|
+
function createObjectTypeCode(type, opts) {
|
|
19
|
+
const isInterface = type instanceof codegen_core_1.InterfaceType;
|
|
20
|
+
const typeOpts = {
|
|
21
|
+
name: type.typeName,
|
|
22
|
+
fields: (0, ts_poet_1.code) `t => ({${type.fields.length > 0
|
|
23
|
+
? (0, ts_poet_1.joinCode)(type.fields.map((f) => (0, ts_poet_1.code) `${f.name}: ${(0, field_1.createFieldRefCode)(f, opts)},`))
|
|
24
|
+
: (0, ts_poet_1.code) `_: ${(0, field_1.createNoopFieldRefCode)({ input: false })}`}})`,
|
|
25
|
+
description: type.description,
|
|
26
|
+
isTypeOf: isInterface
|
|
27
|
+
? undefined
|
|
28
|
+
: (0, ts_poet_1.code) `
|
|
29
|
+
(source) => {
|
|
30
|
+
return (source as ${(0, codegen_core_1.protoType)(type.proto, opts)} | { $type: string & {} }).$type
|
|
31
|
+
=== ${(0, ts_poet_1.literalOf)(type.proto.fullName.toString())};
|
|
32
|
+
}
|
|
33
|
+
`,
|
|
34
|
+
extensions: (0, codegen_core_1.protobufGraphQLExtensions)(type),
|
|
35
|
+
};
|
|
36
|
+
const buildRefFunc = (0, ts_poet_1.code) `${(0, util_1.pothosBuilder)(type, opts)}.${isInterface ? "interface" : "object"}Ref`;
|
|
37
|
+
const buildTypeFunc = (0, ts_poet_1.code) `${(0, util_1.pothosBuilder)(type, opts)}.${isInterface ? "interface" : "object"}Type`;
|
|
38
|
+
const refFuncTypeArg = isInterface
|
|
39
|
+
? (0, ts_poet_1.code) `
|
|
40
|
+
Pick<
|
|
41
|
+
${(0, codegen_core_1.protoType)(type.proto, opts)},
|
|
42
|
+
${(0, ts_poet_1.joinCode)(type.fields.map((f) => (0, ts_poet_1.code) `${(0, ts_poet_1.literalOf)(f.proto.jsonName)}`), { on: "|" })}
|
|
43
|
+
>`
|
|
44
|
+
: (0, codegen_core_1.protoType)(type.proto, opts);
|
|
45
|
+
return (0, ts_poet_1.code) `
|
|
46
|
+
export const ${(0, util_1.pothosRef)(type)} = ${buildRefFunc}<${refFuncTypeArg}>(${(0, ts_poet_1.literalOf)(type.typeName)});
|
|
47
|
+
${buildTypeFunc}(${(0, util_1.pothosRef)(type)}, ${(0, ts_poet_1.literalOf)((0, codegen_core_1.compact)(typeOpts))});
|
|
48
|
+
`;
|
|
103
49
|
}
|
|
50
|
+
exports.createObjectTypeCode = createObjectTypeCode;
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
3
|
+
exports.createOneofUnionTypeCode = void 0;
|
|
7
4
|
const codegen_core_1 = require("@proto-graphql/codegen-core");
|
|
8
|
-
const
|
|
5
|
+
const ts_poet_1 = require("ts-poet");
|
|
9
6
|
const util_1 = require("./util");
|
|
10
7
|
/**
|
|
11
8
|
* @example
|
|
@@ -16,61 +13,15 @@ const util_1 = require("./util");
|
|
|
16
13
|
* })
|
|
17
14
|
* ```
|
|
18
15
|
*/
|
|
19
|
-
function
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
exports.createOneofUnionTypeDslStmt = createOneofUnionTypeDslStmt;
|
|
30
|
-
/**
|
|
31
|
-
* @example
|
|
32
|
-
* ```ts
|
|
33
|
-
* {
|
|
34
|
-
* protobufMessage: {
|
|
35
|
-
* fullName: "...",
|
|
36
|
-
* name: "...",
|
|
37
|
-
* package: "...",
|
|
38
|
-
* },
|
|
39
|
-
* }
|
|
40
|
-
* ```
|
|
41
|
-
*/
|
|
42
|
-
function createExtensionsObjectLiteralExpr(type) {
|
|
43
|
-
if (type instanceof codegen_core_1.SquashedOneofUnionType) {
|
|
44
|
-
return typescript_1.default.factory.createObjectLiteralExpression([
|
|
45
|
-
typescript_1.default.factory.createPropertyAssignment("protobufMessage", typescript_1.default.factory.createObjectLiteralExpression([
|
|
46
|
-
typescript_1.default.factory.createPropertyAssignment("fullName", typescript_1.default.factory.createStringLiteral(type.proto.fullName.toString())),
|
|
47
|
-
typescript_1.default.factory.createPropertyAssignment("name", typescript_1.default.factory.createStringLiteral(type.proto.name)),
|
|
48
|
-
typescript_1.default.factory.createPropertyAssignment("package", typescript_1.default.factory.createStringLiteral(type.proto.file.package)),
|
|
49
|
-
typescript_1.default.factory.createPropertyAssignment("fields", typescript_1.default.factory.createArrayLiteralExpression(type.proto.oneofs[0].fields.map((f) => typescript_1.default.factory.createObjectLiteralExpression([
|
|
50
|
-
typescript_1.default.factory.createPropertyAssignment("name", typescript_1.default.factory.createStringLiteral(f.name)),
|
|
51
|
-
...(f.type && f.type.kind !== "Scalar"
|
|
52
|
-
? [
|
|
53
|
-
typescript_1.default.factory.createPropertyAssignment("type", typescript_1.default.factory.createStringLiteral(f.type.fullName.toString())),
|
|
54
|
-
]
|
|
55
|
-
: []),
|
|
56
|
-
], true)), true)),
|
|
57
|
-
], true)),
|
|
58
|
-
], true);
|
|
59
|
-
}
|
|
60
|
-
return typescript_1.default.factory.createObjectLiteralExpression([
|
|
61
|
-
typescript_1.default.factory.createPropertyAssignment("protobufOneof", typescript_1.default.factory.createObjectLiteralExpression([
|
|
62
|
-
typescript_1.default.factory.createPropertyAssignment("fullName", typescript_1.default.factory.createStringLiteral(type.proto.fullName.toString())),
|
|
63
|
-
typescript_1.default.factory.createPropertyAssignment("name", typescript_1.default.factory.createStringLiteral(type.proto.name)),
|
|
64
|
-
typescript_1.default.factory.createPropertyAssignment("messageName", typescript_1.default.factory.createStringLiteral(type.proto.parent.name)),
|
|
65
|
-
typescript_1.default.factory.createPropertyAssignment("package", typescript_1.default.factory.createStringLiteral(type.proto.parent.file.package)),
|
|
66
|
-
typescript_1.default.factory.createPropertyAssignment("fields", typescript_1.default.factory.createArrayLiteralExpression(type.proto.fields.map((f) => typescript_1.default.factory.createObjectLiteralExpression([
|
|
67
|
-
typescript_1.default.factory.createPropertyAssignment("name", typescript_1.default.factory.createStringLiteral(f.name)),
|
|
68
|
-
...(f.type && f.type.kind !== "Scalar"
|
|
69
|
-
? [
|
|
70
|
-
typescript_1.default.factory.createPropertyAssignment("type", typescript_1.default.factory.createStringLiteral(f.type.fullName.toString())),
|
|
71
|
-
]
|
|
72
|
-
: []),
|
|
73
|
-
], true)), true)),
|
|
74
|
-
], true)),
|
|
75
|
-
], true);
|
|
16
|
+
function createOneofUnionTypeCode(type, opts) {
|
|
17
|
+
const typeOpts = {
|
|
18
|
+
types: type.fields.map((f) => (0, util_1.pothosRef)(f.type)),
|
|
19
|
+
description: type.description,
|
|
20
|
+
extensions: (0, codegen_core_1.protobufGraphQLExtensions)(type),
|
|
21
|
+
};
|
|
22
|
+
return (0, ts_poet_1.code) `
|
|
23
|
+
export const ${(0, util_1.pothosRef)(type)} =
|
|
24
|
+
${(0, util_1.pothosBuilder)(type, opts)}.unionType(${(0, ts_poet_1.literalOf)(type.typeName)}, ${(0, ts_poet_1.literalOf)((0, codegen_core_1.compact)(typeOpts))});
|
|
25
|
+
`;
|
|
76
26
|
}
|
|
27
|
+
exports.createOneofUnionTypeCode = createOneofUnionTypeCode;
|
|
@@ -1,96 +1,69 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
4
24
|
};
|
|
5
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
* ```
|
|
13
|
-
*/
|
|
14
|
-
function createBuilderCallExpr(name, args) {
|
|
15
|
-
return typescript_1.default.factory.createCallExpression(createBuilderPropExpr(name), undefined, args);
|
|
16
|
-
}
|
|
17
|
-
exports.createBuilderCallExpr = createBuilderCallExpr;
|
|
18
|
-
/**
|
|
19
|
-
* @example
|
|
20
|
-
* ```
|
|
21
|
-
* builder.objectRef
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
function createBuilderPropExpr(name) {
|
|
25
|
-
return typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier("builder"), name);
|
|
26
|
-
}
|
|
27
|
-
exports.createBuilderPropExpr = createBuilderPropExpr;
|
|
28
|
-
function createDescriptionPropertyAssignment(gql) {
|
|
29
|
-
if (!gql.description)
|
|
30
|
-
return null;
|
|
31
|
-
return typescript_1.default.factory.createPropertyAssignment("description", typescript_1.default.factory.createStringLiteral(gql.description));
|
|
32
|
-
}
|
|
33
|
-
exports.createDescriptionPropertyAssignment = createDescriptionPropertyAssignment;
|
|
34
|
-
function createDeprecationPropertyAssignment(gql) {
|
|
35
|
-
const reason = gql.deprecationReason;
|
|
36
|
-
if (!reason)
|
|
37
|
-
return null;
|
|
38
|
-
return typescript_1.default.factory.createPropertyAssignment("deprecationReason", typescript_1.default.factory.createStringLiteral(reason));
|
|
26
|
+
exports.pothosBuilder = exports.fieldTypeShape = exports.fieldTypeRef = exports.shapeTypeName = exports.shapeType = exports.pothosRef = void 0;
|
|
27
|
+
const codegen_core_1 = require("@proto-graphql/codegen-core");
|
|
28
|
+
const path = __importStar(require("path"));
|
|
29
|
+
const ts_poet_1 = require("ts-poet");
|
|
30
|
+
function pothosRef(type) {
|
|
31
|
+
return (0, ts_poet_1.code) `${pothosRefName(type)}`;
|
|
39
32
|
}
|
|
40
|
-
exports.
|
|
41
|
-
function
|
|
42
|
-
|
|
43
|
-
return fn;
|
|
44
|
-
}
|
|
45
|
-
return `${typeof fn[0] === "string" ? fn[0] : fullNameString(fn[0])}.${fn[1]}`;
|
|
46
|
-
}
|
|
47
|
-
exports.fullNameString = fullNameString;
|
|
48
|
-
function createFullNameExpr(fn) {
|
|
49
|
-
if (typeof fn === "string") {
|
|
50
|
-
return typescript_1.default.factory.createIdentifier(fn);
|
|
51
|
-
}
|
|
52
|
-
return typescript_1.default.factory.createPropertyAccessExpression(typeof fn[0] === "string" ? typescript_1.default.factory.createIdentifier(fn[0]) : createFullNameExpr(fn[0]), fn[1]);
|
|
33
|
+
exports.pothosRef = pothosRef;
|
|
34
|
+
function pothosRefName(type) {
|
|
35
|
+
return `${type.typeName}$Ref`;
|
|
53
36
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (typeof fn === "string") {
|
|
57
|
-
return typescript_1.default.factory.createIdentifier(fn);
|
|
58
|
-
}
|
|
59
|
-
return typescript_1.default.factory.createQualifiedName(typeof fn[0] === "string" ? typescript_1.default.factory.createIdentifier(fn[0]) : createQualifiedName(fn[0]), fn[1]);
|
|
37
|
+
function shapeType(type) {
|
|
38
|
+
return (0, ts_poet_1.code) `${shapeTypeName(type)}`;
|
|
60
39
|
}
|
|
61
|
-
exports.
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
* ```
|
|
65
|
-
* import * as foo$bar$baz from "foo/bar/baz";
|
|
66
|
-
* ```
|
|
67
|
-
*/
|
|
68
|
-
function createImportDecl({ alias, module, type, }) {
|
|
69
|
-
return typescript_1.default.factory.createImportDeclaration(undefined, undefined, typescript_1.default.factory.createImportClause(false, undefined, type === "namespace"
|
|
70
|
-
? typescript_1.default.factory.createNamespaceImport(typescript_1.default.factory.createIdentifier(alias))
|
|
71
|
-
: type === "named"
|
|
72
|
-
? typescript_1.default.factory.createNamedImports([
|
|
73
|
-
typescript_1.default.factory.createImportSpecifier(false, undefined, typescript_1.default.factory.createIdentifier(alias)),
|
|
74
|
-
])
|
|
75
|
-
: undefined), typescript_1.default.factory.createStringLiteral(module));
|
|
40
|
+
exports.shapeType = shapeType;
|
|
41
|
+
function shapeTypeName(type) {
|
|
42
|
+
return `${type.typeName}$Shape`;
|
|
76
43
|
}
|
|
77
|
-
exports.
|
|
78
|
-
function
|
|
79
|
-
|
|
44
|
+
exports.shapeTypeName = shapeTypeName;
|
|
45
|
+
function fieldTypeRef(field, opts) {
|
|
46
|
+
const importPath = (0, codegen_core_1.generatedGraphQLTypeImportPath)(field, opts);
|
|
47
|
+
if (importPath == null)
|
|
48
|
+
return pothosRef(field.type);
|
|
49
|
+
const imported = (0, ts_poet_1.imp)(`IMPORTED_PLACEHOLDER@${importPath}`);
|
|
50
|
+
imported.symbol = pothosRefName(field.type); // NOTE: Workaround for ts-poet not recognizing "$" as an identifier
|
|
51
|
+
return (0, ts_poet_1.code) `${imported}`;
|
|
80
52
|
}
|
|
81
|
-
exports.
|
|
82
|
-
function
|
|
83
|
-
|
|
53
|
+
exports.fieldTypeRef = fieldTypeRef;
|
|
54
|
+
function fieldTypeShape(field, opts) {
|
|
55
|
+
const importPath = (0, codegen_core_1.generatedGraphQLTypeImportPath)(field, opts);
|
|
56
|
+
if (importPath == null)
|
|
57
|
+
return shapeType(field.type);
|
|
58
|
+
const imported = (0, ts_poet_1.imp)(`IMPORTED_PLACEHOLDER@${importPath}`);
|
|
59
|
+
imported.symbol = shapeTypeName(field.type); // NOTE: Workaround for ts-poet not recognizing "$" as an identifier
|
|
60
|
+
return (0, ts_poet_1.code) `${imported}`;
|
|
84
61
|
}
|
|
85
|
-
exports.
|
|
86
|
-
function
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return false;
|
|
92
|
-
set.add(key);
|
|
93
|
-
return true;
|
|
94
|
-
};
|
|
62
|
+
exports.fieldTypeShape = fieldTypeShape;
|
|
63
|
+
function pothosBuilder(type, opts) {
|
|
64
|
+
const importPath = opts.pothos.builderPath.startsWith(".")
|
|
65
|
+
? path.relative(path.dirname((0, codegen_core_1.filename)(type, opts)), opts.pothos.builderPath)
|
|
66
|
+
: opts.pothos.builderPath;
|
|
67
|
+
return (0, ts_poet_1.code) `${(0, ts_poet_1.imp)(`builder@${importPath}`)}`;
|
|
95
68
|
}
|
|
96
|
-
exports.
|
|
69
|
+
exports.pothosBuilder = pothosBuilder;
|
package/lib/printer.js
CHANGED
|
@@ -1,64 +1,34 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.generateFiles = void 0;
|
|
7
|
-
const typescript_1 = __importDefault(require("typescript"));
|
|
8
4
|
const codegen_core_1 = require("@proto-graphql/codegen-core");
|
|
9
5
|
const dslgen_1 = require("./dslgen");
|
|
10
6
|
function generateFiles(registry, file, opts) {
|
|
11
|
-
|
|
12
|
-
const types = (0, codegen_core_1.collectTypesFromFile)(
|
|
13
|
-
switch (opts.fileLayout) {
|
|
7
|
+
opts.printer.protobuf = "ts-proto"; // default
|
|
8
|
+
const types = (0, codegen_core_1.collectTypesFromFile)(file, opts.type, registry);
|
|
9
|
+
switch (opts.printer.fileLayout) {
|
|
14
10
|
case "proto_file": {
|
|
15
11
|
return [
|
|
16
12
|
{
|
|
17
|
-
filename:
|
|
18
|
-
content:
|
|
13
|
+
filename: (0, codegen_core_1.filenameFromProtoFile)(file, opts.printer),
|
|
14
|
+
content: (0, codegen_core_1.printCodes)(createCodes(types, opts.printer), "protoc-gen-pothos", file),
|
|
19
15
|
},
|
|
20
16
|
];
|
|
21
17
|
}
|
|
22
18
|
case "graphql_type": {
|
|
23
19
|
return types.map((t) => ({
|
|
24
|
-
filename:
|
|
25
|
-
content:
|
|
20
|
+
filename: (0, codegen_core_1.filename)(t, opts.printer),
|
|
21
|
+
content: (0, codegen_core_1.printCodes)(createCodes([t], opts.printer), "protoc-gen-pothos", file),
|
|
26
22
|
}));
|
|
27
23
|
}
|
|
28
24
|
/* istanbul ignore next */
|
|
29
25
|
default: {
|
|
30
|
-
const _exhaustiveCheck = opts.fileLayout;
|
|
26
|
+
const _exhaustiveCheck = opts.printer.fileLayout;
|
|
31
27
|
throw "unreachable";
|
|
32
28
|
}
|
|
33
29
|
}
|
|
34
30
|
}
|
|
35
31
|
exports.generateFiles = generateFiles;
|
|
36
|
-
function
|
|
37
|
-
|
|
38
|
-
// `import builder from "../../builder";`
|
|
39
|
-
// `import * as _$hello$hello_pb from "./hello/hello_pb";`
|
|
40
|
-
...(0, dslgen_1.createImportDecls)(types),
|
|
41
|
-
// `export cosnt Hello = objectType({ ... });`
|
|
42
|
-
// `export cosnt HelloInput = inputObjectType({ ... });`
|
|
43
|
-
// `export cosnt Oneof = unionType({ ... });`
|
|
44
|
-
// `export const Role = enumType({ ... });`
|
|
45
|
-
...(0, dslgen_1.createTypeDslStmts)(types),
|
|
46
|
-
];
|
|
47
|
-
if (ast.length === 0) {
|
|
48
|
-
ast.push(
|
|
49
|
-
// `export {}`
|
|
50
|
-
typescript_1.default.factory.createExportDeclaration(undefined, undefined, false, typescript_1.default.factory.createNamedExports([]), undefined));
|
|
51
|
-
}
|
|
52
|
-
const nexusFile = typescript_1.default.factory.updateSourceFile(typescript_1.default.createSourceFile("generated.ts", "", typescript_1.default.ScriptTarget.Latest, false, typescript_1.default.ScriptKind.TS), ast, false);
|
|
53
|
-
const printer = typescript_1.default.createPrinter({ newLine: typescript_1.default.NewLineKind.LineFeed });
|
|
54
|
-
const result = printer.printFile(nexusFile);
|
|
55
|
-
const content = [
|
|
56
|
-
"// Code generated by protoc-gen-pothos. DO NOT EDIT.",
|
|
57
|
-
`// source: ${file.descriptor.getName()}`,
|
|
58
|
-
"",
|
|
59
|
-
"/* eslint-disable */",
|
|
60
|
-
"",
|
|
61
|
-
result,
|
|
62
|
-
].join("\n");
|
|
63
|
-
return content;
|
|
32
|
+
function createCodes(types, opts) {
|
|
33
|
+
return [...(0, dslgen_1.createTypeDslCodes)(types, opts)];
|
|
64
34
|
}
|
package/lib/process.js
CHANGED
|
@@ -3,4 +3,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.processRequest = void 0;
|
|
4
4
|
const protoc_plugin_helpers_1 = require("@proto-graphql/protoc-plugin-helpers");
|
|
5
5
|
const printer_1 = require("./printer");
|
|
6
|
-
exports.processRequest = (0, protoc_plugin_helpers_1.createProcessor)({ generateFiles: printer_1.generateFiles });
|
|
6
|
+
exports.processRequest = (0, protoc_plugin_helpers_1.createProcessor)({ generateFiles: printer_1.generateFiles, dsl: "pothos" });
|