forked from samsarahq/thunder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
190 lines (156 loc) · 4.2 KB
/
types.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package graphql
import (
"context"
"fmt"
)
// Type represents a GraphQL type, and should be either an Object, a Scalar,
// or a List
type Type interface {
String() string
// isType() is a no-op used to tag the known values of Type, to prevent
// arbitrary interface{} from implementing Type
isType()
}
// Scalar is a leaf value. A custom "Unwrapper" can be attached to the scalar
// so it can have a custom unwrapping (if nil we will use the default unwrapper).
type Scalar struct {
Type string
Unwrapper func(interface{}) (interface{}, error)
}
func (s *Scalar) isType() {}
func (s *Scalar) String() string {
return s.Type
}
// Enum is a leaf value
type Enum struct {
Type string
Values []string
ReverseMap map[interface{}]string
}
func (e *Enum) isType() {}
func (e *Enum) String() string {
return e.Type
}
func (e *Enum) enumValues() []string {
return e.Values
}
// Object is a value with several fields
type Object struct {
Name string
Description string
Key Resolver
Fields map[string]*Field
}
func (o *Object) isType() {}
func (o *Object) String() string {
return o.Name
}
// List is a collection of other values
type List struct {
Type Type
}
func (l *List) isType() {}
func (l *List) String() string {
return fmt.Sprintf("[%s]", l.Type)
}
type InputObject struct {
Name string
InputFields map[string]Type
}
func (io *InputObject) isType() {}
func (io *InputObject) String() string {
return io.Name
}
// NonNull is a non-nullable other value
type NonNull struct {
Type Type
}
func (n *NonNull) isType() {}
func (n *NonNull) String() string {
return fmt.Sprintf("%s!", n.Type)
}
// Union is a option between multiple types
type Union struct {
Name string
Description string
Types map[string]*Object
}
func (*Union) isType() {}
func (u *Union) String() string {
return u.Name
}
// Verify *Scalar, *Object, *List, *InputObject, and *NonNull implement Type
var _ Type = &Scalar{}
var _ Type = &Object{}
var _ Type = &List{}
var _ Type = &InputObject{}
var _ Type = &NonNull{}
var _ Type = &Enum{}
var _ Type = &Union{}
// A Resolver calculates the value of a field of an object
type Resolver func(ctx context.Context, source, args interface{}, selectionSet *SelectionSet) (interface{}, error)
// Field knows how to compute field values of an Object
//
// Fields are responsible for computing their value themselves.
type Field struct {
Resolve Resolver
Type Type
Args map[string]Type
ParseArguments func(json interface{}) (interface{}, error)
Expensive bool
}
type Schema struct {
Query Type
Mutation Type
}
// SelectionSet represents a core GraphQL query
//
// A SelectionSet can contain multiple fields and multiple fragments. For
// example, the query
//
// {
// name
// ... UserFragment
// memberships {
// organization { name }
// }
// }
//
// results in a root SelectionSet with two selections (name and memberships),
// and one fragment (UserFragment). The subselection `organization { name }`
// is stored in the memberships selection.
//
// Because GraphQL allows multiple fragments with the same name or alias,
// selections are stored in an array instead of a map.
type SelectionSet struct {
Selections []*Selection
Fragments []*Fragment
}
// A selection represents a part of a GraphQL query
//
// The selection
//
// me: user(id: 166) { name }
//
// has name "user" (representing the source field to be queried), alias "me"
// (representing the name to be used in the output), args id: 166 (representing
// arguments passed to the source field to be queried), and subselection name
// representing the information to be queried from the resulting object.
type Selection struct {
Name string
Alias string
Args interface{}
SelectionSet *SelectionSet
// The parsed flag is used to make sure the args for this Selection are only
// parsed once.
parsed bool
}
// A Fragment represents a reusable part of a GraphQL query
//
// The On part of a Fragment represents the type of source object for which
// this Fragment should be used. That is not currently implemented in this
// package.
type Fragment struct {
On string
SelectionSet *SelectionSet
}