-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed bug when marshaling errors ; ran gofmt(#8)
* added path and extension to Error * ran gofmt
- Loading branch information
1 parent
56d3536
commit 2770323
Showing
8 changed files
with
1,057 additions
and
1,047 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ". ") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
Oops, something went wrong.