65 lines
1.6 KiB
Go
65 lines
1.6 KiB
Go
// https://github.com/99designs/gqlgen/issues/2281#issuecomment-1506561381
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/99designs/gqlgen/api"
|
|
"github.com/99designs/gqlgen/codegen"
|
|
"github.com/99designs/gqlgen/codegen/config"
|
|
)
|
|
|
|
type fieldDirectiveFix struct {
|
|
}
|
|
|
|
func (fieldDirectiveFix) Name() string {
|
|
return "Fix Directive hook called with wrong object"
|
|
}
|
|
|
|
func (fieldDirectiveFix) GenerateCode(cfg *codegen.Data) error {
|
|
for _, input := range cfg.Inputs {
|
|
for _, field := range input.Fields {
|
|
if field.GoFieldType == codegen.GoFieldVariable {
|
|
directiveMap := make(map[string]int, len(field.TypeReference.Definition.Directives)+len(field.Object.Directives))
|
|
for _, v := range field.TypeReference.Definition.Directives {
|
|
directiveMap[v.Name]++
|
|
}
|
|
// for _, v := range field.Object.Directives {
|
|
// directiveMap[v.Name]++
|
|
// }
|
|
|
|
directive := make([]*codegen.Directive, 0, len(field.Directives))
|
|
for _, v := range field.Directives {
|
|
if count := directiveMap[v.Name]; count > 0 {
|
|
directiveMap[v.Name] = count - 1
|
|
fmt.Printf("Ignore field %s{%s} directive: @%s\n", input.Name, field.Name, v.Name)
|
|
continue
|
|
}
|
|
|
|
directive = append(directive, v)
|
|
}
|
|
|
|
field.Directives = directive
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func main() {
|
|
cfg, err := config.LoadConfigFromDefaultLocations()
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "failed to load config", err.Error())
|
|
os.Exit(2)
|
|
}
|
|
|
|
err = api.Generate(cfg,
|
|
api.AddPlugin(&fieldDirectiveFix{}),
|
|
)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
|
os.Exit(3)
|
|
}
|
|
}
|