Skip to content

Commit

Permalink
add support for views
Browse files Browse the repository at this point in the history
  • Loading branch information
arjendevos committed Feb 18, 2022
1 parent 53909f0 commit 3111f76
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 39 deletions.
48 changes: 27 additions & 21 deletions convert_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ type Model struct { //nolint:maligned
HasDeletedAt bool
HasPrimaryStringID bool
// other stuff
Description string
PureFields []*ast.FieldDefinition
Implements []string
Description string
PureFields []*ast.FieldDefinition
Implements []string
TableNameResolverName string
}

type ColumnSetting struct {
Expand Down Expand Up @@ -776,25 +777,30 @@ func getModelsFromSchema(schema *ast.Schema, boilerModels []*BoilerModel) (model

hasBoilerModel := !hasEmptyBoilerModel
hasDeletedAt := hasBoilerModel && boilerModel.HasDeletedAt
tableNameResolverName := "TableNames"
if hasBoilerModel && boilerModel.IsView {
tableNameResolverName = "ViewNames"
}
m := &Model{
Name: modelName,
Description: schemaType.Description,
PluralName: Plural(modelName),
BoilerModel: boilerModel,
HasBoilerModel: hasBoilerModel,
IsInput: isInput,
IsFilter: isFilter,
IsWhere: isWhere,
IsUpdateInput: isUpdateInput,
IsCreateInput: isCreateInput,
IsNormalInput: isNormalInput,
IsConnection: isConnection,
IsEdge: isEdge,
IsPayload: isPayload,
IsOrdering: isOrdering,
IsNormal: isNormal,
IsPreloadable: isNormal,
HasDeletedAt: hasDeletedAt,
Name: modelName,
Description: schemaType.Description,
PluralName: Plural(modelName),
BoilerModel: boilerModel,
HasBoilerModel: hasBoilerModel,
IsInput: isInput,
IsFilter: isFilter,
IsWhere: isWhere,
IsUpdateInput: isUpdateInput,
IsCreateInput: isCreateInput,
IsNormalInput: isNormalInput,
IsConnection: isConnection,
IsEdge: isEdge,
IsPayload: isPayload,
IsOrdering: isOrdering,
IsNormal: isNormal,
IsPreloadable: isNormal,
HasDeletedAt: hasDeletedAt,
TableNameResolverName: tableNameResolverName,
}

for _, implementor := range schema.GetImplements(schemaType) {
Expand Down
11 changes: 10 additions & 1 deletion sqlboiler_graphql_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type SchemaGenerateConfig struct {

type SchemaModel struct {
Name string
IsView bool
Fields []*SchemaField
}

Expand Down Expand Up @@ -348,9 +349,13 @@ func SchemaGet(

w.br()

// Generate input and payloads for mutatations
// Generate input and payloads for mutations
if config.GenerateMutations { //nolint:nestif
for _, model := range models {
if model.IsView {
continue
}

filteredFields := fieldsWithout(model.Fields, config.SkipInputFields)

modelPluralName := Plural(model.Name)
Expand Down Expand Up @@ -486,6 +491,9 @@ func SchemaGet(
w.l("type Mutation {")

for _, model := range models {
if model.IsView {
continue
}
modelPluralName := Plural(model.Name)

// create single
Expand Down Expand Up @@ -577,6 +585,7 @@ func boilerModelsToModels(boilerModels []*BoilerModel) []*SchemaModel {
a[i] = &SchemaModel{
Name: boilerModel.Name,
Fields: boilerFieldsToFields(boilerModel.Fields),
IsView: boilerModel.IsView,
}
}
return a
Expand Down
24 changes: 24 additions & 0 deletions sqlboiler_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type BoilerModel struct {
Enums []*BoilerEnum
HasPrimaryStringID bool
HasDeletedAt bool
IsView bool
}

type BoilerField struct {
Expand Down Expand Up @@ -65,6 +66,7 @@ func GetBoilerModels(dir string) ([]*BoilerModel, []*BoilerEnum) { //nolint:goco
boilerTypes := getSortedBoilerTypes(boilerTypeMap, boilerTypeOrder)
tableNames := parseTableNames(dir)
enums := parseEnums(dir)
viewNames := parseViews(dir)

// sortedModelNames is needed to get the right order back of the models since we want the same order every time
// this program has ran.
Expand Down Expand Up @@ -169,6 +171,7 @@ func GetBoilerModels(dir string) ([]*BoilerModel, []*BoilerEnum) { //nolint:goco
Enums: filterEnumsByModelName(enums, modelName),
HasPrimaryStringID: hasPrimaryStringID,
HasDeletedAt: hasDeletedAt,
IsView: sliceContains(viewNames, modelName),
}
}

Expand Down Expand Up @@ -349,6 +352,27 @@ func parseTableNames(dir string) []string {
return tableNames
}

func parseViews(dir string) []string {
dir, err := filepath.Abs(dir)
errMessage := "could not open boiler table names file, this could not lead to problems if you're " +
"using plural table names"
if err != nil {
log.Warn().Err(err).Msg(errMessage)
return nil
}
content, err := ioutil.ReadFile(filepath.Join(dir, "boil_view_names.go"))
if err != nil {
log.Warn().Err(err).Msg(errMessage)
return nil
}
viewNamesMatches := tableNameRegex.FindAllStringSubmatch(string(content), -1)
viewNames := make([]string, len(viewNamesMatches))
for i, tableNameMatch := range viewNamesMatches {
viewNames[i] = tableNameMatch[1]
}
return viewNames
}

var (
enumRegex = regexp.MustCompile(`// Enum values for (.*)\nconst\s\(\n(:?(.|\n)*?)\n\)`) //nolint:gochecknoglobals
enumValuesRegex = regexp.MustCompile(`\s(\w+)\s*=\s*"(\w+)"`) //nolint:gochecknoglobals
Expand Down
4 changes: 2 additions & 2 deletions template_files/generated_convert.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@ import (
{{ range $field := .Fields }}
{{- if $field.IsPrimaryNumberID -}}
func {{ $model.Name }}IDToGraphQL(v uint) string {
return boilergql.IDToGraphQL(v, {{ $.Backend.PackageName }}.TableNames.{{ $model.BoilerModel.TableName }})
return boilergql.IDToGraphQL(v, {{ $.Backend.PackageName }}.{{- $model.TableNameResolverName }}.{{ $model.BoilerModel.TableName }})
}
{{- end -}}
{{- if $field.IsPrimaryStringID -}}
func {{ $model.Name }}IDToGraphQL(v string) string {
return boilergql.StringIDToGraphQL(v, {{ $.Backend.PackageName }}.TableNames.{{ $model.BoilerModel.TableName }})
return boilergql.StringIDToGraphQL(v, {{ $.Backend.PackageName }}.{{- $model.TableNameResolverName }}.{{ $model.BoilerModel.TableName }})
}
{{- end -}}
{{- end }}
Expand Down
2 changes: 1 addition & 1 deletion template_files/generated_convert_batch.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const batchInsertStatement = "INSERT INTO %s (%s) VALUES %s"
queryMarks, values := {{ lcFirst .BoilerModel.PluralName }}ToBatchCreate(a)
// nolint: gosec -> remove warning because no user input without questions marks
return fmt.Sprintf(batchInsertStatement,
{{ $.Backend.PackageName }}.TableNames.{{ .BoilerModel.Name }},
{{ $.Backend.PackageName }}.{{- .TableNameResolverName }}.{{ .BoilerModel.Name }},
strings.Join({{ lcFirst .BoilerModel.PluralName }}BatchCreateColumns, ", "),
strings.Join(queryMarks, ", "),
), values
Expand Down
14 changes: 7 additions & 7 deletions template_files/generated_filter.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,11 @@ func TimeUnixFilterToMods(m *{{ $.Frontend.PackageName }}.TimeUnixFilter, c stri
{{ range $field := .Fields }}
{{- if and $field.IsRelation $field.BoilerField.IsRelation }}
{{- if $field.IsPlural }}
queryMods = append(queryMods, {{ $field.TypeWithoutPointer|go }}SubqueryToMods(m.{{ $field.Name }}, "", {{ $.Backend.PackageName }}.TableNames.{{- $model.BoilerModel.TableName }})...)
queryMods = append(queryMods, {{ $field.TypeWithoutPointer|go }}SubqueryToMods(m.{{ $field.Name }}, "", {{ $.Backend.PackageName }}.{{- $model.TableNameResolverName }}.{{- $model.BoilerModel.TableName }})...)
{{- else if $field.BoilerField.IsForeignKey }}
queryMods = append(queryMods, {{ $field.TypeWithoutPointer|go }}SubqueryToMods(m.{{ $field.Name }}, {{ $.Backend.PackageName }}.{{ $model.BoilerModel.Name }}Columns.{{ $field.BoilerField.Name }}, {{ $.Backend.PackageName }}.TableNames.{{- $model.BoilerModel.TableName }})...)
queryMods = append(queryMods, {{ $field.TypeWithoutPointer|go }}SubqueryToMods(m.{{ $field.Name }}, {{ $.Backend.PackageName }}.{{ $model.BoilerModel.Name }}Columns.{{ $field.BoilerField.Name }}, {{ $.Backend.PackageName }}.{{- $model.TableNameResolverName }}.{{- $model.BoilerModel.TableName }})...)
{{- else }}
queryMods = append(queryMods, {{ $field.TypeWithoutPointer|go }}SubqueryToMods(m.{{ $field.Name }}, "", {{ $.Backend.PackageName }}.TableNames.{{- $model.BoilerModel.TableName }})...)
queryMods = append(queryMods, {{ $field.TypeWithoutPointer|go }}SubqueryToMods(m.{{ $field.Name }}, "", {{ $.Backend.PackageName }}.{{- $model.TableNameResolverName }}.{{- $model.BoilerModel.TableName }})...)
{{- end }}
{{- else if $field.IsOr }}
if m.Or != nil {
Expand All @@ -441,13 +441,13 @@ func TimeUnixFilterToMods(m *{{ $.Frontend.PackageName }}.TimeUnixFilter, c stri
{{- if and $field.IsRelation $field.BoilerField.IsRelation -}}
{{- if not $field.IsPlural -}}
{{- if $field.BoilerField.IsForeignKey }}
if parentTable == {{ $.Backend.PackageName }}.TableNames.{{ $field.Relationship.BoilerModel.TableName }} {
queryMods = append(queryMods, qm.Where(fmt.Sprintf(parentTableStatement, {{ $.Backend.PackageName }}.TableNames.{{- $model.BoilerModel.TableName }}, {{ $.Backend.PackageName }}.{{ $model.BoilerModel.Name }}Columns.{{ $field.BoilerField.Name }}, parentTable, parentForeignKey)))
if parentTable == {{ $.Backend.PackageName }}.{{- $model.TableNameResolverName }}.{{ $field.Relationship.BoilerModel.TableName }} {
queryMods = append(queryMods, qm.Where(fmt.Sprintf(parentTableStatement, {{ $.Backend.PackageName }}.{{- $model.TableNameResolverName }}.{{- $model.BoilerModel.TableName }}, {{ $.Backend.PackageName }}.{{ $model.BoilerModel.Name }}Columns.{{ $field.BoilerField.Name }}, parentTable, parentForeignKey)))
}
{{- end -}}
{{- else }}
if parentTable == {{ $.Backend.PackageName }}.TableNames.{{ $field.Relationship.BoilerModel.TableName }} {
queryMods = append(queryMods, qm.Where(fmt.Sprintf(parentTableStatement, {{ $.Backend.PackageName }}.TableNames.{{- $model.BoilerModel.TableName }}, {{ $.Backend.PackageName }}.{{ $model.BoilerModel.Name }}Columns.ID, parentTable, parentForeignKey)))
if parentTable == {{ $.Backend.PackageName }}.{{- $model.TableNameResolverName }}.{{ $field.Relationship.BoilerModel.TableName }} {
queryMods = append(queryMods, qm.Where(fmt.Sprintf(parentTableStatement, {{ $.Backend.PackageName }}.{{- $model.TableNameResolverName }}.{{- $model.BoilerModel.TableName }}, {{ $.Backend.PackageName }}.{{ $model.BoilerModel.Name }}Columns.ID, parentTable, parentForeignKey)))
}
{{- end -}}
{{- end -}}
Expand Down
10 changes: 5 additions & 5 deletions template_files/generated_preload.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ import (
var TablePreloadMap = map[string]map[string]boilergql.ColumnSetting{
{{ range $model := .Models -}}
{{ if $model.IsPreloadable -}}
{{ $.Backend.PackageName }}.TableNames.{{- $model.BoilerModel.TableName }}: {
{{ $.Backend.PackageName }}.{{- $model.TableNameResolverName }}.{{- $model.BoilerModel.TableName }}: {
{{- range $value := $model.PreloadArray }}
"{{$value.Key}}": {
Name: {{$value.ColumnSetting.Name}},
RelationshipModelName: {{ $.Backend.PackageName }}.TableNames.{{$value.ColumnSetting.RelationshipModelName}},
RelationshipModelName: {{ $.Backend.PackageName }}.{{- $model.TableNameResolverName }}.{{$value.ColumnSetting.RelationshipModelName}},
IDAvailable: {{$value.ColumnSetting.IDAvailable}},
},
{{- end }}
Expand All @@ -53,13 +53,13 @@ var TablePreloadMap = map[string]map[string]boilergql.ColumnSetting{
{{ range $model := .Models }}
{{ if $model.IsPreloadable -}}
func Get{{ .Name }}PreloadMods(ctx context.Context) (queryMods []qm.QueryMod) {
return boilergql.GetPreloadModsWithLevel(ctx, TablePreloadMap, {{ $.Backend.PackageName }}.TableNames.{{ $model.BoilerModel.TableName }}, "")
return boilergql.GetPreloadModsWithLevel(ctx, TablePreloadMap, {{ $.Backend.PackageName }}.{{- $model.TableNameResolverName }}.{{ $model.BoilerModel.TableName }}, "")
}
func Get{{ .Name }}NodePreloadMods(ctx context.Context) (queryMods []qm.QueryMod) {
return boilergql.GetPreloadModsWithLevel(ctx, TablePreloadMap, {{ $.Backend.PackageName }}.TableNames.{{ $model.BoilerModel.TableName }}, DefaultLevels.EdgesNode)
return boilergql.GetPreloadModsWithLevel(ctx, TablePreloadMap, {{ $.Backend.PackageName }}.{{- $model.TableNameResolverName }}.{{ $model.BoilerModel.TableName }}, DefaultLevels.EdgesNode)
}
func Get{{ .Name }}PreloadModsWithLevel(ctx context.Context, level string) (queryMods []qm.QueryMod) {
return boilergql.GetPreloadModsWithLevel(ctx, TablePreloadMap, {{ $.Backend.PackageName }}.TableNames.{{ $model.BoilerModel.TableName }}, level)
return boilergql.GetPreloadModsWithLevel(ctx, TablePreloadMap, {{ $.Backend.PackageName }}.{{- $model.TableNameResolverName }}.{{ $model.BoilerModel.TableName }}, level)
}
{{ end -}}
{{- end }}
Expand Down
4 changes: 2 additions & 2 deletions template_files/generated_resolver.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ const inputKey = "input"
{{- end }}
mods = append(mods, {{.Model.Name}}FilterToMods(filter)...)
mods = append(mods, qm.Select(dm.{{ .Model.Name }}Columns.ID))
mods = append(mods, qm.From(dm.TableNames.{{ .Model.BoilerModel.TableName }}))
mods = append(mods, qm.From(dm.{{- .Model.TableNameResolverName }}.{{ .Model.BoilerModel.TableName }}))

{{- if .Model.HasPrimaryStringID }}
var IDsToRemove []boilergql.RemovedStringID
Expand All @@ -290,7 +290,7 @@ const inputKey = "input"
}

return &fm.{{ .Model.PluralName }}DeletePayload{
Ids: boilergql.{{.Model.PrimaryKeyType|go}}IDsToGraphQL(boilerIDs, dm.TableNames.{{ .Model.BoilerModel.TableName }}),
Ids: boilergql.{{.Model.PrimaryKeyType|go}}IDsToGraphQL(boilerIDs, dm.{{- .Model.TableNameResolverName }}.{{ .Model.BoilerModel.TableName }}),
}, nil
{{- end }}
}
Expand Down

0 comments on commit 3111f76

Please sign in to comment.