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
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# protoc-gen-pothos
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#239](https://github.com/proto-graphql/proto-graphql-js/pull/239) [`f072608`](https://github.com/proto-graphql/proto-graphql-js/commit/f0726082c9ad728ff5ad648e79ab7862f85a1a97) Thanks [@izumin5210](https://github.com/izumin5210)! - rewrite protoc-gen-pothos printer with ts-poet
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- [#243](https://github.com/proto-graphql/proto-graphql-js/pull/243) [`3aa729a`](https://github.com/proto-graphql/proto-graphql-js/commit/3aa729a62a36aa782e8c2153bb3b789e9551ae00) Thanks [@izumin5210](https://github.com/izumin5210)! - remove unused code from codegen-core
|
|
12
|
+
|
|
13
|
+
- [#241](https://github.com/proto-graphql/proto-graphql-js/pull/241) [`cef3d3e`](https://github.com/proto-graphql/proto-graphql-js/commit/cef3d3e512e616f98869c12060d592c3ff887aa9) Thanks [@izumin5210](https://github.com/izumin5210)! - rewrite protoc-gen-nexus printer with ts-poet
|
|
14
|
+
|
|
15
|
+
- Updated dependencies [[`3aa729a`](https://github.com/proto-graphql/proto-graphql-js/commit/3aa729a62a36aa782e8c2153bb3b789e9551ae00), [`cef3d3e`](https://github.com/proto-graphql/proto-graphql-js/commit/cef3d3e512e616f98869c12060d592c3ff887aa9), [`f072608`](https://github.com/proto-graphql/proto-graphql-js/commit/f0726082c9ad728ff5ad648e79ab7862f85a1a97)]:
|
|
16
|
+
- @proto-graphql/codegen-core@0.3.0
|
|
17
|
+
- @proto-graphql/protoc-plugin-helpers@0.2.2
|
|
18
|
+
- @proto-graphql/proto-descriptors@0.3.0
|
|
19
|
+
|
|
20
|
+
## 0.2.3
|
|
21
|
+
|
|
22
|
+
### Patch Changes
|
|
23
|
+
|
|
24
|
+
- [#237](https://github.com/proto-graphql/proto-graphql-js/pull/237) [`b9945c8`](https://github.com/proto-graphql/proto-graphql-js/commit/b9945c827e737bda8b85621966a90a2e7e5cdf41) Thanks [@izumin5210](https://github.com/izumin5210)! - tweak input type definition for avoiding type error
|
|
25
|
+
|
|
26
|
+
- Updated dependencies [[`b9945c8`](https://github.com/proto-graphql/proto-graphql-js/commit/b9945c827e737bda8b85621966a90a2e7e5cdf41)]:
|
|
27
|
+
- @proto-graphql/codegen-core@0.2.3
|
|
28
|
+
|
|
3
29
|
## 0.2.2
|
|
4
30
|
|
|
5
31
|
### Patch Changes
|
|
@@ -1,75 +1,40 @@
|
|
|
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.
|
|
7
|
-
const
|
|
3
|
+
exports.createEnumTypeCode = void 0;
|
|
4
|
+
const codegen_core_1 = require("@proto-graphql/codegen-core");
|
|
5
|
+
const ts_poet_1 = require("ts-poet");
|
|
8
6
|
const util_1 = require("./util");
|
|
9
7
|
/**
|
|
10
8
|
* @example
|
|
11
9
|
* ```ts
|
|
12
|
-
* export cosnt Hello = builder.enumType("Hello", {
|
|
10
|
+
* export cosnt Hello$Ref = builder.enumType("Hello", {
|
|
11
|
+
* values: [
|
|
12
|
+
* // ...
|
|
13
|
+
* ],
|
|
13
14
|
* // ...
|
|
14
15
|
* })
|
|
15
16
|
* ```
|
|
16
17
|
*/
|
|
17
|
-
function
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* @example
|
|
42
|
-
* ```ts
|
|
43
|
-
* {{
|
|
44
|
-
* protobufEnum: {
|
|
45
|
-
* name: "...",
|
|
46
|
-
* fullName: "...",
|
|
47
|
-
* package: "...",
|
|
48
|
-
* },
|
|
49
|
-
* } as ProtobufEnumExtensions)
|
|
50
|
-
* ```
|
|
51
|
-
*/
|
|
52
|
-
function createExtensionsObjectLiteralExpr(type) {
|
|
53
|
-
return typescript_1.default.factory.createObjectLiteralExpression([
|
|
54
|
-
typescript_1.default.factory.createPropertyAssignment("protobufEnum", typescript_1.default.factory.createObjectLiteralExpression([
|
|
55
|
-
typescript_1.default.factory.createPropertyAssignment("name", typescript_1.default.factory.createStringLiteral(type.proto.name)),
|
|
56
|
-
typescript_1.default.factory.createPropertyAssignment("fullName", typescript_1.default.factory.createStringLiteral(type.proto.fullName.toString())),
|
|
57
|
-
typescript_1.default.factory.createPropertyAssignment("package", typescript_1.default.factory.createStringLiteral(type.proto.file.package)),
|
|
58
|
-
], true)),
|
|
59
|
-
], true);
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* @example
|
|
63
|
-
* ```ts
|
|
64
|
-
* ({
|
|
65
|
-
* protobufEnumValue: {
|
|
66
|
-
* name: "...",
|
|
67
|
-
* },
|
|
68
|
-
* } as ProtobufEnumValueExtensions)
|
|
69
|
-
* ```
|
|
70
|
-
*/
|
|
71
|
-
function createExtensionsObjectLiteralExprForEnumValue(ev) {
|
|
72
|
-
return typescript_1.default.factory.createObjectLiteralExpression([
|
|
73
|
-
typescript_1.default.factory.createPropertyAssignment("protobufEnumValue", typescript_1.default.factory.createObjectLiteralExpression([typescript_1.default.factory.createPropertyAssignment("name", typescript_1.default.factory.createStringLiteral(ev.proto.name))], true)),
|
|
74
|
-
], true);
|
|
18
|
+
function createEnumTypeCode(type, opts) {
|
|
19
|
+
const typeOpts = {
|
|
20
|
+
description: type.description,
|
|
21
|
+
values: (0, ts_poet_1.code) `{${(0, ts_poet_1.joinCode)(type.values
|
|
22
|
+
.filter((v) => !v.isIgnored() && !v.isUnespecified())
|
|
23
|
+
.map((ev) => (0, ts_poet_1.code) `${ev.name}: ${(0, ts_poet_1.literalOf)((0, codegen_core_1.compact)({
|
|
24
|
+
description: ev.description,
|
|
25
|
+
deprecationReason: ev.deprecationReason,
|
|
26
|
+
value: ev.number,
|
|
27
|
+
extensions: {
|
|
28
|
+
protobufEnumValue: {
|
|
29
|
+
name: ev.proto.name,
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
}))},`))}} as const`,
|
|
33
|
+
extensions: (0, codegen_core_1.protobufGraphQLExtensions)(type),
|
|
34
|
+
};
|
|
35
|
+
return (0, ts_poet_1.code) `
|
|
36
|
+
export const ${(0, util_1.pothosRef)(type)} =
|
|
37
|
+
${(0, util_1.pothosBuilder)(type, opts)}.enumType(${(0, ts_poet_1.literalOf)(type.typeName)}, ${(0, ts_poet_1.literalOf)((0, codegen_core_1.compact)(typeOpts))});
|
|
38
|
+
`;
|
|
75
39
|
}
|
|
40
|
+
exports.createEnumTypeCode = createEnumTypeCode;
|
|
@@ -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.createNoopFieldRefCode = exports.createFieldRefCode = 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 enumFieldResolver_1 = require("./fieldResolver/enumFieldResolver");
|
|
10
7
|
const nonNullResolver_1 = require("./fieldResolver/nonNullResolver");
|
|
11
8
|
const oneofUnionResolver_1 = require("./fieldResolver/oneofUnionResolver");
|
|
@@ -30,55 +27,47 @@ const util_1 = require("./util");
|
|
|
30
27
|
* })
|
|
31
28
|
* ```
|
|
32
29
|
*/
|
|
33
|
-
function
|
|
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);
|
|
30
|
+
function createFieldRefCode(field, opts) {
|
|
37
31
|
const isInput = field instanceof codegen_core_1.InputObjectField;
|
|
38
|
-
const
|
|
39
|
-
const
|
|
40
|
-
let
|
|
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;
|
|
32
|
+
const baseType = field.type instanceof codegen_core_1.ScalarType ? (0, ts_poet_1.literalOf)(field.type.typeName) : (0, util_1.fieldTypeRef)(field, opts);
|
|
33
|
+
const sourceExpr = (0, ts_poet_1.code) `source`;
|
|
34
|
+
let resolverCode;
|
|
53
35
|
if (!isInput) {
|
|
54
|
-
const nullableInProto = field.type instanceof codegen_core_1.ObjectType ||
|
|
55
|
-
(field.type instanceof codegen_core_1.ScalarType && !field.type.isPrimitive() && !field.type.isWrapperType());
|
|
56
|
-
if (nullableInProto && !field.isNullable()) {
|
|
57
|
-
createResolveStmts = (sourceExpr) => (0, nonNullResolver_1.createNonNullResolverStmts)(typescript_1.default.factory.createPropertyAccessExpression(sourceExpr, field.protoJsName));
|
|
58
|
-
}
|
|
59
|
-
if (field.type instanceof codegen_core_1.EnumType && field instanceof codegen_core_1.ObjectField) {
|
|
60
|
-
createResolveStmts = (sourceExpr) => (0, enumFieldResolver_1.createEnumResolverStmts)(typescript_1.default.factory.createPropertyAccessExpression(sourceExpr, field.protoJsName), field);
|
|
61
|
-
}
|
|
62
36
|
if (field instanceof codegen_core_1.ObjectOneofField) {
|
|
63
|
-
|
|
37
|
+
resolverCode = (0, oneofUnionResolver_1.createOneofUnionResolverCode)(sourceExpr, field);
|
|
64
38
|
}
|
|
65
|
-
|
|
66
|
-
|
|
39
|
+
else {
|
|
40
|
+
const valueExpr = (0, codegen_core_1.createGetFieldValueCode)(sourceExpr, field.proto, opts);
|
|
41
|
+
const nullableInProto = field.type instanceof codegen_core_1.ObjectType ||
|
|
42
|
+
(field.type instanceof codegen_core_1.ScalarType && !field.type.isPrimitive() && !field.type.isWrapperType());
|
|
43
|
+
if (nullableInProto && !field.isNullable()) {
|
|
44
|
+
resolverCode = (0, nonNullResolver_1.createNonNullResolverCode)(valueExpr);
|
|
45
|
+
}
|
|
46
|
+
if (field.type instanceof codegen_core_1.EnumType && field instanceof codegen_core_1.ObjectField) {
|
|
47
|
+
resolverCode = (0, enumFieldResolver_1.createEnumResolverCode)(valueExpr, field, opts);
|
|
48
|
+
}
|
|
49
|
+
if (field.type instanceof codegen_core_1.SquashedOneofUnionType && field instanceof codegen_core_1.ObjectField) {
|
|
50
|
+
resolverCode = (0, oneofUnionResolver_1.createOneofUnionResolverCode)(valueExpr, field);
|
|
51
|
+
}
|
|
67
52
|
}
|
|
68
53
|
}
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
54
|
+
const nullableValue = isInput !== field.isNullable(); /* Logical XOR */
|
|
55
|
+
const fieldOpts = {
|
|
56
|
+
type: field.isList() ? (0, ts_poet_1.code) `[${baseType}]` : baseType,
|
|
57
|
+
[isInput ? "required" : "nullable"]: field.isList()
|
|
58
|
+
? { list: nullableValue, items: isInput /* always non-null */ }
|
|
59
|
+
: nullableValue,
|
|
60
|
+
description: field.description,
|
|
61
|
+
deprecationReason: field.deprecationReason,
|
|
62
|
+
resolve: resolverCode ? (0, ts_poet_1.code) `${sourceExpr} => {${resolverCode}}` : null,
|
|
63
|
+
extensions: (0, codegen_core_1.protobufGraphQLExtensions)(field),
|
|
64
|
+
};
|
|
65
|
+
const shouldUseFieldFunc = isInput || resolverCode != null;
|
|
66
|
+
return shouldUseFieldFunc
|
|
67
|
+
? (0, ts_poet_1.code) `t.field(${(0, ts_poet_1.literalOf)((0, codegen_core_1.compact)(fieldOpts))})`
|
|
68
|
+
: (0, ts_poet_1.code) `t.expose(${(0, ts_poet_1.literalOf)(field.proto.jsonName)}, ${(0, ts_poet_1.literalOf)((0, codegen_core_1.compact)(fieldOpts))})`;
|
|
80
69
|
}
|
|
81
|
-
exports.
|
|
70
|
+
exports.createFieldRefCode = createFieldRefCode;
|
|
82
71
|
/**
|
|
83
72
|
* @example
|
|
84
73
|
* ```ts
|
|
@@ -92,52 +81,14 @@ exports.createFieldDefinitionExpr = createFieldDefinitionExpr;
|
|
|
92
81
|
* })
|
|
93
82
|
* ```
|
|
94
83
|
*/
|
|
95
|
-
function
|
|
96
|
-
return
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
? null
|
|
105
|
-
: typescript_1.default.factory.createMethodDeclaration(undefined, undefined, undefined, "resolve", undefined, undefined, [], undefined, typescript_1.default.factory.createBlock([
|
|
106
|
-
typescript_1.default.factory.createReturnStatement(typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.TrueKeyword)),
|
|
107
|
-
])),
|
|
108
|
-
].filter((0, util_1.onlyNonNull)()), true),
|
|
109
|
-
]);
|
|
110
|
-
}
|
|
111
|
-
exports.createNoopFieldDefinitionExpr = createNoopFieldDefinitionExpr;
|
|
112
|
-
function createResolverExpr(createStmts) {
|
|
113
|
-
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));
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* @example
|
|
117
|
-
* ```ts
|
|
118
|
-
* ({
|
|
119
|
-
* protobufField: {
|
|
120
|
-
* name: "...",
|
|
121
|
-
* },
|
|
122
|
-
* } as ProtobufFieldExtensions)
|
|
123
|
-
* ```
|
|
124
|
-
*/
|
|
125
|
-
function createExtensionsObjectLiteralExpr(field) {
|
|
126
|
-
let typeName;
|
|
127
|
-
if ((field instanceof codegen_core_1.ObjectField || field instanceof codegen_core_1.InputObjectField) && field.proto.type !== null) {
|
|
128
|
-
if (field.proto.type.kind === "Scalar") {
|
|
129
|
-
typeName = field.proto.type.type;
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
typeName = field.proto.type.fullName.toString();
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
return typescript_1.default.factory.createObjectLiteralExpression([
|
|
136
|
-
typescript_1.default.factory.createPropertyAssignment("protobufField", typescript_1.default.factory.createObjectLiteralExpression([
|
|
137
|
-
typescript_1.default.factory.createPropertyAssignment("name", typescript_1.default.factory.createStringLiteral(field.proto.name)),
|
|
138
|
-
typeName
|
|
139
|
-
? typescript_1.default.factory.createPropertyAssignment("typeFullName", typescript_1.default.factory.createStringLiteral(typeName))
|
|
140
|
-
: undefined,
|
|
141
|
-
].filter((0, util_1.onlyNonNull)()), true)),
|
|
142
|
-
], true);
|
|
84
|
+
function createNoopFieldRefCode(opts) {
|
|
85
|
+
return (0, ts_poet_1.code) `
|
|
86
|
+
t.field({
|
|
87
|
+
type: "Boolean",
|
|
88
|
+
${opts.input ? "required: false" : "nullable: true"},
|
|
89
|
+
description: "noop field",
|
|
90
|
+
${opts.input ? "" : "resolve: () => true,"}
|
|
91
|
+
})
|
|
92
|
+
`;
|
|
143
93
|
}
|
|
94
|
+
exports.createNoopFieldRefCode = createNoopFieldRefCode;
|
|
@@ -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.
|
|
7
|
-
const
|
|
8
|
-
const
|
|
3
|
+
exports.createEnumResolverCode = void 0;
|
|
4
|
+
const codegen_core_1 = require("@proto-graphql/codegen-core");
|
|
5
|
+
const ts_poet_1 = require("ts-poet");
|
|
9
6
|
/**
|
|
10
7
|
* @example nullable
|
|
11
8
|
* ```ts
|
|
@@ -22,47 +19,43 @@ const util_1 = require("../util");
|
|
|
22
19
|
* return root.myEnum
|
|
23
20
|
* ```
|
|
24
21
|
*/
|
|
25
|
-
function
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
22
|
+
function createEnumResolverCode(valueExpr, field, opts) {
|
|
23
|
+
const createBlockStmtCodes = (valueExpr) => {
|
|
24
|
+
const chunks = [];
|
|
25
|
+
if (field.type.unspecifiedValue != null) {
|
|
26
|
+
const escapeCode = field.isNullable() && !field.isList()
|
|
27
|
+
? (0, ts_poet_1.code) `return null;`
|
|
28
|
+
: (0, ts_poet_1.code) `throw new Error("${field.name} is required field. But got unspecified.");`;
|
|
29
|
+
chunks.push((0, ts_poet_1.code) `
|
|
30
|
+
if (${valueExpr} === ${(0, codegen_core_1.protoType)(field.type.proto, opts)}.${field.type.unspecifiedValue.proto.name}) {
|
|
31
|
+
${escapeCode}
|
|
32
|
+
}
|
|
33
|
+
`);
|
|
34
|
+
}
|
|
35
|
+
for (const ev of field.type.valuesWithIgnored) {
|
|
36
|
+
if (!ev.isIgnored())
|
|
37
|
+
continue;
|
|
38
|
+
chunks.push((0, ts_poet_1.code) `
|
|
39
|
+
if (${valueExpr} === ${(0, codegen_core_1.protoType)(field.type.proto, opts)}.${ev.proto.name}) {
|
|
40
|
+
throw new Error("${ev.name} is ignored in GraphQL schema");
|
|
41
|
+
}
|
|
42
|
+
`);
|
|
43
|
+
}
|
|
44
|
+
return chunks;
|
|
45
|
+
};
|
|
33
46
|
if (field.isList()) {
|
|
34
|
-
const
|
|
35
|
-
if (
|
|
36
|
-
return
|
|
47
|
+
const stmts = createBlockStmtCodes((0, ts_poet_1.code) `item`);
|
|
48
|
+
if (stmts.length === 0) {
|
|
49
|
+
return (0, ts_poet_1.code) `return ${valueExpr}`;
|
|
37
50
|
}
|
|
38
|
-
return
|
|
39
|
-
|
|
40
|
-
|
|
51
|
+
return (0, ts_poet_1.code) `return ${valueExpr}.map(item => {
|
|
52
|
+
${stmts}
|
|
53
|
+
return item;
|
|
54
|
+
})`;
|
|
41
55
|
}
|
|
42
|
-
return
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
]);
|
|
56
|
+
return (0, ts_poet_1.code) `
|
|
57
|
+
${createBlockStmtCodes(valueExpr)}
|
|
58
|
+
return ${valueExpr};
|
|
59
|
+
`;
|
|
68
60
|
}
|
|
61
|
+
exports.createEnumResolverCode = createEnumResolverCode;
|
|
@@ -1,11 +1,10 @@
|
|
|
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.
|
|
7
|
-
const
|
|
8
|
-
function
|
|
9
|
-
return
|
|
3
|
+
exports.createNonNullResolverCode = void 0;
|
|
4
|
+
const ts_poet_1 = require("ts-poet");
|
|
5
|
+
function createNonNullResolverCode(valueExpr) {
|
|
6
|
+
return (0, ts_poet_1.code) `
|
|
7
|
+
return ${valueExpr}!;
|
|
8
|
+
`;
|
|
10
9
|
}
|
|
11
|
-
exports.
|
|
10
|
+
exports.createNonNullResolverCode = createNonNullResolverCode;
|
|
@@ -1,12 +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.createOneofUnionResolverCode = void 0;
|
|
7
4
|
const codegen_core_1 = require("@proto-graphql/codegen-core");
|
|
8
|
-
const
|
|
9
|
-
const util_1 = require("../util");
|
|
5
|
+
const ts_poet_1 = require("ts-poet");
|
|
10
6
|
/**
|
|
11
7
|
* @example nullable
|
|
12
8
|
* ```ts
|
|
@@ -17,45 +13,29 @@ const util_1 = require("../util");
|
|
|
17
13
|
* return value
|
|
18
14
|
* ```
|
|
19
15
|
*/
|
|
20
|
-
function
|
|
21
|
-
const
|
|
16
|
+
function createOneofUnionResolverCode(sourceExpr, field) {
|
|
17
|
+
const createBlockStmtCode = (sourceExpr, { nullable }) => {
|
|
22
18
|
const createFieldExpr = (memberField) => {
|
|
23
19
|
if (field instanceof codegen_core_1.ObjectOneofField) {
|
|
24
|
-
return
|
|
20
|
+
return (0, ts_poet_1.code) `${sourceExpr}.${memberField.proto.jsonName}`;
|
|
25
21
|
}
|
|
26
|
-
return
|
|
22
|
+
return (0, ts_poet_1.code) `${sourceExpr}?.${memberField.proto.jsonName}`;
|
|
27
23
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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)());
|
|
24
|
+
return (0, ts_poet_1.code) `
|
|
25
|
+
const value = ${(0, ts_poet_1.joinCode)(field.type.fields.map(createFieldExpr), { on: "??" })};
|
|
26
|
+
if (value == null) {
|
|
27
|
+
${nullable ? "return null" : `throw new Error("${field.name} should not be null")`};
|
|
28
|
+
}
|
|
29
|
+
return value;
|
|
30
|
+
`;
|
|
49
31
|
};
|
|
50
|
-
if (
|
|
51
|
-
return
|
|
32
|
+
if (field instanceof codegen_core_1.ObjectField && field.isList()) {
|
|
33
|
+
return (0, ts_poet_1.code) `
|
|
34
|
+
return ${sourceExpr}.map(item => {
|
|
35
|
+
${createBlockStmtCode((0, ts_poet_1.code) `item`, { nullable: false })}
|
|
36
|
+
})
|
|
37
|
+
`;
|
|
52
38
|
}
|
|
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
|
-
];
|
|
39
|
+
return createBlockStmtCode(sourceExpr, { nullable: field.isNullable() });
|
|
60
40
|
}
|
|
61
|
-
exports.
|
|
41
|
+
exports.createOneofUnionResolverCode = createOneofUnionResolverCode;
|
|
@@ -1,62 +1,27 @@
|
|
|
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.createTypeDslCodes = void 0;
|
|
7
4
|
const codegen_core_1 = require("@proto-graphql/codegen-core");
|
|
8
|
-
const typescript_1 = __importDefault(require("typescript"));
|
|
9
5
|
const enumType_1 = require("./enumType");
|
|
10
6
|
const inputObjectType_1 = require("./inputObjectType");
|
|
11
7
|
const objectType_1 = require("./objectType");
|
|
12
8
|
const oneofUnionType_1 = require("./oneofUnionType");
|
|
13
|
-
|
|
14
|
-
|
|
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) => {
|
|
9
|
+
function createTypeDslCodes(types, opts) {
|
|
10
|
+
return types.flatMap((type) => {
|
|
45
11
|
if (type instanceof codegen_core_1.ObjectType) {
|
|
46
|
-
return (0, objectType_1.
|
|
12
|
+
return (0, objectType_1.createObjectTypeCode)(type, opts);
|
|
47
13
|
}
|
|
48
14
|
if (type instanceof codegen_core_1.InputObjectType) {
|
|
49
|
-
return (0, inputObjectType_1.
|
|
15
|
+
return (0, inputObjectType_1.createInputObjectTypeCode)(type, opts);
|
|
50
16
|
}
|
|
51
17
|
if (type instanceof codegen_core_1.EnumType) {
|
|
52
|
-
return [(0, enumType_1.
|
|
18
|
+
return [(0, enumType_1.createEnumTypeCode)(type, opts)];
|
|
53
19
|
}
|
|
54
20
|
if (type instanceof codegen_core_1.OneofUnionType || type instanceof codegen_core_1.SquashedOneofUnionType) {
|
|
55
|
-
return [(0, oneofUnionType_1.
|
|
21
|
+
return [(0, oneofUnionType_1.createOneofUnionTypeCode)(type, opts)];
|
|
56
22
|
}
|
|
57
23
|
const _exhaustiveCheck = type;
|
|
58
24
|
throw "unreachable";
|
|
59
|
-
})
|
|
60
|
-
.filter((0, util_1.onlyNonNull)());
|
|
25
|
+
});
|
|
61
26
|
}
|
|
62
|
-
exports.
|
|
27
|
+
exports.createTypeDslCodes = createTypeDslCodes;
|