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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
{-# LANGUAGE OverloadedStrings #-}
module Data.GraphQL.Parser where
import Prelude hiding (takeWhile)
import Control.Applicative (Alternative, (<|>), empty, many)
import Data.Char
import Data.Attoparsec.Text
( Parser
, (<?>)
, anyChar
, decimal
, double
, endOfLine
, isEndOfLine
, many1
, manyTill
, satisfy
, sepBy
, sepBy1
, skipMany
, skipSpace
, skipWhile
, signed
, space
, takeWhile
, takeWhile1
)
import Data.GraphQL.AST
-- * Name
-- XXX: Handle starting `_` and no number at the beginning:
-- https://facebook.github.io/graphql/#sec-Names
name :: Parser Name
name = takeWhile1 isAlphaNum
-- * Document
document :: Parser Document
document = s *>
(Document <$> many1_ definition
-- Try SelectionSet when no definition
<|> Document <$> pure
<$> DefinitionOperation
<$> Query mempty empty empty
<$> selectionSet)
<* s
<?> "document error!"
definition :: Parser Definition
definition = DefinitionOperation <$> operationDefinition
<|> DefinitionFragment <$> fragmentDefinition
<|> DefinitionType <$> typeDefinition
<?> "definition error!"
operationDefinition :: Parser OperationDefinition
operationDefinition =
op Query "query"
<|> op Mutation "mutation"
<?> "operationDefinition error!"
where
op f n = f <$ n <* s1 <*> name <* s1
<*> (variableDefinitions <* s <|> empty)
<*> directives <* s
<*> selectionSet
variableDefinitions :: Parser [VariableDefinition]
variableDefinitions = "(" *> s *> many1_ variableDefinition <* s <* ")"
variableDefinition :: Parser VariableDefinition
variableDefinition =
VariableDefinition <$> variable <* s <* ":" <* s
<*> type_ <* s
<*> (defaultValue <|> empty)
defaultValue :: Parser DefaultValue
defaultValue = "=" *> s *> value
-- In defense of good taste, I'm taking liberty of not allowing space between
-- '$' and the 'name' even though that's not in the spec.
variable :: Parser Variable
variable = Variable <$ "$" <*> name
selectionSet :: Parser SelectionSet
selectionSet = "{" *> s *> (SelectionSet <$> many1_ selection) <* s <* "}"
selection :: Parser Selection
selection = SelectionField <$> field
-- Inline first to catch `on` case
<|> SelectionInlineFragment <$> inlineFragment
<|> SelectionFragmentSpread <$> fragmentSpread
<?> "selection error!"
field :: Parser Field
field = Field <$> (alias <* s1 <|> empty)
<*> name <* s
<*> (arguments <* s <|> empty)
<*> directives <* s
<*> selectionSet
<?> "field error!"
alias :: Parser Alias
alias = name <* s <* ":"
arguments :: Parser [Argument]
arguments = "(" *> s *> many1_ argument <* s <* ")"
argument :: Parser Argument
argument = Argument <$> name <* s <* ":" <* s <*> value
-- * Fragments
fragmentSpread :: Parser FragmentSpread
-- TODO: Make sure it fails when `... on`.
-- See https://facebook.github.io/graphql/#FragmentSpread
fragmentSpread = FragmentSpread <$ "..." <* s <*> name <* s1 <*> many_ directive
-- InlineFragment tried first in order to guard against 'on' keyword
inlineFragment :: Parser InlineFragment
inlineFragment = InlineFragment <$ "..." <* s <* "on" <* s1
<*> typeCondition <* s
<*> directives <* s
<*> selectionSet
fragmentDefinition :: Parser FragmentDefinition
fragmentDefinition =
FragmentDefinition <$ "fragment" <* s1
<*> name <* s
<* "on" <* s1
<*> typeCondition <* s
<*> directives <* s
<*> selectionSet
typeCondition :: Parser TypeCondition
typeCondition = namedType
-- * Values
-- This will try to pick the first type it can parse. If you are working with
-- explicit types use the `typedValue` parser.
value :: Parser Value
value = -- TODO: Handle arbitrary precision.
ValueInt <$> signed decimal
<|> ValueFloat <$> signed double
<|> ValueBoolean <$> bool
-- TODO: Handle escape characters, unicode, etc
<|> ValueString <$ "\"" <*> takeWhile isAlphaNum <* "\""
-- `true` and `false` have been tried before
<|> ValueEnum <$> name
<|> ValueList <$> listValue
<|> ValueObject <$> objectValue
-- Notice it can be empty
listValue :: Parser ListValue
listValue = ListValue <$ "[" <* s <*> many_ value <* s <* "]"
-- Notice it can be empty
objectValue :: Parser ObjectValue
objectValue = ObjectValue <$ "{" <* s <*> many_ objectField <* s <* "}"
objectField :: Parser ObjectField
objectField = ObjectField <$> name <* s <* ":" <* s <*> value
bool :: Parser Bool
bool = True <$ "true"
<|> False <$ "false"
-- * Directives
directives :: Parser [Directive]
directives = many_ directive
directive :: Parser Directive
directive = Directive <$ "@" <*> name <* s <*> arguments
-- * Type Reference
type_ :: Parser Type
type_ = TypeNamed <$> namedType
<|> TypeList <$> listType
<|> TypeNonNull <$> nonNullType
namedType :: Parser NamedType
namedType = NamedType <$> name
listType :: Parser ListType
listType = ListType <$ "[" <* s <*> type_ <* s <* "]"
nonNullType :: Parser NonNullType
nonNullType = NonNullTypeNamed <$> namedType <* s <* "!"
<|> NonNullTypeList <$> listType <* s <* "!"
-- * Type Definition
-- TODO: what is Const Variable?
-- https://facebook.github.io/graphql/#Value
typeDefinition :: Parser TypeDefinition
typeDefinition =
TypeDefinitionObject <$> objectTypeDefinition
<|> TypeDefinitionInterface <$> interfaceTypeDefinition
<|> TypeDefinitionUnion <$> unionTypeDefinition
<|> TypeDefinitionScalar <$> scalarTypeDefinition
<|> TypeDefinitionEnum <$> enumTypeDefinition
<|> TypeDefinitionInputObject <$> inputObjectTypeDefinition
<|> TypeDefinitionTypeExtension <$> typeExtensionDefinition
<?> "typeDefinition error!"
objectTypeDefinition :: Parser ObjectTypeDefinition
objectTypeDefinition = ObjectTypeDefinition
<$ "type" <* s1
<*> name <* s1
<*> (interfaces <* s <|> empty)
<*> fieldDefinitions
<?> "objectTypeDefinition error!"
interfaces :: Parser Interfaces
interfaces = "implements" *> s1 *> many1_ namedType
fieldDefinitions :: Parser [FieldDefinition]
fieldDefinitions = "{" *> s *> many1_ fieldDefinition <* s <* "}"
fieldDefinition :: Parser FieldDefinition
fieldDefinition = FieldDefinition
<$> name <* s
<*> (argumentsDefinition <* s <|> empty)
<* ":" <* s
<*> type_
argumentsDefinition :: Parser ArgumentsDefinition
argumentsDefinition = inputValueDefinitions
inputValueDefinitions :: Parser [InputValueDefinition]
inputValueDefinitions = "(" *> s *> many1_ inputValueDefinition <* s <* ")"
inputValueDefinition :: Parser InputValueDefinition
inputValueDefinition = InputValueDefinition
<$> name <* s
<* ":" <* s
<*> type_ <* s
<*> (value <|> empty)
interfaceTypeDefinition :: Parser InterfaceTypeDefinition
interfaceTypeDefinition = InterfaceTypeDefinition
<$ "interface" <* s1
<*> name <* s
<*> fieldDefinitions
unionTypeDefinition :: Parser UnionTypeDefinition
unionTypeDefinition = UnionTypeDefinition
<$ "union" <* s1
<*> name <* s
<* "=" <* s
<*> unionMembers
where
-- This should take care of standalone `NamedType`
unionMembers = namedType `sepBy1` (s *> "|" <* s)
scalarTypeDefinition :: Parser ScalarTypeDefinition
scalarTypeDefinition = ScalarTypeDefinition <$ "scalar" <* s1 <*> name
enumTypeDefinition :: Parser EnumTypeDefinition
enumTypeDefinition = EnumTypeDefinition
<$ "enum" <* s1
<*> name <* s
<*> enumValueDefinitions
enumValueDefinitions :: Parser [EnumValueDefinition]
enumValueDefinitions = "{" *> s *> many1_ enumValueDefinition <* s <* "}"
enumValueDefinition :: Parser EnumValueDefinition
enumValueDefinition = EnumValueDefinition <$> name
inputObjectTypeDefinition :: Parser InputObjectTypeDefinition
inputObjectTypeDefinition = InputObjectTypeDefinition
<$ "input" <* s1
<*> name <* s
<*> inputValueDefinitions
typeExtensionDefinition :: Parser TypeExtensionDefinition
typeExtensionDefinition = TypeExtensionDefinition
<$ "extend" <* s1
<*> objectTypeDefinition
-- * Internal
many_ :: Parser a -> Parser [a]
many_ = flip sepBy space'
many1_ :: Parser a -> Parser [a]
many1_ = flip sepBy1 space'
space' :: Parser Char
space' = satisfy isSpace'
s :: Parser ()
s = comments <|> skipWhile isSpace'
s1 :: Parser ()
s1 = comments <|> space' *> s
isSpace' :: Char -> Bool
isSpace' c = isSpace c || ',' == c || isEndOfLine c
comments :: Parser ()
comments = skipMany comment
comment :: Parser ()
comment = () <$ "#" <* manyTill anyChar endOfLine
|