Skip to content

Commit

Permalink
support converting value out of db into expected representation (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
lolopinto committed Jun 17, 2021
1 parent ef27dd0 commit 7e32576
Show file tree
Hide file tree
Showing 21 changed files with 187 additions and 63 deletions.
14 changes: 7 additions & 7 deletions examples/todo-sqlite/package-lock.json

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

6 changes: 3 additions & 3 deletions examples/todo-sqlite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
"@types/uuid": "^8.3.0",
"jest": "^26.6.3",
"jest-date-mock": "^1.0.8",
"jest-expect-message": "^1.0.2",
"supertest": "^6.1.3",
"ts-jest": "^26.5.6",
"jest-expect-message": "^1.0.2"
"ts-jest": "^26.5.6"
},
"dependencies": {
"@lolopinto/ent": "^0.0.96",
"@lolopinto/ent": "^0.0.97",
"@lolopinto/ent-phonenumber": "^0.0.3",
"@types/node": "^15.0.3",
"express": "^4.17.1",
Expand Down
5 changes: 3 additions & 2 deletions examples/todo-sqlite/src/ent/generated/account_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ObjectLoaderFactory,
PrivacyPolicy,
Viewer,
convertDate,
loadEnt,
loadEntViaKey,
loadEntX,
Expand Down Expand Up @@ -36,8 +37,8 @@ export class AccountBase {

constructor(public viewer: Viewer, data: Data) {
this.id = data.id;
this.createdAt = data.created_at;
this.updatedAt = data.updated_at;
this.createdAt = convertDate(data.created_at);
this.updatedAt = convertDate(data.updated_at);
this.name = data.name;
this.phoneNumber = data.phone_number;
}
Expand Down
5 changes: 3 additions & 2 deletions examples/todo-sqlite/src/ent/generated/tag_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ObjectLoaderFactory,
PrivacyPolicy,
Viewer,
convertDate,
loadEnt,
loadEntX,
loadEnts,
Expand Down Expand Up @@ -39,8 +40,8 @@ export class TagBase {

constructor(public viewer: Viewer, data: Data) {
this.id = data.id;
this.createdAt = data.created_at;
this.updatedAt = data.updated_at;
this.createdAt = convertDate(data.created_at);
this.updatedAt = convertDate(data.updated_at);
this.displayName = data.display_name;
this.canonicalName = data.canonical_name;
this.ownerID = data.owner_id;
Expand Down
8 changes: 5 additions & 3 deletions examples/todo-sqlite/src/ent/generated/todo_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
ObjectLoaderFactory,
PrivacyPolicy,
Viewer,
convertBool,
convertDate,
loadEnt,
loadEntX,
loadEnts,
Expand Down Expand Up @@ -39,10 +41,10 @@ export class TodoBase {

constructor(public viewer: Viewer, data: Data) {
this.id = data.id;
this.createdAt = data.created_at;
this.updatedAt = data.updated_at;
this.createdAt = convertDate(data.created_at);
this.updatedAt = convertDate(data.updated_at);
this.text = data.text;
this.completed = data.completed;
this.completed = convertBool(data.completed);
this.creatorID = data.creator_id;
}

Expand Down
6 changes: 2 additions & 4 deletions examples/todo-sqlite/src/ent/tests/todo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ test("mark as completed", async () => {
completed: true,
}).saveX();

// TODO boolean
expect(todo.completed).toBe(1);
expect(todo.completed).toBe(true);

// reopen
todo = await ChangeTodoStatusAction.create(todo.viewer, todo, {
completed: false,
}).saveX();

// TODO boolean
expect(todo.completed).toBe(0);
expect(todo.completed).toBe(false);
});

test("rename todo", async () => {
Expand Down
5 changes: 3 additions & 2 deletions examples/todo-sqlite/src/ent/testutils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export async function createAccount() {
expect(account.name).toBe("Jon Snow");
expect(account.phoneNumber).toBe(number);
expect(validate(account.id as string)).toBe(true);
expect(account.createdAt).toBeInstanceOf(Date);
expect(account.updatedAt).toBeInstanceOf(Date);
return account;
}

Expand All @@ -40,8 +42,7 @@ export async function createTodo(opts?: Partial<TodoCreateInput>) {
}).saveX();
expect(todo.text).toBe(text);
expect(todo.creatorID).toBe(creatorID);
// TODO need to convert sqlite...
expect(todo.completed).toBe(0);
expect(todo.completed).toBe(false);

return todo;
}
5 changes: 1 addition & 4 deletions examples/todo-sqlite/src/graphql/tests/todo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ test("mark all as completed", async () => {
account.viewer,
account,
).queryEnts();
// TODO sqlite
expect(loadedTodos.every((todo) => !todo.completed)).toBe(true);

await expectMutation(
Expand All @@ -53,8 +52,7 @@ test("mark all as completed", async () => {
account,
).queryEnts();

// TODO sqlite
expect(loadedTodos2.every((todo) => !!todo.completed)).toBe(true);
expect(loadedTodos2.every((todo) => todo.completed)).toBe(true);

await expectMutation(
{
Expand All @@ -72,7 +70,6 @@ test("mark all as completed", async () => {
account,
).queryEnts();

// TODO sqlite
expect(loadedTodos3.every((todo) => !todo.completed)).toBe(true);
});

Expand Down
47 changes: 47 additions & 0 deletions internal/enttype/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ type IDMarkerInterface interface {
IsIDType() bool
}

type ConvertDataType interface {
TSType
Convert() FileImport
}

type TSTypeWithActionFields interface {
TSGraphQLType
GetActionName() string
Expand All @@ -70,6 +75,7 @@ const (
Connection ImportType = "connection"
// EntGraphQL refers to graphql scalars or things in the ent graphql space
EntGraphQL ImportType = "ent_graphql"
Package ImportType = "package"
)

// for imports that are not from "graphql"
Expand Down Expand Up @@ -194,6 +200,13 @@ func (t *boolType) GetZeroValue() string {
return "false"
}

func (t *boolType) Convert() FileImport {
return FileImport{
Type: "convertBool",
ImportType: Package,
}
}

type BoolType struct {
boolType
}
Expand Down Expand Up @@ -246,6 +259,13 @@ func (t *NullableBoolType) GetTSGraphQLImports() []FileImport {
}
}

func (t *NullableBoolType) Convert() FileImport {
return FileImport{
Type: "convertNullableBool",
ImportType: Package,
}
}

// TODO uuid support needed
// and eventually need to work for non uuid types...

Expand Down Expand Up @@ -452,6 +472,13 @@ func (t *timestampType) DefaultGraphQLFieldName() string {
return "time"
}

func (t *timestampType) Convert() FileImport {
return FileImport{
Type: "convertDate",
ImportType: Package,
}
}

type TimestampType struct {
timestampType
}
Expand Down Expand Up @@ -536,6 +563,13 @@ func (t *NullableTimestampType) GetTSGraphQLImports() []FileImport {
}
}

func (t *NullableTimestampType) Convert() FileImport {
return FileImport{
Type: "convertNullableDate",
ImportType: Package,
}
}

type NullableTimestamptzType struct {
NullableTimestampType
}
Expand Down Expand Up @@ -1365,3 +1399,16 @@ func GetGoType(typ types.Type) string {
}
return str[:letterIdx] + fp
}

func IsConvertDataType(t EntType) bool {
_, ok := t.(ConvertDataType)
return ok
}

func ConvertFunc(t EntType) string {
tt, ok := t.(ConvertDataType)
if !ok {
return ""
}
return tt.Convert().Type
}
19 changes: 19 additions & 0 deletions internal/enttype/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type expType struct {
contextType bool
goType string
tsType string
convertFn string
}

func TestStringType(t *testing.T) {
Expand Down Expand Up @@ -122,6 +123,7 @@ func f() bool {
goType: "bool",
nullableType: &enttype.NullableBoolType{},
tsType: "boolean",
convertFn: "convertBool",
}, ret)
}

Expand All @@ -144,6 +146,7 @@ func f() *bool {
nonNullableType: &enttype.BoolType{},
goType: "*bool",
tsType: "boolean | null",
convertFn: "convertNullableBool",
}, ret)
}

Expand Down Expand Up @@ -320,6 +323,7 @@ func f() time.Time {
defaultGQLFieldName: "time",
goType: "time.Time",
tsType: "Date",
convertFn: "convertDate",
}, ret)
}

Expand Down Expand Up @@ -348,6 +352,7 @@ func f() *time.Time {
defaultGQLFieldName: "time",
goType: "*time.Time",
tsType: "Date | null",
convertFn: "convertNullableDate",
}, ret)
}

Expand Down Expand Up @@ -1035,6 +1040,7 @@ func TestTimestamptzType(t *testing.T) {
castToMethod: "cast.ToNullableTime",
defaultGQLFieldName: "time",
zeroValue: "time.Time{}",
convertFn: "convertNullableDate",
},
nil,
},
Expand All @@ -1055,6 +1061,7 @@ func TestTimestamptzType(t *testing.T) {
castToMethod: "cast.ToTime",
defaultGQLFieldName: "time",
zeroValue: "time.Time{}",
convertFn: "convertDate",
},
nil,
},
Expand All @@ -1076,6 +1083,7 @@ func TestTimeType(t *testing.T) {
castToMethod: "cast.ToNullableTime",
defaultGQLFieldName: "time",
zeroValue: "time.Time{}",
convertFn: "convertNullableDate",
},
nil,
},
Expand All @@ -1093,6 +1101,7 @@ func TestTimeType(t *testing.T) {
castToMethod: "cast.ToTime",
defaultGQLFieldName: "time",
zeroValue: "time.Time{}",
convertFn: "convertDate",
},
nil,
},
Expand All @@ -1114,6 +1123,7 @@ func TestTimetzType(t *testing.T) {
castToMethod: "cast.ToNullableTime",
defaultGQLFieldName: "time",
zeroValue: "time.Time{}",
convertFn: "convertNullableDate",
},
nil,
},
Expand All @@ -1131,6 +1141,7 @@ func TestTimetzType(t *testing.T) {
castToMethod: "cast.ToTime",
defaultGQLFieldName: "time",
zeroValue: "time.Time{}",
convertFn: "convertDate",
},
nil,
},
Expand All @@ -1155,6 +1166,7 @@ func TestDateType(t *testing.T) {
castToMethod: "cast.ToNullableTime",
defaultGQLFieldName: "time",
zeroValue: "time.Time{}",
convertFn: "convertNullableDate",
},
nil,
},
Expand All @@ -1175,6 +1187,7 @@ func TestDateType(t *testing.T) {
castToMethod: "cast.ToTime",
defaultGQLFieldName: "time",
zeroValue: "time.Time{}",
convertFn: "convertDate",
},
nil,
},
Expand Down Expand Up @@ -1334,4 +1347,10 @@ func testType(t *testing.T, exp expType, ret returnType) {

assert.Equal(t, exp.errorType, enttype.IsErrorType(typ))
assert.Equal(t, exp.contextType, enttype.IsContextType(typ))

convType, ok := typ.(enttype.ConvertDataType)
if ok {
assert.Equal(t, exp.convertFn, convType.Convert().Type)
}

}
2 changes: 2 additions & 0 deletions internal/graphql/generate_ts_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,8 @@ func getGQLFileImports(imps []enttype.FileImport, mutation bool) []*fileImport {
case enttype.EntGraphQL:
importPath = codepath.GraphQLPackage
break
case enttype.Package:
importPath = codepath.Package
default:
// empty means nothing to import and that's ok...
if imp.ImportType != "" {
Expand Down
Loading

0 comments on commit 7e32576

Please sign in to comment.