Skip to content

Commit

Permalink
Add Directives to Build
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathew Byrne committed Jul 26, 2018
1 parent 69e790c commit 0924206
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions codegen/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Build struct {
SubscriptionRoot *Object
SchemaRaw string
SchemaFilename string
Directives []*Directive
}

type ModelBuild struct {
Expand Down Expand Up @@ -84,6 +85,7 @@ func (cfg *Config) bind() (*Build, error) {
Imports: imports.finalize(),
SchemaRaw: cfg.SchemaStr,
SchemaFilename: cfg.SchemaFilename,
Directives: cfg.buildDirectives(),
}

if cfg.schema.Query != nil {
Expand Down
11 changes: 11 additions & 0 deletions codegen/directive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package codegen

import "strings"

type Directive struct {
name string
}

func (d *Directive) Name() string {
return strings.Title(d.name)
}
13 changes: 13 additions & 0 deletions codegen/directive_build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package codegen

func (cfg *Config) buildDirectives() (directives []*Directive) {
for name := range cfg.schema.Directives {
if name == "skip" || name == "include" || name == "deprecated" {
continue
}
directives = append(directives, &Directive{
name: name,
})
}
return directives
}
2 changes: 1 addition & 1 deletion codegen/templates/data.go

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

13 changes: 13 additions & 0 deletions codegen/templates/generated.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ func NewExecutableSchema(resolvers ResolverRoot) graphql.ExecutableSchema {
return &executableSchema{resolvers: resolvers}
}

type Config struct {
resolvers ResolverRoot
{{ if .Directives }}directive DirectiveRoot{{ end }}
}

type ResolverRoot interface {
{{- range $object := .Objects -}}
{{ if $object.HasResolvers -}}
Expand All @@ -21,6 +26,14 @@ type ResolverRoot interface {
{{- end }}
}

{{ if .Directives }}
type DirectiveRoot struct {
{{ range $directive := .Directives }}
{{$directive.Name}} graphql.FieldMiddleware
{{ end }}
}
{{ end }}

{{- range $object := .Objects -}}
{{ if $object.HasResolvers }}
type {{$object.GQLType}}Resolver interface {
Expand Down

0 comments on commit 0924206

Please sign in to comment.