Skip to content

Commit

Permalink
fix polymorphic uuid struct fields
Browse files Browse the repository at this point in the history
  • Loading branch information
lolopinto committed Nov 13, 2024
1 parent 095c305 commit c6296b7
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 18 deletions.
4 changes: 4 additions & 0 deletions examples/simple/src/ent/generated/types.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 23 additions & 3 deletions examples/simple/src/graphql/generated/resolvers/attachment_type.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions examples/simple/src/graphql/generated/schema.gql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions examples/simple/src/graphql/generated/schema.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/simple/src/graphql/resolvers/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export * from "../generated/resolvers/user_super_nested_object_type";
export * from "../generated/resolvers/address_type";
export * from "../generated/resolvers/comment_type";
export * from "../generated/resolvers/contact_email_type";
export * from "../generated/resolvers/contact_info_type";
export * from "../generated/resolvers/contact_phone_number_type";
export * from "../generated/resolvers/contact_type";
export * from "../generated/resolvers/event_type";
Expand Down
13 changes: 13 additions & 0 deletions examples/simple/src/schema/__global__schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ const glo: GlobalSchema = {
date: TimestampType(),
phone_number: PhoneNumberType({ nullable: true }),
email_address: EmailType({ nullable: true }),
creator_id: UUIDType({
polymorphic: {
types: ["User"],
},
nullable: true,
}),
// doesn't seem like we support this...
// owner_ids: UUIDListType({
// polymorphic: {
// types: ["User"],
// },
// nullable: true,
// }),
},
}),
},
Expand Down
27 changes: 20 additions & 7 deletions internal/graphql/generate_ts_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -2417,16 +2417,22 @@ func addSingularEdge(edge edge.Edge, obj *objectType) error {
return obj.addField(gqlField)
}

func addSingularEdgeFromExternal(processor *codegen.Processor, edge edge.Edge, f *field.Field, obj *objectType) error {
func addSingularEdgeFromExternal(processor *codegen.Processor, edge *edge.FieldEdge, f *field.Field, obj *objectType) error {
gqlField := createFieldTypeForSingularEdge(edge)
var contents []string

tsFieldName := f.TsFieldName(processor.Config)
if f.Nullable() {
contents = append(contents, fmt.Sprintf("if (obj.%s === null || obj.%s === undefined) { return null;}", tsFieldName, tsFieldName))
}
contents = append(contents, fmt.Sprintf("return %s.load(context.getViewer(), obj.%s);", edge.GetNodeInfo().Node, tsFieldName))
gqlField.ExtraImports = append(gqlField.ExtraImports, tsimport.NewLocalEntImportPath(edge.GetNodeInfo().Node))
// TODO make this easier to do. shouldn't have to remember this
if edge.Polymorphic != nil {
contents = append(contents, fmt.Sprintf("return loadEntByType(context.getViewer(), obj.%s as unknown as NodeType, obj.%s);", edge.Polymorphic.NodeTypeField, edge.TSFieldName))
gqlField.ExtraImports = append(gqlField.ExtraImports, tsimport.NewTypesEntImportPath("NodeType"), tsimport.NewLocalEntImportPath("loadEntByType"))
} else {
contents = append(contents, fmt.Sprintf("return %s.load(context.getViewer(), obj.%s);", edge.GetNodeInfo().Node, tsFieldName))
gqlField.ExtraImports = append(gqlField.ExtraImports, tsimport.NewLocalEntImportPath(edge.GetNodeInfo().Node))
}
gqlField.FunctionContents = contents
return obj.addField(gqlField)
}
Expand All @@ -2445,7 +2451,7 @@ func addPluralEdge(edge edge.Edge, obj *objectType) error {
return obj.addField(gqlField)
}

func addPluralEdgeFromExternal(processor *codegen.Processor, edge edge.Edge, f *field.Field, obj *objectType) error {
func addPluralEdgeFromExternal(processor *codegen.Processor, edge *edge.FieldEdge, f *field.Field, obj *objectType) error {
gqlField := createFieldTypeForPluralEdge(edge)
gqlField.HasAsyncModifier = true

Expand All @@ -2454,10 +2460,17 @@ func addPluralEdgeFromExternal(processor *codegen.Processor, edge edge.Edge, f *
if f.Nullable() {
contents = append(contents, fmt.Sprintf("if (obj.%s === null || obj.%s === undefined) { return null;}", tsFieldName, tsFieldName))
}
contents = append(contents, fmt.Sprintf("const objs = await %s.loadMany(context.getViewer(), ...obj.%s);", edge.GetNodeInfo().Node, tsFieldName))
contents = append(contents, "return Array.from(objs.values());")
if edge.Polymorphic != nil {
// PS: doesn't seem like we support this. see examples/simple/src/schema/__global__schema.ts
contents = append(contents, fmt.Sprintf("const objs = await Promise.all(obj.%s.map((id, idx) => loadEntByType(context.getViewer(), obj.%s[idx] as unknown as NodeType, id));", edge.Polymorphic.NodeTypeField, edge.TSFieldName))
contents = append(contents, "return objs.filter((obj) => obj !== null);")
gqlField.ExtraImports = append(gqlField.ExtraImports, tsimport.NewTypesEntImportPath("NodeType"), tsimport.NewLocalEntImportPath("loadEntByType"))

gqlField.ExtraImports = append(gqlField.ExtraImports, tsimport.NewLocalEntImportPath(edge.GetNodeInfo().Node))
} else {
contents = append(contents, fmt.Sprintf("const objs = await %s.loadMany(context.getViewer(), ...obj.%s);", edge.GetNodeInfo().Node, tsFieldName))
contents = append(contents, "return Array.from(objs.values());")
gqlField.ExtraImports = append(gqlField.ExtraImports, tsimport.NewLocalEntImportPath(edge.GetNodeInfo().Node))
}
gqlField.FunctionContents = contents
return obj.addField(gqlField)
}
Expand Down

0 comments on commit c6296b7

Please sign in to comment.