protoc-gen-pothos 0.1.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 +14 -0
- package/README.md +5 -0
- package/bin/protoc-gen-pothos +3 -0
- package/lib/dslgen/index.js +17 -0
- package/lib/dslgen/printers/enumType.js +37 -0
- package/lib/dslgen/printers/field.js +111 -0
- package/lib/dslgen/printers/fieldResolver/enumFieldResolver.js +68 -0
- package/lib/dslgen/printers/fieldResolver/nonNullResolver.js +11 -0
- package/lib/dslgen/printers/fieldResolver/oneofUnionResolver.js +61 -0
- package/lib/dslgen/printers/index.js +62 -0
- package/lib/dslgen/printers/inputObjectType.js +79 -0
- package/lib/dslgen/printers/objectType.js +81 -0
- package/lib/dslgen/printers/oneofUnionType.js +27 -0
- package/lib/dslgen/printers/util.js +96 -0
- package/lib/printer.js +62 -0
- package/lib/process.js +6 -0
- package/lib/protoc-gen-pothos.js +5 -0
- package/module/dslgen/index.js +1 -0
- package/module/dslgen/printers/enumType.js +30 -0
- package/module/dslgen/printers/field.js +103 -0
- package/module/dslgen/printers/fieldResolver/enumFieldResolver.js +61 -0
- package/module/dslgen/printers/fieldResolver/nonNullResolver.js +4 -0
- package/module/dslgen/printers/fieldResolver/oneofUnionResolver.js +54 -0
- package/module/dslgen/printers/index.js +53 -0
- package/module/dslgen/printers/inputObjectType.js +72 -0
- package/module/dslgen/printers/objectType.js +74 -0
- package/module/dslgen/printers/oneofUnionType.js +20 -0
- package/module/dslgen/printers/util.js +79 -0
- package/module/printer.js +55 -0
- package/module/process.js +3 -0
- package/module/protoc-gen-pothos.js +3 -0
- package/package.json +55 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# protoc-gen-pothos
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#200](https://github.com/proto-graphql/proto-graphql-js/pull/200) [`515eabd`](https://github.com/proto-graphql/proto-graphql-js/commit/515eabd2f39baa0a99ae057b1b30a4ccc4149f66) Thanks [@izumin5210](https://github.com/izumin5210)! - support Pothos GraphQL
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [[`515eabd`](https://github.com/proto-graphql/proto-graphql-js/commit/515eabd2f39baa0a99ae057b1b30a4ccc4149f66)]:
|
|
12
|
+
- @proto-graphql/codegen-core@0.1.0
|
|
13
|
+
- @proto-graphql/proto-descriptors@0.1.0
|
|
14
|
+
- @proto-graphql/protoc-plugin-helpers@0.1.0
|
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./printers"), exports);
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createEnumTypeDslStmt = void 0;
|
|
7
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
8
|
+
const util_1 = require("./util");
|
|
9
|
+
/**
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* export cosnt Hello = builder.enumType("Hello", {
|
|
13
|
+
* // ...
|
|
14
|
+
* })
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
function createEnumTypeDslStmt(type) {
|
|
18
|
+
return (0, util_1.createDslExportConstStmt)(type.pothosRefObjectName, (0, util_1.createBuilderCallExpr)("enumType", [
|
|
19
|
+
typescript_1.default.factory.createStringLiteral(type.typeName),
|
|
20
|
+
typescript_1.default.factory.createObjectLiteralExpression([
|
|
21
|
+
(0, util_1.createDescriptionPropertyAssignment)(type),
|
|
22
|
+
typescript_1.default.factory.createPropertyAssignment("values", typescript_1.default.factory.createAsExpression(typescript_1.default.factory.createObjectLiteralExpression(type.values
|
|
23
|
+
.filter((v) => !v.isIgnored() && !v.isUnespecified())
|
|
24
|
+
.map((ev) => typescript_1.default.factory.createPropertyAssignment(ev.name, createEnumValueExpr(ev))), true // multiline
|
|
25
|
+
), typescript_1.default.factory.createTypeReferenceNode("const"))),
|
|
26
|
+
].filter((0, util_1.onlyNonNull)()), true),
|
|
27
|
+
]));
|
|
28
|
+
}
|
|
29
|
+
exports.createEnumTypeDslStmt = createEnumTypeDslStmt;
|
|
30
|
+
function createEnumValueExpr(ev) {
|
|
31
|
+
return typescript_1.default.factory.createObjectLiteralExpression([
|
|
32
|
+
(0, util_1.createDescriptionPropertyAssignment)(ev),
|
|
33
|
+
(0, util_1.createDeprecationPropertyAssignment)(ev),
|
|
34
|
+
typescript_1.default.factory.createPropertyAssignment("value", typescript_1.default.factory.createNumericLiteral(ev.number)),
|
|
35
|
+
].filter((0, util_1.onlyNonNull)()), true // multiline
|
|
36
|
+
);
|
|
37
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createNoopFieldDefinitionExpr = exports.createFieldDefinitionExpr = void 0;
|
|
7
|
+
const codegen_core_1 = require("@proto-graphql/codegen-core");
|
|
8
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
9
|
+
const enumFieldResolver_1 = require("./fieldResolver/enumFieldResolver");
|
|
10
|
+
const nonNullResolver_1 = require("./fieldResolver/nonNullResolver");
|
|
11
|
+
const oneofUnionResolver_1 = require("./fieldResolver/oneofUnionResolver");
|
|
12
|
+
const util_1 = require("./util");
|
|
13
|
+
/**
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* t.expose("name", {
|
|
17
|
+
* type: "Boolean",
|
|
18
|
+
* nullable: true,
|
|
19
|
+
* description: "...",
|
|
20
|
+
* })
|
|
21
|
+
* ```
|
|
22
|
+
* ```ts
|
|
23
|
+
* t.field({
|
|
24
|
+
* type: "Boolean",
|
|
25
|
+
* nullable: true,
|
|
26
|
+
* description: "...",
|
|
27
|
+
* resolve() {
|
|
28
|
+
* return true
|
|
29
|
+
* }
|
|
30
|
+
* })
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
function createFieldDefinitionExpr(field) {
|
|
34
|
+
let typeExpr = field.type instanceof codegen_core_1.ScalarType
|
|
35
|
+
? typescript_1.default.factory.createStringLiteral(field.type.typeName)
|
|
36
|
+
: typescript_1.default.factory.createIdentifier(field.type.pothosRefObjectName);
|
|
37
|
+
const isInput = field instanceof codegen_core_1.InputObjectField;
|
|
38
|
+
const nullableToken = typescript_1.default.factory.createToken(isInput ? typescript_1.default.SyntaxKind.FalseKeyword : typescript_1.default.SyntaxKind.TrueKeyword);
|
|
39
|
+
const nonNullableToken = typescript_1.default.factory.createToken(isInput ? typescript_1.default.SyntaxKind.TrueKeyword : typescript_1.default.SyntaxKind.FalseKeyword);
|
|
40
|
+
let nullableExpr = field.isNullable() ? nullableToken : nonNullableToken;
|
|
41
|
+
// let defaultValueExpr: ts.Expression | undefined;
|
|
42
|
+
if (field.isList()) {
|
|
43
|
+
typeExpr = typescript_1.default.factory.createArrayLiteralExpression([typeExpr], false);
|
|
44
|
+
// if (isInput) {
|
|
45
|
+
// defaultValueExpr = ts.factory.createArrayLiteralExpression([], false);
|
|
46
|
+
// }
|
|
47
|
+
nullableExpr = typescript_1.default.factory.createObjectLiteralExpression([
|
|
48
|
+
typescript_1.default.factory.createPropertyAssignment("list", nullableExpr),
|
|
49
|
+
typescript_1.default.factory.createPropertyAssignment("items", nonNullableToken),
|
|
50
|
+
], false);
|
|
51
|
+
}
|
|
52
|
+
let createResolveStmts;
|
|
53
|
+
if (!isInput) {
|
|
54
|
+
if (field.type instanceof codegen_core_1.ObjectType && !field.isNullable()) {
|
|
55
|
+
createResolveStmts = (sourceExpr) => (0, nonNullResolver_1.createNonNullResolverStmts)(typescript_1.default.factory.createPropertyAccessExpression(sourceExpr, field.protoJsName));
|
|
56
|
+
}
|
|
57
|
+
if (field.type instanceof codegen_core_1.EnumType && field instanceof codegen_core_1.ObjectField) {
|
|
58
|
+
createResolveStmts = (sourceExpr) => (0, enumFieldResolver_1.createEnumResolverStmts)(typescript_1.default.factory.createPropertyAccessExpression(sourceExpr, field.protoJsName), field);
|
|
59
|
+
}
|
|
60
|
+
if (field instanceof codegen_core_1.ObjectOneofField) {
|
|
61
|
+
createResolveStmts = (sourceExpr) => (0, oneofUnionResolver_1.createOneofUnionResolverStmts)(sourceExpr, field);
|
|
62
|
+
}
|
|
63
|
+
if (field.type instanceof codegen_core_1.SquashedOneofUnionType && field instanceof codegen_core_1.ObjectField) {
|
|
64
|
+
createResolveStmts = (sourceExpr) => (0, oneofUnionResolver_1.createOneofUnionResolverStmts)(typescript_1.default.factory.createPropertyAccessExpression(sourceExpr, field.protoJsName), field);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const fieldOptionsExpr = typescript_1.default.factory.createObjectLiteralExpression([
|
|
68
|
+
typescript_1.default.factory.createPropertyAssignment("type", typeExpr),
|
|
69
|
+
typescript_1.default.factory.createPropertyAssignment(isInput ? "required" : "nullable", nullableExpr),
|
|
70
|
+
(0, util_1.createDescriptionPropertyAssignment)(field),
|
|
71
|
+
(0, util_1.createDeprecationPropertyAssignment)(field),
|
|
72
|
+
// defaultValueExpr && ts.factory.createPropertyAssignment("defaultValue", defaultValueExpr),
|
|
73
|
+
createResolveStmts && typescript_1.default.factory.createPropertyAssignment("resolve", createResolverExpr(createResolveStmts)),
|
|
74
|
+
].filter((0, util_1.onlyNonNull)()), true);
|
|
75
|
+
const shouldUseFieldFunc = field instanceof codegen_core_1.InputObjectField || createResolveStmts != null;
|
|
76
|
+
return typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier("t"), typescript_1.default.factory.createIdentifier(shouldUseFieldFunc ? "field" : "expose")), undefined, shouldUseFieldFunc ? [fieldOptionsExpr] : [typescript_1.default.factory.createStringLiteral(field.protoJsName), fieldOptionsExpr]);
|
|
77
|
+
}
|
|
78
|
+
exports.createFieldDefinitionExpr = createFieldDefinitionExpr;
|
|
79
|
+
/**
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* t.field( {
|
|
83
|
+
* type: "Boolean",
|
|
84
|
+
* nullable: true,
|
|
85
|
+
* description: "noop field",
|
|
86
|
+
* resolve() {
|
|
87
|
+
* return true
|
|
88
|
+
* }
|
|
89
|
+
* })
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
function createNoopFieldDefinitionExpr(opts) {
|
|
93
|
+
return typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier("t"), typescript_1.default.factory.createIdentifier("field")), undefined, [
|
|
94
|
+
typescript_1.default.factory.createObjectLiteralExpression([
|
|
95
|
+
typescript_1.default.factory.createPropertyAssignment("type", typescript_1.default.factory.createStringLiteral("Boolean")),
|
|
96
|
+
opts.input
|
|
97
|
+
? typescript_1.default.factory.createPropertyAssignment("required", typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.FalseKeyword))
|
|
98
|
+
: typescript_1.default.factory.createPropertyAssignment("nullable", typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.TrueKeyword)),
|
|
99
|
+
typescript_1.default.factory.createPropertyAssignment("description", typescript_1.default.factory.createStringLiteral("noop field")),
|
|
100
|
+
opts.input
|
|
101
|
+
? null
|
|
102
|
+
: typescript_1.default.factory.createMethodDeclaration(undefined, undefined, undefined, "resolve", undefined, undefined, [], undefined, typescript_1.default.factory.createBlock([
|
|
103
|
+
typescript_1.default.factory.createReturnStatement(typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.TrueKeyword)),
|
|
104
|
+
])),
|
|
105
|
+
].filter((0, util_1.onlyNonNull)()), true),
|
|
106
|
+
]);
|
|
107
|
+
}
|
|
108
|
+
exports.createNoopFieldDefinitionExpr = createNoopFieldDefinitionExpr;
|
|
109
|
+
function createResolverExpr(createStmts) {
|
|
110
|
+
return typescript_1.default.factory.createArrowFunction(undefined, undefined, [typescript_1.default.factory.createParameterDeclaration(undefined, undefined, undefined, typescript_1.default.factory.createIdentifier("source"))], undefined, typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.EqualsGreaterThanToken), typescript_1.default.factory.createBlock(createStmts(typescript_1.default.factory.createIdentifier("source")), true));
|
|
111
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createEnumResolverStmts = void 0;
|
|
7
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
8
|
+
const util_1 = require("../util");
|
|
9
|
+
/**
|
|
10
|
+
* @example nullable
|
|
11
|
+
* ```ts
|
|
12
|
+
* if (!root.myEnum || root.myEnum === _$enums.myEnum.MY_ENUM_UNSPECIFIED) {
|
|
13
|
+
* return null
|
|
14
|
+
* }
|
|
15
|
+
* return root.myEnum
|
|
16
|
+
* ```
|
|
17
|
+
* @example notNull
|
|
18
|
+
* ```ts
|
|
19
|
+
* if (!root.myEnum || root.myEnum === _$enums.myEnum.MY_ENUM_UNSPECIFIED) {
|
|
20
|
+
* throw new Error("Message.field is required field. but got null or unspecified.")
|
|
21
|
+
* }
|
|
22
|
+
* return root.myEnum
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
function createEnumResolverStmts(valueExpr, field) {
|
|
26
|
+
let whenNullStmt = field.isNullable() && !field.isList()
|
|
27
|
+
? typescript_1.default.factory.createReturnStatement(typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.NullKeyword))
|
|
28
|
+
: typescript_1.default.factory.createThrowStatement(typescript_1.default.factory.createNewExpression(typescript_1.default.factory.createIdentifier("Error"), undefined, [
|
|
29
|
+
typescript_1.default.factory.createStringLiteral(`${field.name} is required field. But got unspecified.`),
|
|
30
|
+
]));
|
|
31
|
+
whenNullStmt = typescript_1.default.factory.createBlock([whenNullStmt], true // multiline
|
|
32
|
+
);
|
|
33
|
+
if (field.isList()) {
|
|
34
|
+
const guardStmts = createGuardStmts(typescript_1.default.factory.createIdentifier("item"), whenNullStmt, field.type.unspecifiedValue, field.type.valuesWithIgnored);
|
|
35
|
+
if (guardStmts.length === 0) {
|
|
36
|
+
return [typescript_1.default.factory.createReturnStatement(valueExpr)];
|
|
37
|
+
}
|
|
38
|
+
return [
|
|
39
|
+
typescript_1.default.factory.createReturnStatement(createMapExpr(valueExpr, (itemExpr) => [...guardStmts, typescript_1.default.factory.createReturnStatement(itemExpr)])),
|
|
40
|
+
];
|
|
41
|
+
}
|
|
42
|
+
return [
|
|
43
|
+
...createGuardStmts(valueExpr, whenNullStmt, field.type.unspecifiedValue, field.type.valuesWithIgnored),
|
|
44
|
+
typescript_1.default.factory.createReturnStatement(valueExpr),
|
|
45
|
+
];
|
|
46
|
+
}
|
|
47
|
+
exports.createEnumResolverStmts = createEnumResolverStmts;
|
|
48
|
+
function createGuardStmts(valueExpr, thenStmt, unspecifiedValue, ignoredValues) {
|
|
49
|
+
return [
|
|
50
|
+
unspecifiedValue
|
|
51
|
+
? typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createBinaryExpression(valueExpr, typescript_1.default.SyntaxKind.EqualsEqualsEqualsToken, (0, util_1.createFullNameExpr)(unspecifiedValue.fullName)), thenStmt)
|
|
52
|
+
: null,
|
|
53
|
+
...ignoredValues.map((ev) => ev.isIgnored()
|
|
54
|
+
? typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createBinaryExpression(valueExpr, typescript_1.default.SyntaxKind.EqualsEqualsEqualsToken, (0, util_1.createFullNameExpr)(ev.fullName)), typescript_1.default.factory.createBlock([
|
|
55
|
+
typescript_1.default.factory.createThrowStatement(typescript_1.default.factory.createNewExpression(typescript_1.default.factory.createIdentifier("Error"), undefined, [
|
|
56
|
+
typescript_1.default.factory.createStringLiteral(`${ev.name} is ignored in GraphQL schema`),
|
|
57
|
+
])),
|
|
58
|
+
], true // multiline
|
|
59
|
+
))
|
|
60
|
+
: null),
|
|
61
|
+
].filter((0, util_1.onlyNonNull)());
|
|
62
|
+
}
|
|
63
|
+
function createMapExpr(listExpr, blockFn) {
|
|
64
|
+
return typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createPropertyAccessExpression(listExpr, "map"), undefined, [
|
|
65
|
+
typescript_1.default.factory.createArrowFunction(undefined, undefined, [typescript_1.default.factory.createParameterDeclaration(undefined, undefined, undefined, "item", undefined, undefined, undefined)], undefined, typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.EqualsGreaterThanToken), typescript_1.default.factory.createBlock(blockFn(typescript_1.default.factory.createIdentifier("item")), true // multiline
|
|
66
|
+
)),
|
|
67
|
+
]);
|
|
68
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createNonNullResolverStmts = void 0;
|
|
7
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
8
|
+
function createNonNullResolverStmts(valueExpr) {
|
|
9
|
+
return [typescript_1.default.factory.createReturnStatement(typescript_1.default.factory.createNonNullExpression(valueExpr))];
|
|
10
|
+
}
|
|
11
|
+
exports.createNonNullResolverStmts = createNonNullResolverStmts;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createOneofUnionResolverStmts = void 0;
|
|
7
|
+
const codegen_core_1 = require("@proto-graphql/codegen-core");
|
|
8
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
9
|
+
const util_1 = require("../util");
|
|
10
|
+
/**
|
|
11
|
+
* @example nullable
|
|
12
|
+
* ```ts
|
|
13
|
+
* const value = source.v1 ?? source.v2 ?? source.v3;
|
|
14
|
+
* if (value != null) {
|
|
15
|
+
* throw new Error("...");
|
|
16
|
+
* }
|
|
17
|
+
* return value
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
function createOneofUnionResolverStmts(sourceExpr, field) {
|
|
21
|
+
const createResolverStmts = (sourceExpr, field, { nullable }) => {
|
|
22
|
+
const createFieldExpr = (memberField) => {
|
|
23
|
+
if (field instanceof codegen_core_1.ObjectOneofField) {
|
|
24
|
+
return typescript_1.default.factory.createPropertyAccessExpression(sourceExpr, memberField.protoJsName);
|
|
25
|
+
}
|
|
26
|
+
return typescript_1.default.factory.createPropertyAccessChain(sourceExpr, typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.QuestionDotToken), memberField.protoJsName);
|
|
27
|
+
};
|
|
28
|
+
const valueExpr = typescript_1.default.factory.createIdentifier("value");
|
|
29
|
+
return [
|
|
30
|
+
typescript_1.default.factory.createVariableStatement(undefined, typescript_1.default.factory.createVariableDeclarationList([
|
|
31
|
+
typescript_1.default.factory.createVariableDeclaration("value", undefined, undefined,
|
|
32
|
+
// obj.field1 ?? obj.field2 ?? obj.field3 ?? ...
|
|
33
|
+
field.type.fields.reduceRight((expr, field) => {
|
|
34
|
+
if (expr == null) {
|
|
35
|
+
return createFieldExpr(field);
|
|
36
|
+
}
|
|
37
|
+
return typescript_1.default.factory.createBinaryExpression(createFieldExpr(field), typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.QuestionQuestionToken), expr);
|
|
38
|
+
}, null)),
|
|
39
|
+
], typescript_1.default.NodeFlags.Const)),
|
|
40
|
+
nullable
|
|
41
|
+
? undefined
|
|
42
|
+
: typescript_1.default.factory.createIfStatement(typescript_1.default.factory.createBinaryExpression(valueExpr, typescript_1.default.SyntaxKind.EqualsEqualsToken, typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.NullKeyword)), typescript_1.default.factory.createBlock([
|
|
43
|
+
typescript_1.default.factory.createThrowStatement(typescript_1.default.factory.createNewExpression(typescript_1.default.factory.createIdentifier("Error"), undefined, [
|
|
44
|
+
typescript_1.default.factory.createStringLiteral(`${field.name} should not be null`),
|
|
45
|
+
])),
|
|
46
|
+
], true)),
|
|
47
|
+
typescript_1.default.factory.createReturnStatement(valueExpr),
|
|
48
|
+
].filter((0, util_1.onlyNonNull)());
|
|
49
|
+
};
|
|
50
|
+
if (!(field instanceof codegen_core_1.ObjectField && field.isList())) {
|
|
51
|
+
return createResolverStmts(sourceExpr, field, { nullable: field.isNullable() });
|
|
52
|
+
}
|
|
53
|
+
return [
|
|
54
|
+
typescript_1.default.factory.createReturnStatement(typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createPropertyAccessExpression(sourceExpr, "map"), undefined, [
|
|
55
|
+
typescript_1.default.factory.createArrowFunction(undefined, undefined, [
|
|
56
|
+
typescript_1.default.factory.createParameterDeclaration(undefined, undefined, undefined, "item", undefined, undefined, undefined),
|
|
57
|
+
], undefined, typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.EqualsGreaterThanToken), typescript_1.default.factory.createBlock(createResolverStmts(typescript_1.default.factory.createIdentifier("item"), field, { nullable: false }), true)),
|
|
58
|
+
])),
|
|
59
|
+
];
|
|
60
|
+
}
|
|
61
|
+
exports.createOneofUnionResolverStmts = createOneofUnionResolverStmts;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createTypeDslStmts = exports.createReExportStmts = exports.createImportDecls = void 0;
|
|
7
|
+
const codegen_core_1 = require("@proto-graphql/codegen-core");
|
|
8
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
9
|
+
const enumType_1 = require("./enumType");
|
|
10
|
+
const inputObjectType_1 = require("./inputObjectType");
|
|
11
|
+
const objectType_1 = require("./objectType");
|
|
12
|
+
const oneofUnionType_1 = require("./oneofUnionType");
|
|
13
|
+
const util_1 = require("./util");
|
|
14
|
+
function createImportDecls(types) {
|
|
15
|
+
return types
|
|
16
|
+
.flatMap((t) => t.importModules)
|
|
17
|
+
.filter((0, util_1.onlyUnique)((m) => JSON.stringify([m.alias, m.module])))
|
|
18
|
+
.sort(({ module: a }, { module: b }) => {
|
|
19
|
+
const pat = /^\.+\//;
|
|
20
|
+
const [aIsRel, bIsRel] = [a.match(pat), b.match(pat)];
|
|
21
|
+
if (aIsRel && !bIsRel)
|
|
22
|
+
return 1;
|
|
23
|
+
if (!aIsRel && bIsRel)
|
|
24
|
+
return -1;
|
|
25
|
+
if (a < b)
|
|
26
|
+
return -1;
|
|
27
|
+
if (a > b)
|
|
28
|
+
return 1;
|
|
29
|
+
return 0;
|
|
30
|
+
})
|
|
31
|
+
.map((m) => (0, util_1.createImportDecl)(m));
|
|
32
|
+
}
|
|
33
|
+
exports.createImportDecls = createImportDecls;
|
|
34
|
+
function createReExportStmts(types) {
|
|
35
|
+
return types
|
|
36
|
+
.flatMap((t) => t.exportTypes)
|
|
37
|
+
.sort()
|
|
38
|
+
.filter((0, util_1.onlyUnique)(({ type }) => (0, util_1.fullNameString)(type)))
|
|
39
|
+
.map(({ name, type }) => typescript_1.default.factory.createTypeAliasDeclaration(undefined, [typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.ExportKeyword)], name, undefined, typescript_1.default.factory.createTypeReferenceNode((0, util_1.createQualifiedName)(type))));
|
|
40
|
+
}
|
|
41
|
+
exports.createReExportStmts = createReExportStmts;
|
|
42
|
+
function createTypeDslStmts(types) {
|
|
43
|
+
return types
|
|
44
|
+
.flatMap((type) => {
|
|
45
|
+
if (type instanceof codegen_core_1.ObjectType) {
|
|
46
|
+
return (0, objectType_1.createObjectTypeDslStmts)(type);
|
|
47
|
+
}
|
|
48
|
+
if (type instanceof codegen_core_1.InputObjectType) {
|
|
49
|
+
return (0, inputObjectType_1.createInputObjectTypeDslStmts)(type);
|
|
50
|
+
}
|
|
51
|
+
if (type instanceof codegen_core_1.EnumType) {
|
|
52
|
+
return [(0, enumType_1.createEnumTypeDslStmt)(type)];
|
|
53
|
+
}
|
|
54
|
+
if (type instanceof codegen_core_1.OneofUnionType || type instanceof codegen_core_1.SquashedOneofUnionType) {
|
|
55
|
+
return [(0, oneofUnionType_1.createOneofUnionTypeDslStmt)(type)];
|
|
56
|
+
}
|
|
57
|
+
const _exhaustiveCheck = type;
|
|
58
|
+
throw "unreachable";
|
|
59
|
+
})
|
|
60
|
+
.filter((0, util_1.onlyNonNull)());
|
|
61
|
+
}
|
|
62
|
+
exports.createTypeDslStmts = createTypeDslStmts;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createInputObjectTypeDslStmts = void 0;
|
|
7
|
+
const codegen_core_1 = require("@proto-graphql/codegen-core");
|
|
8
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
9
|
+
const field_1 = require("./field");
|
|
10
|
+
const util_1 = require("./util");
|
|
11
|
+
/**
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* export const HelloInput = builder.inputRef<Omit<_$hello$hello_pb.Hello, "$type">>("HelloInput");
|
|
15
|
+
* HelloInput.implement({
|
|
16
|
+
* description: "...",
|
|
17
|
+
* fields: (t) => ({
|
|
18
|
+
* // ...
|
|
19
|
+
* }),
|
|
20
|
+
* })
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
function createInputObjectTypeDslStmts(type) {
|
|
24
|
+
return [
|
|
25
|
+
createInputObjectTypeShapeDecl(type),
|
|
26
|
+
(0, util_1.createDslExportConstStmt)(type.pothosRefObjectName, typescript_1.default.factory.createCallExpression((0, util_1.createBuilderPropExpr)("inputRef"), [typescript_1.default.factory.createTypeReferenceNode(createInputObjectTypeShapeIdent(type))], [typescript_1.default.factory.createStringLiteral(type.typeName)])),
|
|
27
|
+
typescript_1.default.factory.createExpressionStatement(typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier(type.pothosRefObjectName), "implement"), undefined, [
|
|
28
|
+
typescript_1.default.factory.createObjectLiteralExpression([
|
|
29
|
+
(0, util_1.createDescriptionPropertyAssignment)(type),
|
|
30
|
+
typescript_1.default.factory.createPropertyAssignment("fields", createInputObjectTypeFieldsMethodExpr(type)),
|
|
31
|
+
// ts.factory.createPropertyAssignment("extensions", createExtensionsObjectLiteralExpr(type)),
|
|
32
|
+
].filter((0, util_1.onlyNonNull)()), true),
|
|
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
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (field.isNullable()) {
|
|
69
|
+
typeNode = typescript_1.default.factory.createUnionTypeNode([
|
|
70
|
+
typeNode,
|
|
71
|
+
typescript_1.default.factory.createLiteralTypeNode(typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.NullKeyword)),
|
|
72
|
+
]);
|
|
73
|
+
}
|
|
74
|
+
return typescript_1.default.factory.createPropertySignature(undefined, field.name, field.isNullable() ? typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.QuestionToken) : undefined, typeNode);
|
|
75
|
+
})));
|
|
76
|
+
}
|
|
77
|
+
function createInputObjectTypeShapeIdent(type) {
|
|
78
|
+
return typescript_1.default.factory.createIdentifier(`${type.typeName}$Shape`);
|
|
79
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createObjectTypeDslStmts = void 0;
|
|
7
|
+
const codegen_core_1 = require("@proto-graphql/codegen-core");
|
|
8
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
9
|
+
const field_1 = require("./field");
|
|
10
|
+
const util_1 = require("./util");
|
|
11
|
+
/**
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* export const Hello = builder.objectRef<_$hello$hello_pb.Hello>("Hello")
|
|
15
|
+
* builder.objectType(Hello, {
|
|
16
|
+
* name: "Hello",
|
|
17
|
+
* // ...
|
|
18
|
+
* })
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
function createObjectTypeDslStmts(objType) {
|
|
22
|
+
const isInterface = objType instanceof codegen_core_1.InterfaceType;
|
|
23
|
+
return [
|
|
24
|
+
(0, util_1.createDslExportConstStmt)(objType.pothosRefObjectName, typescript_1.default.factory.createCallExpression((0, util_1.createBuilderPropExpr)(isInterface ? "interfaceRef" : "objectRef"), [
|
|
25
|
+
isInterface
|
|
26
|
+
? typescript_1.default.factory.createTypeReferenceNode("Pick", [
|
|
27
|
+
typescript_1.default.factory.createTypeReferenceNode((0, util_1.createQualifiedName)(objType.protoTypeFullName)),
|
|
28
|
+
typescript_1.default.factory.createUnionTypeNode(objType.fields.map((f) => typescript_1.default.factory.createLiteralTypeNode(typescript_1.default.factory.createStringLiteral(f.protoJsName)))),
|
|
29
|
+
])
|
|
30
|
+
: typescript_1.default.factory.createTypeReferenceNode((0, util_1.createQualifiedName)(objType.protoTypeFullName)),
|
|
31
|
+
], [typescript_1.default.factory.createStringLiteral(objType.typeName)])),
|
|
32
|
+
typescript_1.default.factory.createExpressionStatement((0, util_1.createBuilderCallExpr)(isInterface ? "interfaceType" : "objectType", [
|
|
33
|
+
typescript_1.default.factory.createIdentifier(objType.pothosRefObjectName),
|
|
34
|
+
typescript_1.default.factory.createObjectLiteralExpression([
|
|
35
|
+
typescript_1.default.factory.createPropertyAssignment("name", typescript_1.default.factory.createStringLiteral(objType.typeName)),
|
|
36
|
+
(0, util_1.createDescriptionPropertyAssignment)(objType),
|
|
37
|
+
typescript_1.default.factory.createPropertyAssignment("fields", createObjectTypeFieldsFuncExpr(objType)),
|
|
38
|
+
isInterface ? null : typescript_1.default.factory.createPropertyAssignment("isTypeOf", createIsTypeOfMethodExpr(objType)),
|
|
39
|
+
].filter((0, util_1.onlyNonNull)()), true),
|
|
40
|
+
])),
|
|
41
|
+
];
|
|
42
|
+
}
|
|
43
|
+
exports.createObjectTypeDslStmts = createObjectTypeDslStmts;
|
|
44
|
+
/**
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* fields: (t) => ({
|
|
48
|
+
* // ...
|
|
49
|
+
* })
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
function createObjectTypeFieldsFuncExpr(objType) {
|
|
53
|
+
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
|
|
54
|
+
? objType.fields.map((f) => typescript_1.default.factory.createPropertyAssignment(f.name, (0, field_1.createFieldDefinitionExpr)(f)))
|
|
55
|
+
: [typescript_1.default.factory.createPropertyAssignment("_", (0, field_1.createNoopFieldDefinitionExpr)({ input: false }))], true)));
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* isTypeOf(data) {
|
|
61
|
+
* return data instanceof _$hello$hello_pb.Hello;
|
|
62
|
+
*
|
|
63
|
+
* (source) =>
|
|
64
|
+
* // eslint-disable-next-line @typescript-eslint/ban-types
|
|
65
|
+
* (source as _$hello$hello_pb.Hello | { $type: string & {} }).$type === "hello.Hello",
|
|
66
|
+
* }
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
function createIsTypeOfMethodExpr(objType) {
|
|
70
|
+
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([
|
|
71
|
+
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([
|
|
72
|
+
typescript_1.default.factory.createTypeReferenceNode((0, util_1.createQualifiedName)(objType.protoTypeFullName)),
|
|
73
|
+
typescript_1.default.factory.createTypeLiteralNode([
|
|
74
|
+
typescript_1.default.factory.createPropertySignature(undefined, "$type", undefined, typescript_1.default.factory.createIntersectionTypeNode([
|
|
75
|
+
typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.StringKeyword),
|
|
76
|
+
typescript_1.default.factory.createTypeLiteralNode([]),
|
|
77
|
+
])),
|
|
78
|
+
]),
|
|
79
|
+
]))), "$type"), typescript_1.default.SyntaxKind.EqualsEqualsEqualsToken, typescript_1.default.factory.createStringLiteral(objType.proto.fullName.toString()))),
|
|
80
|
+
], true));
|
|
81
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createOneofUnionTypeDslStmt = void 0;
|
|
7
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
8
|
+
const util_1 = require("./util");
|
|
9
|
+
/**
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* export cosnt Oneof = builder.unionType("Oneof", {
|
|
13
|
+
* types: [...],
|
|
14
|
+
* // ...
|
|
15
|
+
* })
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
function createOneofUnionTypeDslStmt(type) {
|
|
19
|
+
return (0, util_1.createDslExportConstStmt)(type.pothosRefObjectName, (0, util_1.createBuilderCallExpr)("unionType", [
|
|
20
|
+
typescript_1.default.factory.createStringLiteral(type.typeName),
|
|
21
|
+
typescript_1.default.factory.createObjectLiteralExpression([
|
|
22
|
+
(0, util_1.createDescriptionPropertyAssignment)(type),
|
|
23
|
+
typescript_1.default.factory.createPropertyAssignment("types", typescript_1.default.factory.createArrayLiteralExpression(type.fields.map((f) => typescript_1.default.factory.createIdentifier(f.type.pothosRefObjectName)), true)),
|
|
24
|
+
].filter((0, util_1.onlyNonNull)()), true),
|
|
25
|
+
]));
|
|
26
|
+
}
|
|
27
|
+
exports.createOneofUnionTypeDslStmt = createOneofUnionTypeDslStmt;
|