Skip to content

Commit

Permalink
kill enttype.InputImportType
Browse files Browse the repository at this point in the history
more in the technical debt cleanup

uses json marshalling tricks to make it work

was added by #511

also fixes #703
  • Loading branch information
lolopinto committed Feb 28, 2022
1 parent edef1b3 commit 54c6ffc
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 54 deletions.
48 changes: 24 additions & 24 deletions internal/enttype/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ func TestJSONType(t *testing.T) {
},
"json with import type": {
&enttype.JSONType{
ImportType: &enttype.InputImportType{
Type: "Foo",
Path: "path",
ImportType: &tsimport.ImportPath{
Import: "Foo",
ImportPath: "path",
},
},
expType{
Expand All @@ -94,9 +94,9 @@ func TestJSONType(t *testing.T) {
},
tsType: "Foo",
nullableType: &enttype.NullableJSONType{
ImportType: &enttype.InputImportType{
Type: "Foo",
Path: "path",
ImportType: &tsimport.ImportPath{
Import: "Foo",
ImportPath: "path",
},
},
goTypePanics: true,
Expand All @@ -108,9 +108,9 @@ func TestJSONType(t *testing.T) {
},
"nullable json with import type": {
&enttype.NullableJSONType{
ImportType: &enttype.InputImportType{
Type: "Foo",
Path: "path",
ImportType: &tsimport.ImportPath{
Import: "Foo",
ImportPath: "path",
},
},
expType{
Expand All @@ -121,9 +121,9 @@ func TestJSONType(t *testing.T) {
},
tsType: "Foo | null",
nonNullableType: &enttype.JSONType{
ImportType: &enttype.InputImportType{
Type: "Foo",
Path: "path",
ImportType: &tsimport.ImportPath{
Import: "Foo",
ImportPath: "path",
},
},
goTypePanics: true,
Expand All @@ -135,9 +135,9 @@ func TestJSONType(t *testing.T) {
},
"jsonb with import type": {
&enttype.JSONBType{
ImportType: &enttype.InputImportType{
Type: "Foo",
Path: "path",
ImportType: &tsimport.ImportPath{
Import: "Foo",
ImportPath: "path",
},
},
expType{
Expand All @@ -149,9 +149,9 @@ func TestJSONType(t *testing.T) {
},
tsType: "Foo",
nullableType: &enttype.NullableJSONBType{
ImportType: &enttype.InputImportType{
Type: "Foo",
Path: "path",
ImportType: &tsimport.ImportPath{
Import: "Foo",
ImportPath: "path",
},
},
goTypePanics: true,
Expand All @@ -163,9 +163,9 @@ func TestJSONType(t *testing.T) {
},
"nullable jsonb with import type": {
&enttype.NullableJSONBType{
ImportType: &enttype.InputImportType{
Type: "Foo",
Path: "path",
ImportType: &tsimport.ImportPath{
Import: "Foo",
ImportPath: "path",
},
},
expType{
Expand All @@ -176,9 +176,9 @@ func TestJSONType(t *testing.T) {
},
tsType: "Foo | null",
nonNullableType: &enttype.JSONBType{
ImportType: &enttype.InputImportType{
Type: "Foo",
Path: "path",
ImportType: &tsimport.ImportPath{
Import: "Foo",
ImportPath: "path",
},
},
goTypePanics: true,
Expand Down
34 changes: 14 additions & 20 deletions internal/enttype/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type TSTypeWithActionFields interface {

type ImportDepsType interface {
TSGraphQLType
GetImportDepsType() *InputImportType
GetImportDepsType() *tsimport.ImportPath
}

type ListType interface {
Expand Down Expand Up @@ -1708,12 +1708,6 @@ func (t *NullableArrayListType) Convert() *tsimport.ImportPath {
return elem.convertNullableListWithItem()
}

// to resolve circular dependency btw input and this
type InputImportType struct {
Path string `json:"path"`
Type string `json:"type"`
}

type jSONType struct {
}

Expand All @@ -1739,21 +1733,21 @@ func (t *jSONType) GetGraphQLType() string {
return "JSON!"
}

func (t *jSONType) getTsType(nullable bool, impType *InputImportType) string {
func (t *jSONType) getTsType(nullable bool, impType *tsimport.ImportPath) string {
if impType == nil {
return "any"
}
if nullable {
return impType.Type + " | null"
return impType.Import + " | null"
}
return impType.Type
return impType.Import
}

func (t *jSONType) getTsTypeImports(impType *InputImportType) []string {
func (t *jSONType) getTsTypeImports(impType *tsimport.ImportPath) []string {
if impType == nil {
return nil
}
return []string{impType.Type}
return []string{impType.Import}
}

// TODO https://github.com/taion/graphql-type-json
Expand All @@ -1777,7 +1771,7 @@ func (t *jSONType) convertNullableListWithItem() *tsimport.ImportPath {
}

type JSONType struct {
ImportType *InputImportType
ImportType *tsimport.ImportPath
jSONType
}

Expand All @@ -1803,12 +1797,12 @@ func (t *JSONType) GetTsTypeImports() []string {
return t.getTsTypeImports(t.ImportType)
}

func (t *JSONType) GetImportDepsType() *InputImportType {
func (t *JSONType) GetImportDepsType() *tsimport.ImportPath {
return t.ImportType
}

type NullableJSONType struct {
ImportType *InputImportType
ImportType *tsimport.ImportPath
jSONType
}

Expand Down Expand Up @@ -1844,12 +1838,12 @@ func (t *NullableJSONType) GetTsTypeImports() []string {
return t.getTsTypeImports(t.ImportType)
}

func (t *NullableJSONType) GetImportDepsType() *InputImportType {
func (t *NullableJSONType) GetImportDepsType() *tsimport.ImportPath {
return t.ImportType
}

type JSONBType struct {
ImportType *InputImportType
ImportType *tsimport.ImportPath
jSONType
}

Expand All @@ -1875,7 +1869,7 @@ func (t *JSONBType) GetTsTypeImports() []string {
return t.getTsTypeImports(t.ImportType)
}

func (t *JSONBType) GetImportDepsType() *InputImportType {
func (t *JSONBType) GetImportDepsType() *tsimport.ImportPath {
return t.ImportType
}

Expand All @@ -1884,7 +1878,7 @@ func (t *JSONBType) GetImportType() Import {
}

type NullableJSONBType struct {
ImportType *InputImportType
ImportType *tsimport.ImportPath
jSONType
}

Expand Down Expand Up @@ -1920,7 +1914,7 @@ func (t *NullableJSONBType) GetTsTypeImports() []string {
return t.getTsTypeImports(t.ImportType)
}

func (t *NullableJSONBType) GetImportDepsType() *InputImportType {
func (t *NullableJSONBType) GetImportDepsType() *tsimport.ImportPath {
return t.ImportType
}

Expand Down
31 changes: 29 additions & 2 deletions internal/schema/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/lolopinto/ent/ent"
"github.com/lolopinto/ent/internal/enttype"
"github.com/lolopinto/ent/internal/schemaparser"
"github.com/lolopinto/ent/internal/tsimport"
)

type Schema struct {
Expand Down Expand Up @@ -88,8 +89,34 @@ type FieldType struct {
Type string `json:"type"`
GraphQLType string `json:"graphQLType"`
// optional used by generator to specify different types e.g. email, phone, password
CustomType CustomType `json:"customType"`
ImportType *enttype.InputImportType `json:"importType"`
CustomType CustomType `json:"customType"`

ImportType *tsimport.ImportPath
ImportTypeIgnore *importType `json:"importType"`
}

// needed to get the data from json and then discarded
type importType struct {
Path string `json:"path"`
Type string `json:"type"`
}

func (ft *FieldType) UnmarshalJSON(data []byte) error {
type Alias FieldType
err := json.Unmarshal(data, (*Alias)(ft))
if err != nil {
return err
}
if ft.ImportTypeIgnore == nil {
return nil
}
// need a test that tests this...
ft.ImportType = &tsimport.ImportPath{
ImportPath: ft.ImportTypeIgnore.Path,
Import: ft.ImportTypeIgnore.Type,
}
ft.ImportTypeIgnore = nil
return nil
}

type Field struct {
Expand Down
38 changes: 38 additions & 0 deletions internal/schema/input/parse_ts_field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/lolopinto/ent/internal/schema/input"
"github.com/lolopinto/ent/internal/tsimport"
)

func TestParseFields(t *testing.T) {
Expand Down Expand Up @@ -692,6 +693,43 @@ func TestParseFields(t *testing.T) {
},
},
},
"jsonb import ype": {
code: map[string]string{
"user.ts": getCodeWithSchema(`
import {Schema, Field, BaseEntSchema, JSONBType } from "{schema}"
export default class User extends BaseEntSchema implements Schema {
fields: Field[] = [
JSONBType({name: "foo", importType: {
type: "Foo",
path: "path/to_foo.ts",
} }),
]
}`),
},
expectedPatterns: map[string]pattern{
"node": {
name: "node",
},
},
expectedNodes: map[string]node{
"User": {
fields: fieldsWithNodeFields(
field{
name: "foo",
dbType: input.JSONB,
typ: &input.FieldType{
DBType: input.JSONB,
ImportType: &tsimport.ImportPath{
ImportPath: "path/to_foo.ts",
Import: "Foo",
},
},
},
),
},
},
},
}

runTestCases(t, testCases)
Expand Down
10 changes: 2 additions & 8 deletions internal/schema/node_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,7 @@ func (nodeData *NodeData) GetImportsForBaseFile() ([]*tsimport.ImportPath, error
imp := t2.GetImportDepsType()
if imp != nil {
// TODO ignoring relative. do we need it?
ret = append(ret, &tsimport.ImportPath{
ImportPath: imp.Path,
Import: imp.Type,
})
ret = append(ret, imp)
}
}
}
Expand Down Expand Up @@ -288,10 +285,7 @@ func (nodeData *NodeData) GetImportPathsForDependencies(s *Schema) []*tsimport.I
t2 := t.(enttype.ImportDepsType)
imp := t2.GetImportDepsType()
if imp != nil {
ret = append(ret, &tsimport.ImportPath{
ImportPath: imp.Path,
Import: imp.Type,
})
ret = append(ret, imp)
}
}
}
Expand Down

0 comments on commit 54c6ffc

Please sign in to comment.