Skip to content

Commit

Permalink
use goField directive for getters generation (#2447)
Browse files Browse the repository at this point in the history
* consider goField directive for getters generation

* Re-generate to pass linting

Signed-off-by: Steve Coffman <steve@khanacademy.org>

Signed-off-by: Steve Coffman <steve@khanacademy.org>
Co-authored-by: Umang Parmar <umangjparmar@gmail.com>
  • Loading branch information
StevenACoffman and darkLord19 authored Dec 3, 2022
1 parent 463d213 commit 5c083c7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
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 5c083c7

Please sign in to comment.