-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_operation.go
87 lines (75 loc) · 1.47 KB
/
parse_operation.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
package gqlyzer
import (
"errors"
"github.com/kumparan/gqlyzer/token"
"github.com/kumparan/gqlyzer/token/operation"
)
func (l *Lexer) parseOperationType() (op operation.Type, isAnonymous bool, err error) {
l.consumeWhitespace()
l.pushFlush()
c, err := l.read()
if err != nil {
return
}
switch c {
case 'q':
if err = l.parseKeyword("query"); err != nil {
return
}
return operation.Query, false, nil
case 'm':
if err = l.parseKeyword("mutation"); err != nil {
return
}
return operation.Mutation, false, nil
case 's':
if err = l.parseKeyword("subscription"); err != nil {
return
}
return operation.Subscription, false, nil
case '{': // anonymous operation returns query type
return operation.Query, true, nil
default:
err = errors.New("unknown definition")
return
}
}
func (l *Lexer) parseOperation() (op token.Operation, err error) {
opType, isAnonymous, err := l.parseOperationType()
if err != nil {
return
}
op.Type = opType
if !isAnonymous {
l.cursor++
name, err := l.parseName()
if err != nil {
return token.Operation{}, err
}
op.Name = name
}
l.consumeWhitespace()
c, err := l.read()
if err != nil {
return
}
// ignore variable of operation
if c == '(' {
l.cursor++
c, err = l.read()
for err == nil && c != ')' {
l.cursor++
c, err = l.read()
}
if err != nil {
return
}
l.cursor++
l.consumeWhitespace()
}
op.Selections, err = l.parseSelectionSet()
if err != nil {
return
}
return
}