Skip to content

Commit

Permalink
Refactor preload logic
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardLindhout committed May 11, 2020
1 parent 8d46a1f commit 7a0f6a2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 55 deletions.
46 changes: 7 additions & 39 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Model struct {
IsPayload bool
IsWhere bool
IsFilter bool
IsPreloadable bool
PreloadArray []Preload
HasOrganizationID bool
HasUserOrganizationID bool
Expand Down Expand Up @@ -646,6 +647,7 @@ func getModelsFromSchema(schema *ast.Schema, boilerModels []*BoilerModel) (model
IsNormalInput: isNormalInput,
IsPayload: isPayload,
IsNormal: !isInput && !isWhere && !isFilter && !isPayload,
IsPreloadable: !isInput && !isWhere && !isFilter && !isPayload,
}

for _, implementor := range schema.GetImplements(schemaType) {
Expand All @@ -660,14 +662,6 @@ func getModelsFromSchema(schema *ast.Schema, boilerModels []*BoilerModel) (model
return
}

func isPreloadableModel(m *Model) bool {
// nolint: gosimple -> keep it more readable
if m.IsInput {
return false
}
return true
}

func getPreloadMapForModel(model *Model) map[string]ColumnSetting {
preloadMap := map[string]ColumnSetting{}
for _, field := range model.Fields {
Expand Down Expand Up @@ -696,48 +690,22 @@ func getPreloadMapForModel(model *Model) map[string]ColumnSetting {
const maximumLevelOfPreloads = 4

func enhanceModelsWithPreloadArray(models []*Model) {
fullMap := map[string]map[string]ColumnSetting{}
preloadMapPerModel := map[string]map[string]ColumnSetting{}
// first assing basic first level relations

// first adding basic first level relations
for _, model := range models {
if !isPreloadableModel(model) {
if !model.IsPreloadable {
continue
}

preloadMapPerModel[model.Name] = getPreloadMapForModel(model)
fullMap[model.Name] = getPreloadMapForModel(model)
}

for nested := 1; nested <= maximumLevelOfPreloads; nested++ {
// reverse loop since nested count works that way
// otherwise too much fields are added on the last models
for i := len(models) - 1; i >= 0; i-- {
model := models[i]
if !isPreloadableModel(model) {
continue
}
enhancePreloadMapWithNestedRelations(fullMap, preloadMapPerModel, model.Name)
}
}

for _, model := range models {

modelPreloadMap := fullMap[model.Name]

model.PreloadArray = make([]Preload, len(modelPreloadMap))
modelPreloadMap := getPreloadMapForModel(model)

sortedPreloadKeys := make([]string, 0, len(modelPreloadMap))
for k := range modelPreloadMap {
sortedPreloadKeys = append(sortedPreloadKeys, k)
}
// fmt.Println("FOR MODEL!!!!!!!!!!", model.Name)

// fmt.Println("BEFORE sortedPreloadKeys", sortedPreloadKeys)
// fmt.Println(" ")
sort.Strings(sortedPreloadKeys)
// fmt.Println(" ")
// fmt.Println("AFTER sortedPreloadKeys", sortedPreloadKeys)

model.PreloadArray = make([]Preload, len(sortedPreloadKeys))
for i, k := range sortedPreloadKeys {
columnSetting := modelPreloadMap[k]
model.PreloadArray[i] = Preload{
Expand Down
41 changes: 29 additions & 12 deletions preload.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,38 @@
{{ reserveImport $.BackendModelsPath }}
{{ reserveImport $.FrontendModelsPath }}


var TablePreloadMap = map[string]map[string]boilergql.ColumnSetting{
{{ range $model := .Models -}}
{{ if $model.IsPreloadable -}}
models.TableNames.{{- $model.Name }}: map[string]boilergql.ColumnSetting{
{{- range $value := $model.PreloadArray }}
"{{$value.Key}}": {
Name: {{$value.ColumnSetting.Name}},
RelationshipModelName: models.TableNames.{{$value.ColumnSetting.RelationshipModelName}},
IDAvailable: {{$value.ColumnSetting.IDAvailable}},
},
{{- end }}
},
{{ end -}}
{{ end -}}
}

{{ range $model := .Models }}
{{with .Description }} {{.|prefixLines "// "}} {{end}}

{{ if $model.IsPreloadable -}}
func Get{{ .Name|go }}PreloadMods(ctx context.Context) (queryMods []qm.QueryMod) {
return boilergql.GetPreloadModsWithLevel(ctx, TablePreloadMap, models.TableNames.{{ .Name|go }}, "")
}


{{- if .IsPayload -}}
func Get{{ .Name|go }}PreloadModsWithLevel(ctx context.Context, level string) (queryMods []qm.QueryMod) {
return boilergql.GetPreloadModsWithLevel(ctx, TablePreloadMap, models.TableNames.{{ .Name|go }}, level)
}
{{ end -}}
{{- end }}
{{ range $model := .Models }}
{{ if .IsPayload -}}
var {{ .Name|go }}PreloadLevels = struct {
{{ range $field := .Fields }}
{{- if $field.IsRelation -}}
Expand All @@ -47,15 +73,6 @@
}
{{ end }}

{{- if .IsNormal -}}
var {{ $model.Name|go }}PreloadMap = map[string]boilergql.ColumnSetting{
{{- range $value := $model.PreloadArray }}
"{{$value.Key}}": {
Name: {{$value.ColumnSetting.Name}},
IDAvailable: {{$value.ColumnSetting.IDAvailable}},
},
{{- end }}
}
{{ end }}


{{- end }}
8 changes: 4 additions & 4 deletions resolver.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const inputKey = "input"
dbID := {{ .Model.Name }}ID(id)
{{- end }}

mods := boilergql.GetPreloadMods(ctx, {{ .Model.Name|go }}PreloadMap)
mods := Get{{ .Model.Name|go }}PreloadMods(ctx)
mods = append(mods, dm.{{ .Model.Name|go }}Where.ID.EQ(dbID))
{{- if .Model.BoilerModel.HasOrganizationID }}
mods = append(mods, dm.{{ .Model.Name }}Where.OrganizationID.EQ(
Expand All @@ -73,7 +73,7 @@ const inputKey = "input"
{{- end -}}

{{- if .IsList }}
mods := boilergql.GetPreloadMods(ctx, {{ .Model.Name|go }}PreloadMap)
mods := Get{{ .Model.Name|go }}PreloadMods(ctx)
{{- if .Model.BoilerModel.HasOrganizationID }}
mods = append(mods, dm.{{ .Model.Name }}Where.OrganizationID.EQ(
auth.OrganizationIDFromContext(ctx),
Expand Down Expand Up @@ -168,7 +168,7 @@ const inputKey = "input"
}

// resolve requested fields after creating
mods := boilergql.GetPreloadModsWithLevel(ctx, {{ .Model.Name|go }}PreloadMap, {{ .Model.Name|go }}PayloadPreloadLevels.{{ .Model.Name|go }})
mods := Get{{ .Model.Name|go }}PreloadModsWithLevel(ctx, {{ .Model.Name|go }}PayloadPreloadLevels.{{ .Model.Name|go }})
mods = append(mods, dm.{{ .Model.Name|go }}Where.ID.EQ(m.ID))
{{- if .Model.BoilerModel.HasOrganizationID }}
mods = append(mods, dm.{{ .Model.Name }}Where.OrganizationID.EQ(
Expand Down Expand Up @@ -265,7 +265,7 @@ const inputKey = "input"
}

// resolve requested fields after updating
mods := boilergql.GetPreloadModsWithLevel(ctx, {{ .Model.Name|go }}PreloadMap, {{ .Model.Name|go }}PayloadPreloadLevels.{{ .Model.Name|go }})
mods := Get{{ .Model.Name|go }}PreloadModsWithLevel(ctx, {{ .Model.Name|go }}PayloadPreloadLevels.{{ .Model.Name|go }})
mods = append(mods, dm.{{ .Model.Name|go }}Where.ID.EQ(dbID))
{{- if .Model.BoilerModel.HasOrganizationID }}
mods = append(mods, dm.{{ .Model.Name }}Where.OrganizationID.EQ(auth.OrganizationIDFromContext(ctx)))
Expand Down

0 comments on commit 7a0f6a2

Please sign in to comment.