Skip to content

Commit

Permalink
Merge pull request #80 from fasibio/76-add-comments-to-generated-code
Browse files Browse the repository at this point in the history
fix(#76):added comments
  • Loading branch information
fasibio authored Jul 31, 2023
2 parents 8314945 + 5a7d61c commit 0debaf7
Show file tree
Hide file tree
Showing 14 changed files with 1,213 additions and 217 deletions.
3 changes: 3 additions & 0 deletions generate_code_db.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ type {{$baseName}}DB struct {
Db *gorm.DB
Hooks map[string]any
}

// Create a new {{$baseName}}DB
func New{{$baseName}}DB(db *gorm.DB) {{$baseName}}DB {
return {{$baseName}}DB{
Db: db,
Hooks: make(map[string]any),
}
}

//execute Gorm AutoMigrate with all @SQL Graphql Types
func (db *{{$baseName}}DB) Init() {
db.Db.AutoMigrate({{.ModelsMigrations}})
}
Expand Down
289 changes: 248 additions & 41 deletions generate_code_db_hook.go.tpl

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions generate_code_filter_model.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
{{ reserveImport "github.com/fasibio/autogql/runtimehelper" }}

{{$methodeName := "ExtendsDatabaseQuery"}}
{{- $methodeNameComment := printf "// %s create condition from values " $methodeName }}

const extendsDatabaseFieldNameFormat string = "%[2]s.%[1]s%[3]s%[1]s"

{{- $root := .}}

{{- range $objectName, $object := .Handler.List.Objects }}
{{- if $object.HasSqlDirective}}

// PrimaryKeyName return the name of primarykey for Table {{$object.Name}}
func (d *{{$object.Name}}FiltersInput) PrimaryKeyName() string {
return "{{$root.PrimaryKeyOfObject $object.Name}}"
}

// {{$methodeName}} create condition from {{$object.Name}}FiltersInput values
func (d *{{$object.Name}}FiltersInput) {{$methodeName}}(db *gorm.DB, alias string,deep bool, blackList map[string]struct{}) []runtimehelper.ConditionElement {
res := make([]runtimehelper.ConditionElement, 0)
if d.And != nil {
Expand Down Expand Up @@ -162,6 +166,7 @@ func (d *StringFilterInput) ExtendsDatabaseQuery(db *gorm.DB, fieldName string,
return res
}

{{ $methodeNameComment }}
func (d *IntFilterInput) ExtendsDatabaseQuery(db *gorm.DB, fieldName string, deep bool, blackList map[string]struct{}) []runtimehelper.ConditionElement {
res := make([]runtimehelper.ConditionElement, 0)
Expand Down Expand Up @@ -231,6 +236,7 @@ func (d *IntFilterInput) ExtendsDatabaseQuery(db *gorm.DB, fieldName string, dee
return res
}

{{ $methodeNameComment }}
func (d *BooleanFilterInput) ExtendsDatabaseQuery(db *gorm.DB, fieldName string, deep bool, blackList map[string]struct{}) []runtimehelper.ConditionElement {
res := make([]runtimehelper.ConditionElement, 0)
Expand Down Expand Up @@ -269,6 +275,7 @@ func (d *BooleanFilterInput) ExtendsDatabaseQuery(db *gorm.DB, fieldName string,
return res
}

{{ $methodeNameComment }}
func (d *TimeFilterInput) ExtendsDatabaseQuery(db *gorm.DB, fieldName string, deep bool, blackList map[string]struct{}) []runtimehelper.ConditionElement {
res := make([]runtimehelper.ConditionElement, 0)
Expand Down Expand Up @@ -339,6 +346,7 @@ func (d *TimeFilterInput) ExtendsDatabaseQuery(db *gorm.DB, fieldName string, de
return res
}

{{ $methodeNameComment }}
func (d *IDFilterInput) ExtendsDatabaseQuery(db *gorm.DB, fieldName string, deep bool, blackList map[string]struct{}) []runtimehelper.ConditionElement {
res := make([]runtimehelper.ConditionElement, 0)
Expand Down
24 changes: 23 additions & 1 deletion generate_code_model.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@
{{- $root := .}}
{{- $input2TypeName := "MergeToType"}}

// GetInputStruct returns struct filled from map obj defined by name
// Example useage struct validation with github.com/go-playground/validator by directive:
// func ValidateDirective(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {
// field := graphql.GetPathContext(ctx)
// if data, ok := obj.(map[string]interface{}); ok {
// for _, v := range field.ParentField.Field.Arguments {
// name := v.Value.ExpectedType.Name()
// model, err := model.GetInputStruct(name, data)
// if err != nil {
// //handle not found error
// }
// if err := validate.Struct(model); err != nil {
// //handle error
// }
// }
// }
// return next(ctx)
// }
func GetInputStruct(name string, obj map[string]interface{}) (interface{}, error) {
switch name {
{{- range $objectName, $object := .Handler.List.Objects }}
Expand All @@ -20,6 +38,7 @@ func GetInputStruct(name string, obj map[string]interface{}) (interface{}, error
}

{{- range $objectName, $object := .Handler.List.Enums }}
// {{$input2TypeName}} for enum value {{$objectName}}
func (d *{{$objectName}}) {{$input2TypeName}}() {{$objectName}} {
return *d
}
Expand All @@ -28,13 +47,15 @@ func GetInputStruct(name string, obj map[string]interface{}) (interface{}, error
{{- range $objectName, $object := .Handler.List.Objects }}
{{- if $object.HasSqlDirective}}
{{$objectName := $object.Name}}

// {{$objectName}}InputFromMap return a {{$objectName}}Input from data map
// use github.com/mitchellh/mapstructure with reflaction
func {{$objectName}}InputFromMap(data map[string]interface{}) ({{$objectName}}Input, error) {
model := {{$objectName}}Input{}
err := mapstructure.Decode(data, &model);
return model, err
}

// {{$input2TypeName}} returns a map with all values set to {{$objectName}}Patch
func (d *{{$objectName}}Patch) {{$input2TypeName}}() map[string]interface{} {
res := make(map[string]interface{})

Expand Down Expand Up @@ -75,6 +96,7 @@ func GetInputStruct(name string, obj map[string]interface{}) (interface{}, error
return res
}

// {{$input2TypeName}} retuns a {{$objectName}} filled from {{$objectName}}Input
func (d *{{$objectName}}Input) {{$input2TypeName}}() {{$objectName}} {
{{- range $entityKey, $entity := $object.InputEntities }}
{{- $entityGoName := $root.GetGoFieldName $objectName $entity}}
Expand Down
91 changes: 89 additions & 2 deletions inject_source_late.gql.go.tpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{{- $root := .}}

{{- $commentFilterText := "Filter simple datatypes"}}
"""
ID {{ $commentFilterText }}
"""
input IDFilterInput {
and: [ID]
or: [ID]
Expand All @@ -12,6 +15,9 @@ input IDFilterInput {
notin: [ID]
}

"""
String {{ $commentFilterText }}
"""
input StringFilterInput {
and: [String]
or: [String]
Expand All @@ -31,6 +37,9 @@ input StringFilterInput {
notIn: [String]
}

"""
Int {{ $commentFilterText }}
"""
input IntFilterInput {
and: [Int]
or: [Int]
Expand All @@ -48,11 +57,17 @@ input IntFilterInput {
between: IntFilterBetween
}

"""
Filter between start and end (start > value < end)
"""
input IntFilterBetween{
start: Int!
end: Int!
}

"""
Boolean {{ $commentFilterText }}
"""
input BooleanFilterInput{
and: [Boolean]
or: [Boolean]
Expand All @@ -62,6 +77,9 @@ input BooleanFilterInput{
notNull: Boolean
}

"""
Time {{ $commentFilterText }}
"""
input TimeFilterInput {
and: [Time]
or: [Time]
Expand All @@ -78,55 +96,89 @@ input TimeFilterInput {
notIn: [Time]
between: TimeFilterBetween
}

"""
Filter between start and end (start > value < end)
"""
input TimeFilterBetween{
start: Time!
end: Time!
}
{{- range $objectName, $object := $root.List.Objects}}
{{- if $object.HasSqlDirective}}

"""
{{$object.Name}} Input value to add new {{$object.Name}}
"""
input {{$object.Name}}Input {{$object.InputTypeDirectiveGql}}{
{{- range $entityKey, $entity := $object.InputEntities}}
{{$entity.Name}}: {{$entity.GqlType "Input"}}{{$entity.RequiredChar}} {{$entity.InputTypeDirectiveGql}}
{{- end}}
}

"""
{{$object.Name}} Patch value all values are optional to update {{$object.Name}} entities
"""
input {{$object.Name}}Patch {{$object.InputTypeDirectiveGql}}{
{{- range $entityKey, $entity := $object.PatchEntities}}
{{$entity.Name}}: {{$entity.GqlType "Patch"}} {{$entity.InputTypeDirectiveGql}}
{{- end}}
}



"""
Update rules for {{$object.Name}} multiupdates simple possible by global filtervalue
"""
input Update{{$object.Name}}Input{
filter: {{$object.Name}}FiltersInput!
set: {{$object.Name}}Patch!
}

"""
Add{{$object.Name}} result with filterable data and affected rows
"""
type Add{{$object.Name}}Payload{
{{lcFirst $object.Name}}(filter: {{$object.Name}}FiltersInput, order: {{$object.Name}}Order, first: Int, offset: Int, group: [{{$object.Name}}Group!]): {{$object.Name}}QueryResult!
affected: [{{$object.Name}}!]!
}

"""
Update{{$object.Name}} result with filterable data and affected rows
"""
type Update{{$object.Name}}Payload{
{{lcFirst $object.Name}}(filter: {{$object.Name}}FiltersInput, order: {{$object.Name}}Order, first: Int, offset: Int, group: [{{$object.Name}}Group!]): {{$object.Name}}QueryResult!
"""
Count of affected updates
"""
count: Int!
affected: [{{$object.Name}}!]!
}

"""
Delete{{$object.Name}} result with filterable data and count of affected entries
"""
type Delete{{$object.Name}}Payload{
{{lcFirst $object.Name}}(filter: {{$object.Name}}FiltersInput, order: {{$object.Name}}Order, first: Int, offset: Int, group: [{{$object.Name}}Group!]): {{$object.Name}}QueryResult!
"""
Count of deleted {{$object.Name}} entities
"""
count: Int!
msg: String
}

"""
{{$object.Name}} result
"""
type {{$object.Name}}QueryResult{
data: [{{$object.Name}}!]!
count: Int!
totalCount: Int!
}

"""
for {{$object.Name}} a enum of all orderable entities
can be used f.e.: query{{$object.Name}}
"""
enum {{$object.Name}}Orderable {
{{- range $entityKey, $entity := $object.OrderAbleEntities}}
{{$entity.Name}}
Expand All @@ -135,22 +187,39 @@ input TimeFilterBetween{

{{- range $m2mKey, $m2mEntity := $object.Many2ManyRefEntities }}
{{$refType := $root.List.PrimaryEntityOfObject $m2mEntity.GqlTypeName}}
"""
Many 2 many input between {{$object.Name}} and {{$m2mEntity.GqlTypeName}}
Filter to Select {{$object.Name}} and set to set list of {{$m2mEntity.GqlTypeName}} PrimaryKeys
"""
input {{$m2mEntity.GqlTypeName}}Ref2{{$object.Name}}sInput{
filter: {{$object.Name}}FiltersInput!
set: [{{$refType.GqlTypeName}}!]!
}
{{- end}}
"""
Order {{$object.Name}} by asc or desc
"""
input {{$object.Name}}Order{
asc: {{$object.Name}}Orderable
desc: {{$object.Name}}Orderable
}

"""
Groupable data for {{$object.Name}}
Can be used f.e.: by query{{$object.Name}}
"""
enum {{$object.Name}}Group {
{{- range $entityKey, $entity := $object.InputFilterEntities}}
{{- if $entity.IsPrimitive }}
{{$entity.Name}}
{{- end }}
{{- end}}
}

"""
Filter input selection for {{$object.Name}}
Can be used f.e.: by query{{$object.Name}}
"""
input {{$object.Name}}FiltersInput{
{{- range $entityKey, $entity := $object.InputFilterEntities}}
{{- if $entity.IsPrimitive }}
Expand All @@ -174,25 +243,43 @@ input TimeFilterBetween{
{{- if $object.SQLDirective.HasQueries}}
extend type Query {
{{- if $object.SQLDirective.Query.Get}}
"""
return one {{$object.Name}} selected by PrimaryKey(s)
"""
get{{$object.Name}}({{range $entryKey, $entity := $object.PrimaryKeys}}{{$entity.Name}}: {{$entity.GqlType "Patch"}}!, {{end}}): {{$object.Name}} {{ $object.SQLDirectiveValues "query" "Get" | join " "}}
{{- end}}
{{- if $object.SQLDirective.Query.Query}}
"""
return a list of {{$object.Name}} filterable, pageination, orderbale, groupable ...
"""
query{{$object.Name}}(filter: {{$object.Name}}FiltersInput, order: {{$object.Name}}Order, first: Int, offset: Int, group: [{{$object.Name}}Group!] ): {{$object.Name}}QueryResult {{ $object.SQLDirectiveValues "query" "Query" | join " "}}
{{- end}}
}
{{- end}}
{{- if $object.SQLDirective.HasMutation}}
extend type Mutation {
{{- range $m2mKey, $m2mEntity := $object.Many2ManyRefEntities }}
"""
Add new Many2Many relation(s)
"""
add{{$m2mEntity.GqlTypeName}}2{{$object.Name}}s(input:{{$m2mEntity.GqlTypeName}}Ref2{{$object.Name}}sInput!): Update{{$object.Name}}Payload {{ $object.SQLDirectiveValues "mutation" "Add" | join " "}}
{{- end}}
{{- if $object.SQLDirective.Mutation.Add}}
"""
Add new {{$object.Name}}
"""
add{{$object.Name}}(input: [{{$object.Name}}Input!]!): Add{{$object.Name}}Payload {{ $object.SQLDirectiveValues "mutation" "Add" | join " "}}
{{- end}}
{{- if $object.SQLDirective.Mutation.Update}}
"""
update {{$object.Name}} filtered by selection and update all matched values
"""
update{{$object.Name}}(input: Update{{$object.Name}}Input!): Update{{$object.Name}}Payload {{ $object.SQLDirectiveValues "mutation" "Update" | join " "}}
{{- end}}
{{- if $object.SQLDirective.Mutation.Delete}}
"""
delete {{$object.Name}} filtered by selection and delete all matched values
"""
delete{{$object.Name}}(filter: {{$object.Name}}FiltersInput!): Delete{{$object.Name}}Payload {{ $object.SQLDirectiveValues "mutation" "Delete" | join " "}}
{{- end}}
}
Expand Down
Loading

0 comments on commit 0debaf7

Please sign in to comment.