This repository has been archived by the owner on May 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtypes.go
186 lines (153 loc) · 4.77 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
package graphql
import "encoding/json"
// OperationType is either "query" or "mutation"
// Queries are reads and mutations cause side-effects.
type OperationType string
const (
// OperationQuery is a read operation.
OperationQuery OperationType = "query"
// OperationMutation is a mutation.
OperationMutation OperationType = "mutation"
)
// Document is the top-level representation of a string in GraphQL.
type Document struct {
Operations []Operation
FragmentDefinitions []FragmentDefinition `json:",omitempty"`
EnumDefinitions []EnumDefinition `json:",omitempty"`
TypeDefinitions []TypeDefinition `json:",omitempty"`
TypeExtensions []TypeExtension `json:",omitempty"`
}
// Operation is either a read or mutation in GraphQL.
type Operation struct {
Type OperationType `json:",omitempty"`
Name string `json:",omitempty"`
SelectionSet SelectionSet `json:",omitempty"`
VariableDefinitions []VariableDefinition `json:",omitempty"`
Directives []Directive `json:",omitempty"`
}
func (o *Operation) String() string {
j, _ := json.Marshal(o)
return string(j)
}
// A Selection is either a Field, a FragmentSpread, or an InlineFragment
type Selection struct {
Field *Field `json:",omitempty"`
FragmentSpread *FragmentSpread `json:",omitempty"`
InlineFragment *InlineFragment `json:",omitempty"`
}
func (s *Selection) String() string {
j, _ := json.Marshal(s)
return string(j)
}
// A Field is one of the most important concepts in GraphQL. Fields specify what
// parts of data you would like to select.
type Field struct {
Name string `json:",omitempty"`
Arguments Arguments `json:",omitempty"`
SelectionSet SelectionSet `json:",omitempty"`
Alias string `json:",omitempty"`
Directives []Directive `json:",omitempty"`
}
// FragmentSpread is a reference to a QueryFragment elsewhere in an Operation.
type FragmentSpread struct {
Name string `json:",omitempty"`
Directives []Directive `json:",omitempty"`
}
// InlineFragment is used in-line to apply a type condition within a selection.
type InlineFragment struct {
TypeCondition string `json:",omitempty"`
Directives []Directive `json:",omitempty"`
SelectionSet SelectionSet
}
// Argument is an argument to a Field Call.
type Argument struct {
Name string
Value interface{}
}
// Arguments is a collection of Argument values
type Arguments []Argument
// Get is a helper to fetch a particular argument by name.
func (a Arguments) Get(name string) (interface{}, bool) {
for _, arg := range a {
if arg.Name == name {
return arg.Value, true
}
}
return nil, false
}
// SelectionSet is a collection of Selection
type SelectionSet []Selection
// Fragments
// FragmentDefinition defines a Query Fragment
type FragmentDefinition struct {
Name string
TypeCondition string
SelectionSet SelectionSet
Directives []Directive `json:",omitempty"`
}
// Type system
// TypeDefinition defines a type.
type TypeDefinition struct {
Name string
Interfaces []Interface `json:",omitempty"`
FieldDefinitions []FieldDefinition
}
// TypeExtension extends an existing type.
type TypeExtension struct {
Name string
Interfaces []Interface `json:",omitempty"`
FieldDefinitions []FieldDefinition
}
// FieldDefinition defines a fields on a type.
type FieldDefinition struct {
Name string
Type Type
ArgumentDefinitions []ArgumentDefinition `json:",omitempty"`
}
// ArgumentDefinition defines an argument for a field on a type.
type ArgumentDefinition struct {
Name string
Type Type
DefaultValue *Value `json:",omitempty"`
}
// Type describes an argument's type.
type Type struct {
Name string
Optional bool
Params []Type `json:",omitempty"`
}
// Value refers to a value
type Value interface{}
// Interface descibes a set of methods a type must conform to to satisfy it.
// TODO
type Interface struct{}
// Enums
// EnumDefinition defines an enum.
type EnumDefinition struct {
Name string
Values []string
}
// EnumValue describes a possible value for an enum.
type EnumValue struct {
EnumTypeName string
Value string
}
// Variables
// VariableDefinition defines a variable for an Operation.
type VariableDefinition struct {
Variable Variable
Type Type
DefaultValue *Value `json:",omitempty"`
}
// Variable describes a reference to a variable.
type Variable struct {
Name string
PropertySelection *Variable `json:",omitempty"`
}
// Directives
// Directive describes a directive which can alter behavior in different parts of a GraphQL Operation.
type Directive struct {
Name string
Type *Type `json:",omitempty"`
Value *Value `json:",omitempty"`
}