Skip to content

Commit

Permalink
refactor: separate InlineFragment and FragmentSpread
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Mar 22, 2017
1 parent d6aec0d commit 7058116
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 63 deletions.
13 changes: 11 additions & 2 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,13 +503,22 @@ func (e *objectExec) execSelectionSet(ctx context.Context, r *request, selSet *q
e.execField(ctx, r, field, resolver, addResult)
})

case *query.Fragment:
case *query.InlineFragment:
frag := sel
if skipByDirective(r, frag.Directives) {
continue
}
execSel(func() {
e.execFragment(ctx, r, frag, resolver, addResult)
e.execFragment(ctx, r, &frag.Fragment, resolver, addResult)
})

case *query.FragmentSpread:
spread := sel
if skipByDirective(r, spread.Directives) {
continue
}
execSel(func() {
e.execFragment(ctx, r, &r.doc.Fragments[spread.Name].Fragment, resolver, addResult)
})

default:
Expand Down
74 changes: 14 additions & 60 deletions internal/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ const (
Mutation
)

type NamedFragment struct {
Fragment
Name string
type Fragment struct {
On lexer.Ident
SelSet *SelectionSet
}

type Fragment struct {
On lexer.Ident
SelSet *SelectionSet
type NamedFragment struct {
Fragment
Name string
Directives map[string]common.ArgumentList
}

Expand All @@ -57,13 +57,18 @@ type Field struct {
Loc errors.Location
}

type InlineFragment struct {
Fragment
Directives map[string]common.ArgumentList
}

type FragmentSpread struct {
Name string
Directives map[string]common.ArgumentList
}

func (Field) isSelection() {}
func (Fragment) isSelection() {}
func (InlineFragment) isSelection() {}
func (FragmentSpread) isSelection() {}

func Parse(queryString string) (*Document, *errors.QueryError) {
Expand All @@ -81,61 +86,9 @@ func Parse(queryString string) (*Document, *errors.QueryError) {
return nil, err
}

for _, op := range doc.Operations {
if err := resolveSelSet(doc, op.SelSet); err != nil {
return nil, err
}
}

for _, f := range doc.Fragments {
if err := resolveSelSet(doc, f.Fragment.SelSet); err != nil {
return nil, err
}
}

return doc, nil
}

func resolveSelSet(doc *Document, selSet *SelectionSet) *errors.QueryError {
var err *errors.QueryError
for i, sel := range selSet.Selections {
selSet.Selections[i], err = resolveSelection(doc, sel)
if err != nil {
return err
}
}
return nil
}

func resolveSelection(doc *Document, sel Selection) (Selection, *errors.QueryError) {
switch sel := sel.(type) {
case *Field:
if sel.SelSet != nil {
if err := resolveSelSet(doc, sel.SelSet); err != nil {
return nil, err
}
}
return sel, nil

case *FragmentSpread:
frag, ok := doc.Fragments[sel.Name]
if !ok {
return nil, errors.Errorf("fragment %q not found", sel.Name)
}
return &Fragment{
On: frag.On,
SelSet: frag.SelSet,
Directives: sel.Directives,
}, nil

case *Fragment:
return sel, nil

default:
panic("unreachable")
}
}

func parseDocument(l *lexer.Lexer) *Document {
d := &Document{
Operations: make(map[string]*Operation),
Expand Down Expand Up @@ -189,6 +142,7 @@ func parseFragment(l *lexer.Lexer) *NamedFragment {
f.Name = l.ConsumeIdent()
l.ConsumeKeyword("on")
f.On = l.ConsumeIdentWithLoc()
f.Directives = common.ParseDirectives(l)
f.SelSet = parseSelectionSet(l)
return f
}
Expand Down Expand Up @@ -235,7 +189,7 @@ func parseSpread(l *lexer.Lexer) Selection {
l.ConsumeToken('.')
l.ConsumeToken('.')

f := &Fragment{}
f := &InlineFragment{}
if l.Peek() == scanner.Ident {
ident := l.ConsumeIdent()
if ident != "on" {
Expand Down
5 changes: 4 additions & 1 deletion internal/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func validateSelection(s *schema.Schema, sel query.Selection, t common.Type) (er
errs = append(errs, validateSelectionSet(s, sel.SelSet, ft)...)
}

case *query.Fragment:
case *query.InlineFragment:
errs = append(errs, validateDirectives(s, sel.Directives)...)
if sel.On.Name != "" {
t = s.Types[sel.On.Name]
Expand All @@ -125,6 +125,9 @@ func validateSelection(s *schema.Schema, sel query.Selection, t common.Type) (er
}
errs = append(errs, validateSelectionSet(s, sel.SelSet, t)...)

case *query.FragmentSpread:
// TODO

default:
panic("unreachable")
}
Expand Down

0 comments on commit 7058116

Please sign in to comment.