Skip to content

Commit

Permalink
Fixed bug when marshaling errors ; ran gofmt(#8)
Browse files Browse the repository at this point in the history
* added path and extension to Error

* ran gofmt
  • Loading branch information
AlecAivazis committed Mar 25, 2020
1 parent 56d3536 commit 2770323
Show file tree
Hide file tree
Showing 8 changed files with 1,057 additions and 1,047 deletions.
80 changes: 38 additions & 42 deletions error.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
package graphql

import "strings"

// ErrorExtensions define fields that extend the standard graphql error shape
type ErrorExtensions struct {
Code string `json:"code"`
}

// Error represents a graphql error
type Error struct {
Extensions ErrorExtensions `json:"extensions"`
Message string `json:"message"`
}

func (e *Error) Error() string {
return e.Message
}

// NewError returns a graphql error with the given code and message
func NewError(code string, message string) *Error {
return &Error{
Message: message,
Extensions: ErrorExtensions{
Code: code,
},
}
}

// ErrorList represents a list of errors
type ErrorList []error

// Error returns a string representation of each error
func (list ErrorList) Error() string {
acc := []string{}

for _, error := range list {
acc = append(acc, error.Error())
}

return strings.Join(acc, ". ")
}
package graphql

import "strings"

// Error represents a graphql error
type Error struct {
Extensions map[string]interface{} `json:"extensions"`
Message string `json:"message"`
Path []interface{} `json:"path,omitempty"`
}

func (e *Error) Error() string {
return e.Message
}

// NewError returns a graphql error with the given code and message
func NewError(code string, message string) *Error {
return &Error{
Message: message,
Extensions: map[string]interface{}{
"code": code,
},
}
}

// ErrorList represents a list of errors
type ErrorList []error

// Error returns a string representation of each error
func (list ErrorList) Error() string {
acc := []string{}

for _, error := range list {
acc = append(acc, error.Error())
}

return strings.Join(acc, ". ")
}
42 changes: 21 additions & 21 deletions error_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package graphql

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSerializeError(t *testing.T) {
// marshal the 2 kinds of errors
errWithCode, _ := json.Marshal(NewError("ERROR_CODE", "foo"))
expected, _ := json.Marshal(map[string]interface{}{
"extensions": map[string]interface{}{
"code": "ERROR_CODE",
},
"message": "foo",
})

assert.Equal(t, string(expected), string(errWithCode))
}
package graphql

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSerializeError(t *testing.T) {
// marshal the 2 kinds of errors
errWithCode, _ := json.Marshal(NewError("ERROR_CODE", "foo"))
expected, _ := json.Marshal(map[string]interface{}{
"extensions": map[string]interface{}{
"code": "ERROR_CODE",
},
"message": "foo",
})

assert.Equal(t, string(expected), string(errWithCode))
}
132 changes: 66 additions & 66 deletions format.go
Original file line number Diff line number Diff line change
@@ -1,66 +1,66 @@
package graphql

import (
"fmt"
"strings"

"github.com/vektah/gqlparser/v2/ast"
)

func formatIndentPrefix(level int) string {
acc := "\n"
// build up the prefix
for i := 0; i <= level; i++ {
acc += " "
}

return acc
}
func formatSelectionSelectionSet(level int, selectionSet ast.SelectionSet) string {
acc := " {"
// and any sub selection
acc += formatSelection(level+1, selectionSet)
acc += formatIndentPrefix(level) + "}"

return acc
}

func formatSelection(level int, selectionSet ast.SelectionSet) string {
acc := ""

for _, selection := range selectionSet {
acc += formatIndentPrefix(level)
switch selection := selection.(type) {
case *ast.Field:
// add the field name
acc += selection.Name
if len(selection.SelectionSet) > 0 {
acc += formatSelectionSelectionSet(level, selection.SelectionSet)
}
case *ast.InlineFragment:
// print the fragment name
acc += fmt.Sprintf("... on %v", selection.TypeCondition) +
formatSelectionSelectionSet(level, selection.SelectionSet)
case *ast.FragmentSpread:
// print the fragment name
acc += "..." + selection.Name
}
}

return acc
}

// FormatSelectionSet returns a pretty printed version of a selection set
func FormatSelectionSet(selection ast.SelectionSet) string {
acc := "{"

insides := formatSelection(0, selection)

if strings.TrimSpace(insides) != "" {
acc += insides + "\n}"
} else {
acc += "}"
}

return acc
}
package graphql

import (
"fmt"
"strings"

"github.com/vektah/gqlparser/v2/ast"
)

func formatIndentPrefix(level int) string {
acc := "\n"
// build up the prefix
for i := 0; i <= level; i++ {
acc += " "
}

return acc
}
func formatSelectionSelectionSet(level int, selectionSet ast.SelectionSet) string {
acc := " {"
// and any sub selection
acc += formatSelection(level+1, selectionSet)
acc += formatIndentPrefix(level) + "}"

return acc
}

func formatSelection(level int, selectionSet ast.SelectionSet) string {
acc := ""

for _, selection := range selectionSet {
acc += formatIndentPrefix(level)
switch selection := selection.(type) {
case *ast.Field:
// add the field name
acc += selection.Name
if len(selection.SelectionSet) > 0 {
acc += formatSelectionSelectionSet(level, selection.SelectionSet)
}
case *ast.InlineFragment:
// print the fragment name
acc += fmt.Sprintf("... on %v", selection.TypeCondition) +
formatSelectionSelectionSet(level, selection.SelectionSet)
case *ast.FragmentSpread:
// print the fragment name
acc += "..." + selection.Name
}
}

return acc
}

// FormatSelectionSet returns a pretty printed version of a selection set
func FormatSelectionSet(selection ast.SelectionSet) string {
acc := "{"

insides := formatSelection(0, selection)

if strings.TrimSpace(insides) != "" {
acc += insides + "\n}"
} else {
acc += "}"
}

return acc
}
114 changes: 57 additions & 57 deletions format_test.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
package graphql

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/vektah/gqlparser/v2/ast"
)

func TestFormatSelectionSet(t *testing.T) {
// the table of sets to test
rows := []struct {
input ast.SelectionSet
expected string
}{
{
ast.SelectionSet{},
"{}",
},
{
ast.SelectionSet{
&ast.Field{Name: "firstName"},
&ast.Field{Name: "friend", SelectionSet: ast.SelectionSet{&ast.Field{Name: "lastName"}}},
},
`{
firstName
friend {
lastName
}
}`,
},
{
ast.SelectionSet{&ast.FragmentSpread{Name: "MyFragment"}},
`{
...MyFragment
}`,
},
{
ast.SelectionSet{
&ast.InlineFragment{
TypeCondition: "MyType",
SelectionSet: ast.SelectionSet{&ast.Field{Name: "firstName"}},
},
},
`{
... on MyType {
firstName
}
}`,
},
}

for _, row := range rows {
// make sure we get the expected result
assert.Equal(t, row.expected, FormatSelectionSet(row.input))
}
}
package graphql

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/vektah/gqlparser/v2/ast"
)

func TestFormatSelectionSet(t *testing.T) {
// the table of sets to test
rows := []struct {
input ast.SelectionSet
expected string
}{
{
ast.SelectionSet{},
"{}",
},
{
ast.SelectionSet{
&ast.Field{Name: "firstName"},
&ast.Field{Name: "friend", SelectionSet: ast.SelectionSet{&ast.Field{Name: "lastName"}}},
},
`{
firstName
friend {
lastName
}
}`,
},
{
ast.SelectionSet{&ast.FragmentSpread{Name: "MyFragment"}},
`{
...MyFragment
}`,
},
{
ast.SelectionSet{
&ast.InlineFragment{
TypeCondition: "MyType",
SelectionSet: ast.SelectionSet{&ast.Field{Name: "firstName"}},
},
},
`{
... on MyType {
firstName
}
}`,
},
}

for _, row := range rows {
// make sure we get the expected result
assert.Equal(t, row.expected, FormatSelectionSet(row.input))
}
}
Loading

0 comments on commit 2770323

Please sign in to comment.