/
schema.go
93 lines (77 loc) · 1.96 KB
/
schema.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package introspection
import (
"sort"
"strings"
"github.com/vektah/gqlparser/v2/ast"
)
type Schema struct {
schema *ast.Schema
}
func (s *Schema) Description() *string {
if s.schema.Description == "" {
return nil
}
return &s.schema.Description
}
func (s *Schema) Types() []Type {
typeIndex := map[string]Type{}
typeNames := make([]string, 0, len(s.schema.Types))
for _, typ := range s.schema.Types {
if strings.HasPrefix(typ.Name, "__") {
continue
}
typeNames = append(typeNames, typ.Name)
typeIndex[typ.Name] = *WrapTypeFromDef(s.schema, typ)
}
sort.Strings(typeNames)
types := make([]Type, len(typeNames))
for i, t := range typeNames {
types[i] = typeIndex[t]
}
return types
}
func (s *Schema) QueryType() *Type {
return WrapTypeFromDef(s.schema, s.schema.Query)
}
func (s *Schema) MutationType() *Type {
return WrapTypeFromDef(s.schema, s.schema.Mutation)
}
func (s *Schema) SubscriptionType() *Type {
return WrapTypeFromDef(s.schema, s.schema.Subscription)
}
func (s *Schema) Directives() []Directive {
dIndex := map[string]Directive{}
dNames := make([]string, 0, len(s.schema.Directives))
for _, d := range s.schema.Directives {
dNames = append(dNames, d.Name)
dIndex[d.Name] = s.directiveFromDef(d)
}
sort.Strings(dNames)
res := make([]Directive, len(dNames))
for i, d := range dNames {
res[i] = dIndex[d]
}
return res
}
func (s *Schema) directiveFromDef(d *ast.DirectiveDefinition) Directive {
locs := make([]string, len(d.Locations))
for i, loc := range d.Locations {
locs[i] = string(loc)
}
args := make([]InputValue, len(d.Arguments))
for i, arg := range d.Arguments {
args[i] = InputValue{
Name: arg.Name,
description: arg.Description,
DefaultValue: defaultValue(arg.DefaultValue),
Type: WrapTypeFromType(s.schema, arg.Type),
}
}
return Directive{
Name: d.Name,
description: d.Description,
Locations: locs,
Args: args,
IsRepeatable: d.IsRepeatable,
}
}