Skip to content

Commit

Permalink
feat(cli): custom enum val directive (#31)
Browse files Browse the repository at this point in the history
* feat(cli): add new custom directive for enum values

* feat(golang): check for custom enum value
  • Loading branch information
Zaba505 committed Mar 21, 2020
1 parent ed30261 commit b4aade8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
20 changes: 20 additions & 0 deletions cmd/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ var gqlcTypes = []*ast.TypeDecl{
}},
}},
},
{
Spec: &ast.TypeDecl_TypeSpec{TypeSpec: &ast.TypeSpec{
Name: &ast.Ident{Name: "as"},
Type: &ast.TypeSpec_Directive{Directive: &ast.DirectiveType{
Locs: []*ast.DirectiveLocation{
{Loc: ast.DirectiveLocation_ENUM_VALUE},
},
Args: &ast.InputValueList{
List: []*ast.InputValue{
{
Name: &ast.Ident{Name: "value"},
Type: &ast.InputValue_Ident{
Ident: &ast.Ident{Name: "String"},
},
},
},
},
}},
}},
},
}

func init() {
Expand Down
17 changes: 16 additions & 1 deletion golang/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,11 @@ func (g *Generator) generateEnum(name string, descr bool, doc *ast.DocGroup, ts
g.P('"', v.Name.Name, '"', ": &graphql.EnumValueConfig{")
g.In()

g.P("Value: \"", v.Name.Name, "\",")
val := getValue(v.Directives)
if val == "" {
val = v.Name.Name
}
g.P("Value: \"", val, "\",")

if v.Doc != nil && descr {
g.printDescr(v.Doc)
Expand Down Expand Up @@ -841,3 +845,14 @@ func getResolver(dirs []*ast.DirectiveLit) string {
}
return ""
}

func getValue(dirs []*ast.DirectiveLit) string {
for _, d := range dirs {
if d.Name != "as" {
continue
}

return strings.Trim(d.Args.Args[0].Value.(*ast.Arg_BasicLit).BasicLit.Value, "\"")
}
return ""
}

0 comments on commit b4aade8

Please sign in to comment.