@@ -11,7 +11,7 @@ import (
1111
1212type Document struct {
1313 Operations map [string ]* Operation
14- Fragments map [string ]* Fragment
14+ Fragments map [string ]* NamedFragment
1515}
1616
1717type Operation struct {
@@ -33,8 +33,12 @@ type VariableDef struct {
3333 Type string
3434}
3535
36+ type NamedFragment struct {
37+ Fragment
38+ Name string
39+ }
40+
3641type Fragment struct {
37- Name string
3842 On string
3943 SelSet * SelectionSet
4044}
@@ -65,8 +69,14 @@ type FragmentSpread struct {
6569 Directives map [string ]* Directive
6670}
6771
72+ type InlineFragment struct {
73+ Fragment
74+ Directives map [string ]* Directive
75+ }
76+
6877func (Field ) isSelection () {}
6978func (FragmentSpread ) isSelection () {}
79+ func (InlineFragment ) isSelection () {}
7080
7181type Value interface {
7282 isValue ()
@@ -105,7 +115,7 @@ func Parse(queryString string) (res *Document, errRes error) {
105115func parseDocument (l * lexer.Lexer ) * Document {
106116 d := & Document {
107117 Operations : make (map [string ]* Operation ),
108- Fragments : make (map [string ]* Fragment ),
118+ Fragments : make (map [string ]* NamedFragment ),
109119 }
110120 for l .Peek () != scanner .EOF {
111121 if l .Peek () == '{' {
@@ -151,8 +161,8 @@ func parseOperation(l *lexer.Lexer, opType OperationType) *Operation {
151161 return op
152162}
153163
154- func parseFragment (l * lexer.Lexer ) * Fragment {
155- f := & Fragment {}
164+ func parseFragment (l * lexer.Lexer ) * NamedFragment {
165+ f := & NamedFragment {}
156166 f .Name = l .ConsumeIdent ()
157167 l .ConsumeKeyword ("on" )
158168 f .On = l .ConsumeIdent ()
@@ -184,7 +194,7 @@ func parseSelectionSet(l *lexer.Lexer) *SelectionSet {
184194
185195func parseSelection (l * lexer.Lexer ) Selection {
186196 if l .Peek () == '.' {
187- return parseFragmentSpread (l )
197+ return parseSpread (l )
188198 }
189199 return parseField (l )
190200}
@@ -238,14 +248,29 @@ func parseDirective(l *lexer.Lexer) *Directive {
238248 return d
239249}
240250
241- func parseFragmentSpread (l * lexer.Lexer ) * FragmentSpread {
242- fs := & FragmentSpread {
243- Directives : make (map [string ]* Directive ),
244- }
251+ func parseSpread (l * lexer.Lexer ) Selection {
245252 l .ConsumeToken ('.' )
246253 l .ConsumeToken ('.' )
247254 l .ConsumeToken ('.' )
248- fs .Name = l .ConsumeIdent ()
255+ ident := l .ConsumeIdent ()
256+
257+ if ident == "on" {
258+ f := & InlineFragment {
259+ Directives : make (map [string ]* Directive ),
260+ }
261+ f .On = l .ConsumeIdent ()
262+ for l .Peek () == '@' {
263+ d := parseDirective (l )
264+ f .Directives [d .Name ] = d
265+ }
266+ f .SelSet = parseSelectionSet (l )
267+ return f
268+ }
269+
270+ fs := & FragmentSpread {
271+ Directives : make (map [string ]* Directive ),
272+ Name : ident ,
273+ }
249274 for l .Peek () == '@' {
250275 d := parseDirective (l )
251276 fs .Directives [d .Name ] = d
0 commit comments