Skip to content

Commit 4817b73

Browse files
authored
Merge pull request #243 from proto-graphql/izumin5210/refactor-core
Remove unused code from codegen-core
2 parents 84a24f9 + 15f1f3e commit 4817b73

File tree

37 files changed

+295
-866
lines changed

37 files changed

+295
-866
lines changed

.changeset/lovely-rings-approve.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@proto-graphql/codegen-core": patch
3+
"@proto-graphql/protoc-plugin-helpers": patch
4+
"@proto-graphql/proto-descriptors": patch
5+
"protoc-gen-nexus": patch
6+
"protoc-gen-pothos": patch
7+
---
8+
9+
remove unused code from codegen-core
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./util";
22
export * from "./graphqlExtensions";
33
export * from "./print";
4+
export * from "./options";
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export const fileLayouts = ["proto_file", "graphql_type"] as const;
2+
type FileLayout = typeof fileLayouts[number];
3+
4+
type PrinterDSLOptions =
5+
| { dsl: "nexus" }
6+
| {
7+
dsl: "pothos";
8+
pothos: { builderPath: string };
9+
};
10+
11+
type PrinterProtobufOptions = { protobuf: "google-protobuf" } | { protobuf: "protobufjs" } | { protobuf: "ts-proto" };
12+
13+
export type PrinterCommonOptions = {
14+
importPrefix: string | null;
15+
fileLayout: FileLayout;
16+
filenameSuffix: string;
17+
};
18+
19+
export type PrinterOptions = PrinterCommonOptions & PrinterDSLOptions & PrinterProtobufOptions;

packages/@proto-graphql/codegen-core/src/printer/util.ts

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { ProtoEnum, ProtoField, ProtoMessage, ProtoScalarType } from "@proto-graphql/proto-descriptors";
1+
import {
2+
ProtoEnum,
3+
ProtoField,
4+
ProtoFile,
5+
ProtoMessage,
6+
ProtoScalar,
7+
ProtoScalarType,
8+
} from "@proto-graphql/proto-descriptors";
29
import { camelCase } from "change-case";
310
import * as path from "path";
411
import { code, Code, imp } from "ts-poet";
@@ -11,20 +18,19 @@ import {
1118
ObjectOneofField,
1219
ObjectType,
1320
OneofUnionType,
14-
PrinterOptions,
15-
protoImportPath,
1621
SquashedOneofUnionType,
1722
} from "../types";
23+
import { PrinterOptions } from "./options";
1824

1925
export function filename(
2026
type: ObjectType | InputObjectType | EnumType | OneofUnionType | SquashedOneofUnionType | InterfaceType,
21-
opts: Pick<PrinterOptions, "fileLayout">
27+
opts: Pick<PrinterOptions, "dsl" | "fileLayout" | "filenameSuffix">
2228
): string {
2329
switch (opts.fileLayout) {
2430
case "proto_file":
25-
return type.file.filename;
31+
return filenameFromProtoFile(type.proto.file, opts);
2632
case "graphql_type": {
27-
return path.join(path.dirname(type.file.filename), `${type.typeName}${type.file.extname}`);
33+
return path.join(path.dirname(type.proto.file.name), `${type.typeName}.${opts.dsl}.ts`);
2834
}
2935
/* istanbul ignore next */
3036
default: {
@@ -34,6 +40,18 @@ export function filename(
3440
}
3541
}
3642

43+
export function filenameFromProtoFile(file: ProtoFile, opts: Pick<PrinterOptions, "fileLayout" | "filenameSuffix">) {
44+
switch (opts.fileLayout) {
45+
case "proto_file":
46+
return file.name.replace(/\.proto$/, opts.filenameSuffix);
47+
/* istanbul ignore next */
48+
default: {
49+
const _exhaustiveCheck: "graphql_type" = opts.fileLayout;
50+
throw "unreachable";
51+
}
52+
}
53+
}
54+
3755
export function generatedGraphQLTypeImportPath(
3856
field:
3957
| ObjectField<ObjectType | EnumType | InterfaceType | SquashedOneofUnionType>
@@ -171,6 +189,29 @@ export function isProtobufLong(proto: ProtoField): boolean {
171189
}
172190
}
173191

174-
export function isWellKnownType(proto: ProtoField["type"]): proto is ProtoMessage {
192+
export function isProtobufPrimitiveType(proto: ProtoField["type"]): proto is ProtoScalar {
193+
return proto.kind === "Scalar";
194+
}
195+
196+
export function isProtobufWrapperType(proto: ProtoField["type"]): proto is ProtoMessage {
197+
return proto.kind === "Message" && proto.file.name === "google/protobuf/wrappers.proto";
198+
}
199+
200+
export function isProtobufWellKnownType(proto: ProtoField["type"]): proto is ProtoMessage {
175201
return proto.kind === "Message" && proto.file.name.startsWith("google/protobuf/");
176202
}
203+
204+
function protoImportPath(t: ProtoMessage | ProtoEnum, o: Pick<PrinterOptions, "protobuf" | "importPrefix">) {
205+
const importPath =
206+
o.protobuf === "protobufjs"
207+
? path.dirname(t.file.name)
208+
: o.protobuf === "ts-proto"
209+
? t.file.name.slice(0, -1 * path.extname(t.file.name).length)
210+
: googleProtobufImportPath(t.file);
211+
return `${o.importPrefix ? `${o.importPrefix}/` : "./"}${importPath}`.replace(/(?<!:)\/\//, "/");
212+
}
213+
214+
function googleProtobufImportPath(file: ProtoFile): string {
215+
const { dir, name } = path.parse(file.name);
216+
return `${dir}/${name}_pb`;
217+
}

packages/@proto-graphql/codegen-core/src/types/DslFile.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

packages/@proto-graphql/codegen-core/src/types/EnumType.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
import { ProtoEnum, ProtoEnumValue } from "@proto-graphql/proto-descriptors";
22
import { constantCase } from "change-case";
33
import { TypeBase } from "./TypeBase";
4-
import {
5-
createProtoFullName,
6-
descriptionFromProto,
7-
FullName,
8-
GenerationParams,
9-
getDeprecationReason,
10-
isIgnoredField,
11-
protoImportPathOld,
12-
} from "./util";
4+
import { descriptionFromProto, getDeprecationReason, isIgnoredField } from "./util";
135

146
export class EnumType extends TypeBase<ProtoEnum> {
15-
get protoImportPath(): string {
16-
return protoImportPathOld(this.proto, this.options);
17-
}
18-
197
get unspecifiedValue(): EnumTypeValue | null {
208
return this.valuesWithIgnored.find((v) => v.isUnespecified()) ?? null;
219
}
@@ -25,12 +13,12 @@ export class EnumType extends TypeBase<ProtoEnum> {
2513
}
2614

2715
get valuesWithIgnored(): EnumTypeValue[] {
28-
return this.proto.values.map((v) => new EnumTypeValue(v, this.options));
16+
return this.proto.values.map((v) => new EnumTypeValue(v));
2917
}
3018
}
3119

3220
export class EnumTypeValue {
33-
constructor(readonly proto: ProtoEnumValue, private readonly opts: GenerationParams) {}
21+
constructor(readonly proto: ProtoEnumValue) {}
3422

3523
get name(): string {
3624
const prefix = constantCase(this.proto.parent.name);
@@ -49,10 +37,6 @@ export class EnumTypeValue {
4937
return isIgnoredField(this.proto);
5038
}
5139

52-
get fullName(): FullName {
53-
return [createProtoFullName(this.proto.parent, this.opts), this.proto.name];
54-
}
55-
5640
public isUnespecified(): boolean {
5741
return this.proto.index === 0 && this.proto.name === `${constantCase(this.proto.parent.name)}_UNSPECIFIED`;
5842
}
Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { ProtoField, ProtoOneof } from "@proto-graphql/proto-descriptors";
22
import * as extensions from "../__generated__/extensions/graphql/schema_pb";
3-
import { descriptionFromProto, FullName, GenerationParams, getDeprecationReason } from "./util";
3+
import { descriptionFromProto, getDeprecationReason } from "./util";
44

55
export abstract class FieldBase<P extends ProtoField | ProtoOneof> {
6-
constructor(readonly proto: P, protected readonly opts: GenerationParams & { dsl: "nexus" | "pothos" }) {}
6+
constructor(readonly proto: P) {}
77

88
abstract get name(): string;
9-
abstract get protoJsName(): string;
9+
public abstract isNullable(): boolean;
1010

1111
get description(): string | null {
1212
return descriptionFromProto(this.proto);
@@ -17,28 +17,11 @@ export abstract class FieldBase<P extends ProtoField | ProtoOneof> {
1717
return proto.kind === "Field" && proto.list;
1818
}
1919

20-
public isNullable(): boolean {
21-
throw "unimplemented";
22-
}
23-
2420
get deprecationReason(): string | null {
2521
return getDeprecationReason(this.proto);
2622
}
2723

28-
// FIXME: remove
29-
public isProtobufjs(): boolean {
30-
return this.opts.useProtobufjs;
31-
}
32-
3324
public isResolverSkipped(): boolean {
3425
return this.proto.descriptor.getOptions()?.getExtension(extensions.field)?.getSkipResolver() ?? false;
3526
}
36-
37-
public shouldReferenceTypeWithString(): boolean {
38-
return this.opts.fileLayout === "proto_file" || this.typeFullName == null;
39-
}
40-
41-
abstract get typeFullName(): FullName | null;
42-
abstract get importModules(): { alias: string; module: string }[];
43-
abstract shouldNullCheck(): boolean;
4427
}

packages/@proto-graphql/codegen-core/src/types/InputObjectField.ts

Lines changed: 5 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,36 @@
11
import { ProtoField } from "@proto-graphql/proto-descriptors";
2-
import { camelCase } from "change-case";
3-
import path from "path";
42
import * as extensions from "../__generated__/extensions/graphql/schema_pb";
53
import { EnumType } from "./EnumType";
64
import { FieldBase } from "./FieldBase";
75
import { InputObjectType } from "./InputObjectType";
86
import { ScalarType } from "./ScalarType";
9-
import { FullName, GenerationParams, isRequiredField, modulesWithUniqueImportAlias, uniqueImportAlias } from "./util";
7+
import { isRequiredField } from "./util";
108

119
export class InputObjectField<T extends ScalarType | EnumType | InputObjectType> extends FieldBase<ProtoField> {
12-
constructor(
13-
readonly type: T,
14-
readonly parent: InputObjectType,
15-
proto: ProtoField,
16-
opts: GenerationParams & { dsl: "nexus" | "pothos" }
17-
) {
18-
super(proto, opts);
10+
constructor(readonly type: T, readonly parent: InputObjectType, proto: ProtoField) {
11+
super(proto);
1912
}
2013

2114
get name(): string {
2215
return this.proto.descriptor.getOptions()?.getExtension(extensions.field)?.getName() || this.proto.jsonName;
2316
}
2417

25-
/**
26-
* @override
27-
*/
28-
get protoJsName(): string {
29-
if (this.opts.useProtobufjs) return camelCase(this.proto.name);
30-
return this.proto.jsonName;
31-
}
32-
3318
/**
3419
* @override
3520
*/
3621
public override isNullable() {
3722
return !isRequiredField(this.proto, "input");
3823
}
3924

40-
/**
41-
* @override
42-
*/
43-
public shouldNullCheck(): boolean {
44-
throw "unreachable";
45-
}
46-
47-
get protoSetterType(): "method" | "property" {
48-
return this.opts.useProtobufjs ? "property" : "method";
49-
}
50-
51-
get protoSetterNameForGoogleProtobuf(): string {
52-
return this.proto.googleProtobufSetterName;
53-
}
54-
55-
get importModules(): { alias: string; module: string; type: "namespace" | "named" }[] {
56-
const modulePaths = [];
57-
const modules = [];
58-
if (this.typeImportPath) {
59-
modulePaths.push(this.typeImportPath);
60-
}
61-
if (
62-
this.opts.dsl === "pothos" &&
63-
!(this.type instanceof ScalarType) &&
64-
this.type.proto.file.name !== this.parent.proto.file.name
65-
) {
66-
const file = path.relative(path.dirname(this.parent.filename), this.type.filename);
67-
const module = file.slice(0, -path.extname(file).length);
68-
modules.push({ alias: this.type.pothosRefObjectName, module, type: "named" as const });
69-
if (this.type instanceof InputObjectType) {
70-
modules.push({ alias: `${this.type.typeName}$Shape`, module, type: "named" as const });
71-
}
72-
}
73-
return [...modules, ...modulesWithUniqueImportAlias(modulePaths)];
74-
}
75-
76-
get typeFullName(): T extends ScalarType ? FullName | null : FullName {
77-
if (this.type instanceof ScalarType) {
78-
if (!this.opts.useProtobufjs) return null as any;
79-
80-
const fn = this.type.protoTypeFullName;
81-
if (!fn) return null as any;
82-
83-
let file: string;
84-
{
85-
const parentType = this.parent.protoTypeFullName;
86-
let parent = parentType[0];
87-
for (;;) {
88-
if (typeof parent[0] === "string") {
89-
file = parent[0];
90-
break;
91-
}
92-
parent = parent[0];
93-
}
94-
}
95-
96-
{
97-
let parent = fn[0];
98-
for (;;) {
99-
if (Array.isArray(parent) && typeof parent[0] === "string") {
100-
parent.splice(0, 1, file);
101-
break;
102-
}
103-
parent = parent[0];
104-
}
105-
}
106-
107-
return fn as any;
108-
}
109-
if (!this.typeImportPath) {
110-
return this.type.typeName as FullName as any;
111-
}
112-
return [uniqueImportAlias(this.typeImportPath), this.type.typeName] as FullName as any;
113-
}
114-
115-
private get typeImportPath(): string | null {
116-
const type: InputObjectType | EnumType | ScalarType = this.type;
117-
118-
if (type instanceof ScalarType) return type.importPath;
119-
if (this.parent.filename === type.filename) return null;
120-
121-
const [from, to] = [this.parent.filename, type.filename].map((f) => (path.isAbsolute(f) ? `.${path.sep}${f}` : f));
122-
const result = path.relative(path.dirname(from), to).replace(/\.ts$/, "");
123-
return result.match(/^[\.\/]/) ? result : `./${result}`;
124-
}
125-
12625
public toPartialInput(): InputObjectField<T> | PartialInputObjectField<T> {
12726
if (this.type instanceof InputObjectType) {
12827
return new PartialInputObjectField(
12928
(this.type.hasPartialInput() ? this.type.toPartialInput() : this.type) as any,
13029
this.parent,
131-
this.proto,
132-
this.opts
30+
this.proto
13331
);
13432
}
135-
return new PartialInputObjectField(this.type, this.parent, this.proto, this.opts);
33+
return new PartialInputObjectField(this.type, this.parent, this.proto);
13634
}
13735
}
13836

0 commit comments

Comments
 (0)