Skip to content

Commit

Permalink
Provide config to skip generating runtime for a directive
Browse files Browse the repository at this point in the history
  • Loading branch information
lwc committed May 27, 2019
1 parent ba7092c commit 17a82c3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
28 changes: 22 additions & 6 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import (
)

type Config struct {
SchemaFilename StringList `yaml:"schema,omitempty"`
Exec PackageConfig `yaml:"exec"`
Model PackageConfig `yaml:"model"`
Resolver PackageConfig `yaml:"resolver,omitempty"`
Models TypeMap `yaml:"models,omitempty"`
StructTag string `yaml:"struct_tag,omitempty"`
SchemaFilename StringList `yaml:"schema,omitempty"`
Exec PackageConfig `yaml:"exec"`
Model PackageConfig `yaml:"model"`
Resolver PackageConfig `yaml:"resolver,omitempty"`
Models TypeMap `yaml:"models,omitempty"`
StructTag string `yaml:"struct_tag,omitempty"`
Directives map[string]DirectiveConfig `yaml:"directives,omitempty"`
}

var cfgFilenames = []string{".gqlgen.yml", "gqlgen.yml", "gqlgen.yaml"}
Expand All @@ -33,6 +34,17 @@ func DefaultConfig() *Config {
SchemaFilename: StringList{"schema.graphql"},
Model: PackageConfig{Filename: "models_gen.go"},
Exec: PackageConfig{Filename: "generated.go"},
Directives: map[string]DirectiveConfig{
"skip": {
SkipRuntime: true,
},
"include": {
SkipRuntime: true,
},
"deprecated": {
SkipRuntime: true,
},
},
}
}

Expand Down Expand Up @@ -265,6 +277,10 @@ func (tm TypeMap) Add(Name string, goType string) {
tm[Name] = modelCfg
}

type DirectiveConfig struct {
SkipRuntime bool `yaml:"skip_runtime"`
}

func inStrSlice(haystack []string, needle string) bool {
for _, v := range haystack {
if needle == v {
Expand Down
7 changes: 1 addition & 6 deletions codegen/directive.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ func (b *builder) buildDirectives() (map[string]*Directive, error) {
return nil, errors.Errorf("directive with name %s already exists", name)
}

var builtin bool
if name == "skip" || name == "include" || name == "deprecated" {
builtin = true
}

var args []*FieldArgument
for _, arg := range dir.Arguments {
tr, err := b.Binder.TypeReference(arg.Type, nil)
Expand All @@ -55,7 +50,7 @@ func (b *builder) buildDirectives() (map[string]*Directive, error) {
directives[name] = &Directive{
Name: name,
Args: args,
Builtin: builtin,
Builtin: b.Config.Directives[name].SkipRuntime,
}
}

Expand Down

0 comments on commit 17a82c3

Please sign in to comment.