-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathintrospection.go
112 lines (97 loc) · 3.21 KB
/
introspection.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package gqlfetch
import (
"encoding/json"
"log"
"github.com/vektah/gqlparser/v2/ast"
)
type introspectionResults struct {
Errors []struct {
Message string `json:"message"`
} `json:"errors"`
Data struct {
Schema introspectionSchema `json:"__schema"`
} `json:"data"`
}
type introspectionSchema struct {
QueryType ast.Definition `json:"queryType"`
MutationType ast.Definition `json:"mutationType"`
Types []introspectionTypeDefinition `json:"types"`
Directives []introspectionDirectiveDefinition `json:"directives"`
}
type introspectionTypeDefinition struct {
Kind ast.DefinitionKind `json:"kind"`
Name string `json:"name"`
Description string `json:"description"`
Fields []introspectedTypeField `json:"fields"`
InputFields []introspectionInputField `json:"inputFields"`
Interfaces []ast.Definition `json:"interfaces"`
EnumValues json.RawMessage `json:"enumValues"`
PossibleTypes json.RawMessage `json:"possibleTypes"`
}
type introspectedTypeField struct {
Name string `json:"name"`
Description string `json:"description"`
Args []introspectionInputField `json:"args"`
Type *introspectedType `json:"type"`
IsDeprecated bool `json:"isDeprecated"`
DeprecationReason interface{} `json:"deprecationReason"`
}
type introspectionDirectiveDefinition struct {
Name string `json:"name"`
Description string `json:"description"`
Locations []ast.DirectiveLocation `json:"locations"`
Args []struct {
Name string `json:"name"`
Description string `json:"description"`
Type *introspectedType `json:"type"`
DefaultValue interface{} `json:"defaultValue"`
} `json:"args"`
}
type introspectionInputField struct {
Name string `json:"name"`
Description string `json:"description"`
Type *introspectedType `json:"type"`
DefaultValue interface{} `json:"defaultValue"`
}
type introspectedType struct {
Kind introspectionTypeKind `json:"kind"`
Name *string `json:"name"`
OfType *introspectedType `json:"ofType"`
}
type introspectionTypeKind string
const (
NON_NULL introspectionTypeKind = "NON_NULL"
LIST introspectionTypeKind = "LIST"
OBJECT introspectionTypeKind = "OBJECT"
)
func introspectionTypeToAstType(typ *introspectedType) *ast.Type {
var res ast.Type
if typ.OfType == nil {
res.NamedType = *typ.Name
return &res
}
switch typ.Kind {
case NON_NULL:
res = *introspectionTypeToAstType(typ.OfType)
res.NonNull = true
return &res
case LIST:
res.Elem = introspectionTypeToAstType(typ.OfType)
return &res
default:
log.Fatalf("type kind unknown: %s", typ.Kind)
return nil
}
}
var (
excludeScalarTypes = []string{"ID", "Int", "String", "Float", "Boolean"}
excludeDirectives = []string{"deprecated", "include", "skip"}
)
func containsStr(needle string, hay []string) bool {
for _, s := range hay {
if needle == s {
return true
}
}
return false
}