github.com/niko0xdev/gqlgen@v0.17.55-0.20240120102243-2ecff98c3e37/api/option.go (about)

     1  package api
     2  
     3  import (
     4  	"github.com/niko0xdev/gqlgen/codegen/config"
     5  	"github.com/niko0xdev/gqlgen/plugin"
     6  )
     7  
     8  type Option func(cfg *config.Config, plugins *[]plugin.Plugin)
     9  
    10  func NoPlugins() Option {
    11  	return func(cfg *config.Config, plugins *[]plugin.Plugin) {
    12  		*plugins = nil
    13  	}
    14  }
    15  
    16  func AddPlugin(p plugin.Plugin) Option {
    17  	return func(cfg *config.Config, plugins *[]plugin.Plugin) {
    18  		*plugins = append(*plugins, p)
    19  	}
    20  }
    21  
    22  // PrependPlugin prepends plugin any existing plugins
    23  func PrependPlugin(p plugin.Plugin) Option {
    24  	return func(cfg *config.Config, plugins *[]plugin.Plugin) {
    25  		*plugins = append([]plugin.Plugin{p}, *plugins...)
    26  	}
    27  }
    28  
    29  // ReplacePlugin replaces any existing plugin with a matching plugin name
    30  func ReplacePlugin(p plugin.Plugin) Option {
    31  	return func(cfg *config.Config, plugins *[]plugin.Plugin) {
    32  		if plugins != nil {
    33  			found := false
    34  			ps := *plugins
    35  			for i, o := range ps {
    36  				if p.Name() == o.Name() {
    37  					ps[i] = p
    38  					found = true
    39  				}
    40  			}
    41  			if !found {
    42  				ps = append(ps, p)
    43  			}
    44  			*plugins = ps
    45  		}
    46  	}
    47  }