Skip to content

Commit

Permalink
refactor: Loc on Field
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed Mar 22, 2017
1 parent b5919db commit a5a1160
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
22 changes: 12 additions & 10 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,10 +529,12 @@ func (e *objectExec) execSelectionSet(ctx context.Context, r *request, selSet *q
}

func (e *objectExec) execField(ctx context.Context, r *request, f *query.Field, resolver reflect.Value, addResult addResultFn) {
switch f.Name {
fieldAlias := f.Alias.Name
fieldName := f.Name.Name
switch fieldName {
case "__typename":
if len(e.typeAssertions) == 0 {
addResult(f.Alias, e.name)
addResult(fieldAlias, e.name)
return
}

Expand All @@ -542,28 +544,28 @@ func (e *objectExec) execField(ctx context.Context, r *request, f *query.Field,
return
}
if out[1].Bool() {
addResult(f.Alias, name)
addResult(fieldAlias, name)
return
}
}

case "__schema":
addResult(f.Alias, introspectSchema(ctx, r, f.SelSet))
addResult(fieldAlias, introspectSchema(ctx, r, f.SelSet))

case "__type":
p := valuePacker{valueType: reflect.TypeOf("")}
v, err := p.pack(r, r.resolveVar(f.Arguments.MustGet("name").Value))
if err != nil {
r.addError(errors.Errorf("%s", err))
addResult(f.Alias, nil)
addResult(fieldAlias, nil)
return
}
addResult(f.Alias, introspectType(ctx, r, v.String(), f.SelSet))
addResult(fieldAlias, introspectType(ctx, r, v.String(), f.SelSet))

default:
fe, ok := e.fields[f.Name]
fe, ok := e.fields[fieldName]
if !ok {
panic(fmt.Errorf("%q has no field %q", e.name, f.Name)) // TODO proper error handling
panic(fmt.Errorf("%q has no field %q", e.name, fieldName)) // TODO proper error handling
}

span, spanCtx := opentracing.StartSpanFromContext(ctx, fmt.Sprintf("GraphQL field: %s.%s", fe.typeName, fe.field.Name))
Expand All @@ -580,14 +582,14 @@ func (e *objectExec) execField(ctx context.Context, r *request, f *query.Field,
queryError := errors.Errorf("%s", err)
queryError.ResolverError = err
r.addError(queryError)
addResult(f.Alias, nil) // TODO handle non-nil
addResult(fieldAlias, nil) // TODO handle non-nil

ext.Error.Set(span, true)
span.SetTag(OpenTracingTagError, err)
return
}

addResult(f.Alias, result)
addResult(fieldAlias, result)
}
}

Expand Down
13 changes: 5 additions & 8 deletions internal/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ type Selection interface {
}

type Field struct {
Alias string
Name string
Alias lexer.Ident
Name lexer.Ident
Arguments common.ArgumentList
Directives map[string]*common.Directive
SelSet *SelectionSet
Loc errors.Location
}

type InlineFragment struct {
Expand Down Expand Up @@ -182,14 +181,12 @@ func parseSelection(l *lexer.Lexer) Selection {
}

func parseField(l *lexer.Lexer) *Field {
f := &Field{
Loc: l.Location(),
}
f.Alias = l.ConsumeIdent()
f := &Field{}
f.Alias = l.ConsumeIdentWithLoc()
f.Name = f.Alias
if l.Peek() == ':' {
l.ConsumeToken(':')
f.Name = l.ConsumeIdent()
f.Name = l.ConsumeIdentWithLoc()
}
if l.Peek() == '(' {
f.Arguments = common.ParseArguments(l)
Expand Down
20 changes: 11 additions & 9 deletions internal/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,17 @@ func (c *context) validateSelection(sel query.Selection, t common.Type) {
switch sel := sel.(type) {
case *query.Field:
c.validateDirectives("FIELD", sel.Directives)
if sel.Name == "__schema" || sel.Name == "__type" || sel.Name == "__typename" {

fieldName := sel.Name.Name
if fieldName == "__schema" || fieldName == "__type" || fieldName == "__typename" {
return
}

t = unwrapType(t)
f := fields(t).Get(sel.Name)
f := fields(t).Get(fieldName)
if f == nil && t != nil {
suggestion := makeSuggestion("Did you mean", fields(t).Names(), sel.Name)
c.addErr(sel.Loc, "FieldsOnCorrectType", "Cannot query field %q on type %q.%s", sel.Name, t, suggestion)
suggestion := makeSuggestion("Did you mean", fields(t).Names(), fieldName)
c.addErr(sel.Alias.Loc, "FieldsOnCorrectType", "Cannot query field %q on type %q.%s", fieldName, t, suggestion)
}

names := make(nameSet)
Expand All @@ -117,9 +119,9 @@ func (c *context) validateSelection(sel query.Selection, t common.Type) {
}

if f != nil {
c.validateArguments(sel.Arguments, f.Args, sel.Loc,
func() string { return fmt.Sprintf("field %q of type %q", sel.Name, t) },
func() string { return fmt.Sprintf("Field %q", sel.Name) },
c.validateArguments(sel.Arguments, f.Args, sel.Alias.Loc,
func() string { return fmt.Sprintf("field %q of type %q", fieldName, t) },
func() string { return fmt.Sprintf("Field %q", fieldName) },
)
}

Expand All @@ -128,10 +130,10 @@ func (c *context) validateSelection(sel query.Selection, t common.Type) {
ft = f.Type
sf := hasSubfields(ft)
if sf && sel.SelSet == nil {
c.addErr(sel.Loc, "ScalarLeafs", "Field %q of type %q must have a selection of subfields. Did you mean \"%s { ... }\"?", sel.Name, ft, sel.Name)
c.addErr(sel.Alias.Loc, "ScalarLeafs", "Field %q of type %q must have a selection of subfields. Did you mean \"%s { ... }\"?", fieldName, ft, fieldName)
}
if !sf && sel.SelSet != nil {
c.addErr(sel.SelSet.Loc, "ScalarLeafs", "Field %q must not have a selection since type %q has no subfields.", sel.Name, ft)
c.addErr(sel.SelSet.Loc, "ScalarLeafs", "Field %q must not have a selection since type %q has no subfields.", fieldName, ft)
}
}
if sel.SelSet != nil {
Expand Down

0 comments on commit a5a1160

Please sign in to comment.