aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/99designs/gqlgen/codegen/directive_build.go
blob: 3282884112f12b29453911ce5dfac598c6979cc7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package codegen

import (
	"sort"

	"github.com/pkg/errors"
)

func (cfg *Config) buildDirectives(types NamedTypes) ([]*Directive, error) {
	var directives []*Directive

	for name, dir := range cfg.schema.Directives {
		if name == "skip" || name == "include" || name == "deprecated" {
			continue
		}

		var args []FieldArgument
		for _, arg := range dir.Arguments {
			newArg := FieldArgument{
				GQLName:   arg.Name,
				Type:      types.getType(arg.Type),
				GoVarName: sanitizeArgName(arg.Name),
			}

			if !newArg.Type.IsInput && !newArg.Type.IsScalar {
				return nil, errors.Errorf("%s cannot be used as argument of directive %s(%s) only input and scalar types are allowed", arg.Type, dir.Name, arg.Name)
			}

			if arg.DefaultValue != nil {
				var err error
				newArg.Default, err = arg.DefaultValue.Value(nil)
				if err != nil {
					return nil, errors.Errorf("default value for directive argument %s(%s) is not valid: %s", dir.Name, arg.Name, err.Error())
				}
				newArg.StripPtr()
			}
			args = append(args, newArg)
		}

		directives = append(directives, &Directive{
			Name: name,
			Args: args,
		})
	}

	sort.Slice(directives, func(i, j int) bool { return directives[i].Name < directives[j].Name })

	return directives, nil
}