Skip to content

Commit

Permalink
generate "full paths" for custom types (#94)
Browse files Browse the repository at this point in the history
* generate schema.ts and index.ts to make it easier for clients to start

* run `tsent codegen` on example and confirm that everything is kosher

fix 2 bugs:
1/ multiple reserveImport of the same path breaking e.g. in User module where multiple things referencing Contact
2/ delete action inputName panicking per #85

* generate "full paths" for custom types so we end with consistent types and can merge paths when importing happens across locaitons
  • Loading branch information
lolopinto committed Aug 6, 2020
1 parent d1e3c4f commit 453c12f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
8 changes: 6 additions & 2 deletions internal/graphql/generate_ts_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ func getImportPathForNode(nodeData *schema.NodeData) string {
return fmt.Sprintf("src/graphql/resolvers/generated/%s_type", nodeData.PackageName)
}

func getImportPathFromNodePackage(packageName string) string {
return fmt.Sprintf("src/graphql/resolvers/generated/%s_type", packageName)
}

func getQueryFilePath() string {
return fmt.Sprintf("src/graphql/resolvers/generated/query_type.ts")
}
Expand Down Expand Up @@ -606,7 +610,7 @@ func buildNodeForObject(nodeMap schema.NodeMapInfo, nodeData *schema.NodeData) *
continue
}
result.Imports = append(result.Imports, &fileImport{
ImportPath: fmt.Sprintf("./%s_type", node.PackageName),
ImportPath: getImportPathFromNodePackage(node.PackageName),
Type: fmt.Sprintf("%sType", node.Node),
})
}
Expand Down Expand Up @@ -1053,7 +1057,7 @@ func getQueryData(data *codegen.Data, s *gqlSchema) []rootField {
continue
}
results = append(results, rootField{
ImportPath: fmt.Sprintf("./%s_type", nodeData.PackageName),
ImportPath: getImportPathForNode(nodeData),
Type: fmt.Sprintf("%sQuery", nodeData.Node),
Name: strcase.ToLowerCamel(nodeData.Node),
})
Expand Down
18 changes: 18 additions & 0 deletions internal/tsimport/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,24 @@ func TestImports(t *testing.T) {
getImportLine("import {loadEnt, ID} from {path};", "ent/ent"),
},
},
"reserve exact same thing": {
fn: func(imps *tsimport.Imports) {
reserveDefaultImport(imps, "src/ent/user", "User")
reserveDefaultImport(imps, "src/ent/user", "User")

useImport(imps, "User")
},
expectedLines: []string{
getImportLine("import User from {path};", "src/ent/user"),
},
},
"reserve different paths": {
fn: func(imps *tsimport.Imports) {
reserveDefaultImport(imps, "src/ent/user", "User")
reserveDefaultImport(imps, "/user", "User")
},
panicInFn: true,
},
}

for key, tt := range testCases {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
GraphQLResolveInfo,
} from "graphql";
import { ID, RequestContext } from "@lolopinto/ent";
import { UserType } from "./user_type";
import { UserType } from "src/graphql/resolvers/generated/user_type";
import Contact from "src/ent/contact";

interface ContactQueryArgs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from "graphql";
import { ID, RequestContext } from "@lolopinto/ent";
import { GraphQLTime } from "@lolopinto/ent/graphql";
import { UserType } from "./user_type";
import { UserType } from "src/graphql/resolvers/generated/user_type";
import Event from "src/ent/event";

interface EventQueryArgs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import { GraphQLObjectType } from "graphql";

import { AddressQuery } from "./address_type";
import { ContactQuery } from "./contact_type";
import { EventQuery } from "./event_type";
import { UserQuery } from "./user_type";
import { AddressQuery } from "src/graphql/resolvers/generated/address_type";
import { ContactQuery } from "src/graphql/resolvers/generated/contact_type";
import { EventQuery } from "src/graphql/resolvers/generated/event_type";
import { UserQuery } from "src/graphql/resolvers/generated/user_type";
import { ViewerType } from "src/graphql/resolvers/generated/viewer_type";

export const QueryType = new GraphQLObjectType({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
GraphQLResolveInfo,
} from "graphql";
import { ID, RequestContext } from "@lolopinto/ent";
import { EventType } from "./event_type";
import { ContactType } from "./contact_type";
import { EventType } from "src/graphql/resolvers/generated/event_type";
import { ContactType } from "src/graphql/resolvers/generated/contact_type";
import User from "src/ent/user";

interface UserQueryArgs {
Expand Down

0 comments on commit 453c12f

Please sign in to comment.