Skip to content

Commit

Permalink
Add ReplacePlugin option to replace a specific plugin (#1657)
Browse files Browse the repository at this point in the history
* Add Helper Option for replacing plugins

* Update recipe to use ReplacePlugin instead of NoPlugin and AddPlugin

* fix linting issue on comment
  • Loading branch information
tprebs committed Oct 13, 2021
1 parent f8c4660 commit f6c35be
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 11 deletions.
20 changes: 20 additions & 0 deletions api/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,23 @@ func AddPlugin(p plugin.Plugin) Option {
*plugins = append(*plugins, p)
}
}

// ReplacePlugin replaces any existing plugin with a matching plugin name
func ReplacePlugin(p plugin.Plugin) Option {
return func(cfg *config.Config, plugins *[]plugin.Plugin) {
if plugins != nil {
found := false
ps := *plugins
for i, o := range ps {
if p.Name() == o.Name() {
ps[i] = p
found = true
}
}
if !found {
ps = append(ps, p)
}
*plugins = ps
}
}
}
58 changes: 58 additions & 0 deletions api/option_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package api

import (
"testing"

"github.com/99designs/gqlgen/codegen/config"
"github.com/99designs/gqlgen/plugin"
"github.com/99designs/gqlgen/plugin/federation"
"github.com/99designs/gqlgen/plugin/modelgen"
"github.com/99designs/gqlgen/plugin/resolvergen"
"github.com/stretchr/testify/require"
)

type testPlugin struct {
}

// Name returns the plugin name
func (t *testPlugin) Name() string {
return "modelgen"
}

// MutateConfig mutates the configuration
func (t *testPlugin) MutateConfig(_ *config.Config) error {
return nil
}

func TestReplacePlugin(t *testing.T) {

t.Run("replace plugin if exists", func(t *testing.T) {
pg := []plugin.Plugin{
federation.New(),
modelgen.New(),
resolvergen.New(),
}

expectedPlugin := &testPlugin{}
ReplacePlugin(expectedPlugin)(config.DefaultConfig(), &pg)

require.EqualValues(t, federation.New(), pg[0])
require.EqualValues(t, expectedPlugin, pg[1])
require.EqualValues(t, resolvergen.New(), pg[2])
})

t.Run("add plugin if doesn't exist", func(t *testing.T) {
pg := []plugin.Plugin{
federation.New(),
resolvergen.New(),
}

expectedPlugin := &testPlugin{}
ReplacePlugin(expectedPlugin)(config.DefaultConfig(), &pg)

require.EqualValues(t, federation.New(), pg[0])
require.EqualValues(t, resolvergen.New(), pg[1])
require.EqualValues(t, expectedPlugin, pg[2])
})

}
18 changes: 7 additions & 11 deletions docs/content/recipes/modelgen-hook.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ func main() {
MutateHook: mutateHook,
}

err = api.Generate(cfg,
api.NoPlugins(),
api.AddPlugin(&p),
)
err = api.Generate(cfg, api.ReplacePlugin(&p))

if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(3)
Expand Down Expand Up @@ -82,7 +80,7 @@ type Object struct {

## FieldMutateHook

For more fine grained control over model generation, a graphql schema aware a FieldHook can be provided. This hook has access to type and field graphql definitions enabling the hook to modify the `modelgen.Field` using directives defined within the schema.
For more fine grained control over model generation, a graphql schema aware a FieldHook can be provided. This hook has access to type and field graphql definitions enabling the hook to modify the `modelgen.Field` using directives defined within the schema.

The below recipe uses this feature to add validate tags to the generated model for use with `go-playground/validator` where the validate tags are defined in a constraint directive in the schema.

Expand All @@ -103,11 +101,11 @@ func constraintFieldHook(td *ast.Definition, fd *ast.FieldDefinition, f *modelge
c := fd.Directives.ForName("constraint")
if c != nil {
formatConstraint := c.Arguments.ForName("format")

if formatConstraint != nil{
f.Tag += " validate:"+formatConstraint.Value.String()
}

}

return f, nil
Expand All @@ -125,10 +123,8 @@ func main() {
FieldHook: constraintFieldHook,
}

err = api.Generate(cfg,
api.NoPlugins(),
api.AddPlugin(&p),
)
err = api.Generate(cfg, api.ReplacePlugin(&p))

if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(3)
Expand Down

0 comments on commit f6c35be

Please sign in to comment.