/
nodes.cr
274 lines (228 loc) · 8.22 KB
/
nodes.cr
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# coding: utf-8
require "./ast"
require "./generation"
require "../types/object_type"
module GraphQL
module Language
macro define_array_cast(type)
def self.to_{{type.id.downcase}}(value : Array) : {{type.id}}
_values = [] of {{type.id}}
value.each do |val|
_values << to_{{type.id.downcase}}(val)
end
_values
end
def self.to_{{type.id.downcase}}(value) {{type.id}}
value.as({{type.id}})
end
def self.to_fvalue(v : NullValue): Nil
nil
end
def self.to_argumentvalue(v : NullValue): Nil
nil
end
end
class AbstractNode < ASTNode
# this works only if the module
# gets included in the class exactly
# at this file
include GraphQL::ObjectType
# getter :line, :col
# @col : Int32
# @line : Int32
#
# def initialize(**options)
# position_source = options.fetch(:position_source, nil)
# if position_source
# # @line, @col = position_source.as(Array(Int32)).line_and_column
# end
# super()
# end
end
# This is the AST root for normal queries
#
# @example Deriving a document by parsing a string
# document = GraphQL.parse(query_string)
#
# @example Creating a string from a document
# document.to_query_string
# # { ... }
#
class Document < AbstractNode
values({definitions: Array(
OperationDefinition | FragmentDefinition | SchemaDefinition | ObjectTypeDefinition | InputObjectTypeDefinition |
ScalarTypeDefinition | DirectiveDefinition | EnumTypeDefinition | InterfaceTypeDefinition | UnionTypeDefinition
)})
traverse :children, :definitions
def to_query_string
GraphQL::Language::Generation.generate(self)
end
# def slice_definition(name)
# GraphQL::Language::DefinitionSlice.slice(self, name)
# end
end
class SchemaDefinition < AbstractNode
values({query: String, mutation: String?, subscription: String?})
end
# A query, mutation or subscription.
# May be anonymous or named.
# May be explicitly typed (eg `mutation { ... }`) or implicitly a query (eg `{ ... }`).
class OperationDefinition < AbstractNode
values(
{
operation_type: String,
name: String?,
variables: Array(VariableDefinition),
directives: Array(Directive),
selections: Array(Selection),
}
)
traverse :children, :variables, :directives, :selections
end
class DirectiveDefinition < AbstractNode
values({name: String, arguments: Array(InputValueDefinition), locations: Array(String), description: String?})
traverse :children, :arguments
end
class Directive < AbstractNode
values({name: String, arguments: Array(Argument)})
traverse :children, :arguments
end
alias FValue = String | Int32 | Float64 | Bool | Nil | AEnum | InputObject | Array(FValue) | Hash(String, FValue)
define_array_cast(FValue)
alias Type = TypeName | NonNullType | ListType
alias Selection = Field | FragmentSpread | InlineFragment
class VariableDefinition < AbstractNode
values({name: String, type: Type, default_value: FValue})
traverse :children, :type
end
alias ArgumentValue = FValue | InputObject | VariableIdentifier | Array(ArgumentValue) | ReturnType
define_array_cast(ArgumentValue)
class Argument < AbstractNode
values({name: String, value: ArgumentValue})
def to_value
value
end
end
class TypeDefinition < AbstractNode
values({name: String, description: String?})
end
class ScalarTypeDefinition < TypeDefinition
values({directives: Array(Directive)})
traverse :children, :directives
end
class ObjectTypeDefinition < TypeDefinition
values(
{interfaces: Array(String),
fields: Array(FieldDefinition),
directives: Array(Directive)}
)
traverse :children, :fields, :directives
end
class InputObjectTypeDefinition < TypeDefinition
values({fields: Array(InputValueDefinition), directives: Array(Directive)})
traverse :children, :fields, :directives
end
class InputValueDefinition < AbstractNode
values({name: String, type: Type, default_value: FValue, directives: Array(Directive), description: String?})
traverse :children, :type, :directives
end
# Base class for nodes whose only value is a name (no child nodes or other scalars)
class NameOnlyNode < AbstractNode
values({name: String})
end
# Base class for non-null type names and list type names
class WrapperType < AbstractNode
values({of_type: (Type)})
traverse :children, :of_type
end
# A type name, used for variable definitions
class TypeName < NameOnlyNode; end
# A list type definition, denoted with `[...]` (used for variable type definitions)
class ListType < WrapperType; end
# A collection of key-value inputs which may be a field argument
class InputObject < AbstractNode
values({arguments: Array(Argument)})
traverse :children, :arguments
# @return [Hash<String, Any>] Recursively turn this input object into a Ruby Hash
def to_h
arguments.reduce({} of String => FValue) do |memo, pair|
v = pair.value
memo[pair.name] = case v
when InputObject
v.to_h
when Array
v.map { |v| v.as(FValue) }
else
v
end.as(FValue)
memo
end
end
def to_value
to_h
end
end
# A non-null type definition, denoted with `...!` (used for variable type definitions)
class NonNullType < WrapperType; end
# An enum value. The string is available as {#name}.
class AEnum < NameOnlyNode
def to_value
name
end
end
# A null value literal.
class NullValue < NameOnlyNode; end
class VariableIdentifier < NameOnlyNode; end
# A single selection in a
# A single selection in a GraphQL query.
class Field < AbstractNode
values({
name: String,
_alias: String?,
arguments: Array(Argument),
directives: Array(Directive),
selections: Array(Selection),
})
traverse :children, :arguments, :directives, :selections
end
class FragmentDefinition < AbstractNode
values({
name: String?,
type: Type,
directives: Array(Directive),
selections: Array(Selection),
})
traverse :children, :type, :directives, :selections
end
class FieldDefinition < AbstractNode
values({name: String, arguments: Array(InputValueDefinition), type: Type, directives: Array(Directive), description: String?})
traverse :children, :type, :arguments, :directives
end
class InterfaceTypeDefinition < TypeDefinition
values({fields: Array(FieldDefinition), directives: Array(Directive)})
traverse :children, :fields, :directives
end
class UnionTypeDefinition < TypeDefinition
values({types: Array(TypeName), directives: Array(Directive)})
traverse :children, :types, :directives
end
class EnumTypeDefinition < TypeDefinition
values({fvalues: Array(EnumValueDefinition), directives: Array(Directive)})
traverse :children, :directives
end
# Application of a named fragment in a selection
class FragmentSpread < AbstractNode
values({name: String, directives: Array(Directive)})
traverse :children, :directives
end
# An unnamed fragment, defined directly in the query with `... { }`
class InlineFragment < AbstractNode
values({type: Type?, directives: Array(Directive), selections: Array(Selection)})
traverse :children, :type, :directives, :selections
end
class EnumValueDefinition < AbstractNode
values({name: String, directives: Array(Directive), selection: Array(Selection)?, description: String?})
traverse :children, :directives
end
end
end