Skip to content

Commit

Permalink
consider goField directive for getters generation
Browse files Browse the repository at this point in the history
  • Loading branch information
darkLord19 committed Nov 28, 2022
1 parent f28ffcc commit ac6ae2f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 21 deletions.
2 changes: 1 addition & 1 deletion codegen/testserver/followschema/models-gen.go

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

2 changes: 1 addition & 1 deletion codegen/testserver/singlefile/models-gen.go

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

41 changes: 22 additions & 19 deletions graphql/executable_schema_mock.go

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

19 changes: 19 additions & 0 deletions plugin/modelgen/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ type FieldMutateHook = func(td *ast.Definition, fd *ast.FieldDefinition, f *Fiel

// defaultFieldMutateHook is the default hook for the Plugin which applies the GoTagFieldHook.
func defaultFieldMutateHook(td *ast.Definition, fd *ast.FieldDefinition, f *Field) (*Field, error) {
var err error
f, err = GoFieldHook(td, fd, f)
if err != nil {
return f, err
}
return GoTagFieldHook(td, fd, f)
}

Expand Down Expand Up @@ -412,6 +417,20 @@ func GoTagFieldHook(td *ast.Definition, fd *ast.FieldDefinition, f *Field) (*Fie
return f, nil
}

// GoFieldHook applies the goField directive to the generated Field f.
func GoFieldHook(td *ast.Definition, fd *ast.FieldDefinition, f *Field) (*Field, error) {
args := make([]string, 0)
_ = args
for _, goField := range fd.Directives.ForNames("goField") {
if arg := goField.Arguments.ForName("name"); arg != nil {
if k, err := arg.Value.Value(nil); err == nil {
f.GoName = k.(string)
}
}
}
return f, nil
}

func isStruct(t types.Type) bool {
_, is := t.Underlying().(*types.Struct)
return is
Expand Down
13 changes: 13 additions & 0 deletions plugin/modelgen/out/generated.go

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

14 changes: 14 additions & 0 deletions plugin/modelgen/testdata/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ directive @goTag(
value: String
) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION

directive @goField(
forceResolver: Boolean
name: String
) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION | INTERFACE

type Query {
thisShoudlntGetGenerated: Boolean
}
Expand Down Expand Up @@ -178,4 +183,13 @@ interface ArrayOfA {
type ImplArrayOfA implements ArrayOfA {
trickyField: [CDImplemented!]!
trickyFieldPointer: [CDImplemented]
}

interface X {
Id: String! @goField(name: "Id")
}

type Xer implements X {
Id: String! @goField(name: "Id")
Name: String!
}

0 comments on commit ac6ae2f

Please sign in to comment.