Skip to content

Commit

Permalink
feat(graphql): support directives (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianccm committed Dec 1, 2022
1 parent a6b9ed5 commit 05fb210
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 199 deletions.
26 changes: 26 additions & 0 deletions command.go
Expand Up @@ -202,6 +202,15 @@ func (m *plugin) GenerateCode(data *codegen.Data) error {
"gmethod": func(from, to string) string {
return m.types.resolver[from][to][2]
},
"gdpkg": func(name string) string {
return m.types.directives["@"+name][0]
},
"gdtyp": func(name string) string {
return m.types.directives["@"+name][1]
},
"gdmethod": func(name string) string {
return m.types.directives["@"+name][2]
},
},
PackageDoc: "//+build !graphql\n",
Template: `
Expand Down Expand Up @@ -230,6 +239,9 @@ type {{$root.TypeName}} struct {
{{lcFirst $root.TypeName}}{{$object.Name}} *{{lcFirst $root.TypeName}}{{$object.Name}}
{{- end }}
{{- end }}
{{ range $directive := .AllDirectives }}
{{$directive.Name}}Resolver *{{lookupImport (gdpkg $directive.Name)}}.{{gdtyp $directive.Name}}
{{- end }}
}
func (r *{{$root.TypeName}}) Inject (
Expand All @@ -238,12 +250,26 @@ func (r *{{$root.TypeName}}) Inject (
{{lcFirst $root.TypeName}}{{$object.Name}} *{{lcFirst $root.TypeName}}{{$object.Name}},
{{- end }}
{{- end }}
{{ range $directive := .AllDirectives }}
{{$directive.Name}}Resolver *{{lookupImport (gdpkg $directive.Name)}}.{{gdtyp $directive.Name}},
{{- end }}
) {
{{- range $object := .Objects }}
{{- if $object.HasResolvers }}
r.{{lcFirst $root.TypeName}}{{$object.Name}} = {{lcFirst $root.TypeName}}{{$object.Name}}
{{- end }}
{{- end }}
{{ range $directive := .AllDirectives }}
r.{{$directive.Name}}Resolver = {{$directive.Name}}Resolver
{{- end }}
}
func (r *{{$root.TypeName}}) directives() DirectiveRoot {
return DirectiveRoot{
{{ range $directive := .AllDirectives -}}
{{ucFirst $directive.Name}}: r.{{$directive.Name}}Resolver.{{gdmethod $directive.Name}},
{{end}}
}
}
{{ range $object := .Objects -}}
Expand Down
44 changes: 42 additions & 2 deletions example/graphql/generated.go

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

2 changes: 1 addition & 1 deletion example/graphql/module.go

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

22 changes: 17 additions & 5 deletions example/graphql/resolver.go

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

Expand Up @@ -4,7 +4,9 @@ type User {
attributes: User_Attributes!
}

type User_Attributes {
directive @userAttributeFilter(prefix: String!) on OBJECT

type User_Attributes @userAttributeFilter(prefix: "secret") {
keys: [String!]!
get(key: String!): String!
}
Expand Down
3 changes: 2 additions & 1 deletion example/user/infrastructure/user.go
Expand Up @@ -15,7 +15,8 @@ func (us *UserServiceImpl) UserByID(_ context.Context, id string) (*domain.User,
Name: "User " + id,
Nicknames: []string{"nick", id},
Attributes: map[string]interface{}{
"movie": "starwars",
"movie": "starwars",
"secret_crush": "r2d2",
},
}, nil
}
26 changes: 26 additions & 0 deletions example/user/interfaces/graphql/resolver.go
Expand Up @@ -2,10 +2,15 @@ package graphql

import (
"context"
"errors"
"strings"

"flamingo.me/graphql/example/user/domain"
"github.com/99designs/gqlgen/graphql"
)

var errNoAttributeReturned = errors.New("no attributes returned")

// UserQueryResolver resolver for the user service
type UserQueryResolver struct {
userService domain.UserService
Expand All @@ -21,3 +26,24 @@ func (r *UserQueryResolver) Inject(userService domain.UserService) *UserQueryRes
func (r *UserQueryResolver) User(ctx context.Context, id string) (*domain.User, error) {
return r.userService.UserByID(ctx, id)
}

// UserAttributeFilter directive
func (r *UserQueryResolver) UserAttributeFilter(ctx context.Context, obj interface{}, next graphql.Resolver, prefix string) (res interface{}, err error) {
rawAttributes, err := next(ctx)
if err != nil {
return nil, err
}

attributes, ok := rawAttributes.(domain.Attributes)
if !ok {
return nil, errNoAttributeReturned
}

for k := range attributes {
if strings.HasPrefix(k, prefix) {
delete(attributes, k)
}
}

return attributes, nil
}
4 changes: 3 additions & 1 deletion example/user/interfaces/graphql/schema.graphql
Expand Up @@ -4,7 +4,9 @@ type User {
attributes: User_Attributes!
}

type User_Attributes {
directive @userAttributeFilter(prefix: String!) on OBJECT

type User_Attributes @userAttributeFilter(prefix: "secret") {
keys: [String!]!
get(key: String!): String!
}
Expand Down
3 changes: 2 additions & 1 deletion example/user/interfaces/graphql/service.go
@@ -1,7 +1,6 @@
package graphql

import (
// embed schema.grapqhl
_ "embed"

"flamingo.me/graphql"
Expand All @@ -24,4 +23,6 @@ func (*Service) Types(types *graphql.Types) {
types.Map("User", domain.User{})
types.Map("User_Attributes", domain.Attributes{})
types.Resolve("Query", "User", UserQueryResolver{}, "User")

types.Directive("@userAttributeFilter", UserQueryResolver{}, "UserAttributeFilter")
}
5 changes: 0 additions & 5 deletions go.mod
Expand Up @@ -17,7 +17,6 @@ require (
cuelang.org/go v0.0.15 // indirect
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cockroachdb/apd/v2 v2.0.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
Expand All @@ -27,15 +26,11 @@ require (
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gomodule/redigo v2.0.0+incompatible // indirect
github.com/gorilla/securecookie v1.1.1 // indirect
github.com/gorilla/sessions v1.2.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/labstack/gommon v0.0.0-20180613044413-d6898124de91 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de // indirect
Expand Down

0 comments on commit 05fb210

Please sign in to comment.