Skip to content

Commit

Permalink
use encoding/json to encode scalars
Browse files Browse the repository at this point in the history
There are some edge cases that are better handled by the proven encoder of encoding/json, for example special characters in strings.
  • Loading branch information
neelance committed Apr 5, 2017
1 parent 3f1cb6f commit 67e6f91
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package exec
import (
"bytes"
"context"
"encoding/json"
"reflect"
"strconv"
"sync"

"github.com/neelance/graphql-go/errors"
Expand Down Expand Up @@ -261,24 +261,12 @@ func (r *Request) execSelectionSet(ctx context.Context, sels []selected.Selectio
out.WriteByte(']')

case *schema.Scalar:
var b []byte // TODO use scratch
switch t.Name {
case "Int":
out.Write(strconv.AppendInt(b, resolver.Int(), 10))
case "Float":
out.Write(strconv.AppendFloat(b, resolver.Float(), 'f', -1, 64))
case "String":
out.Write(strconv.AppendQuote(b, resolver.String()))
case "Boolean":
out.Write(strconv.AppendBool(b, resolver.Bool()))
default:
v := resolver.Interface().(marshaler)
data, err := v.MarshalJSON()
if err != nil {
panic(errors.Errorf("could not marshal %v", v))
}
out.Write(data)
v := resolver.Interface()
data, err := json.Marshal(v)
if err != nil {
panic(errors.Errorf("could not marshal %v", v))
}
out.Write(data)

case *schema.Enum:
out.WriteByte('"')
Expand Down

0 comments on commit 67e6f91

Please sign in to comment.