forked from arsmn/fastgql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request 99designs#1115 from bowd/add-input-path-for-unmars…
…haling Add input path in unmarshaling errors
- Loading branch information
Showing
30 changed files
with
1,218 additions
and
272 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
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package testserver | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"regexp" | ||
) | ||
|
||
var re = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$") | ||
|
||
type Email string | ||
|
||
func (value *Email) UnmarshalGQL(v interface{}) error { | ||
input, ok := v.(string) | ||
if !ok { | ||
return fmt.Errorf("email expects a string value") | ||
} | ||
if !re.MatchString(input) { | ||
return fmt.Errorf("invalid email format") | ||
} | ||
*value = Email(input) | ||
return nil | ||
} | ||
|
||
func (value Email) MarshalGQL(w io.Writer) { | ||
output, _ := json.Marshal(string(value)) | ||
w.Write(output) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
extend type Mutation { | ||
updateSomething(input: SpecialInput!): String! | ||
} | ||
|
||
scalar Email | ||
|
||
input SpecialInput { | ||
nesting: NestedInput! | ||
} | ||
|
||
input NestedInput { | ||
field: Email! | ||
} |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package testserver | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/99designs/gqlgen/client" | ||
"github.com/99designs/gqlgen/graphql/handler" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestErrorInsideMutationArgument(t *testing.T) { | ||
resolvers := &Stub{} | ||
resolvers.MutationResolver.UpdateSomething = func(_ context.Context, input SpecialInput) (s string, err error) { | ||
return "Hello world", nil | ||
} | ||
|
||
c := client.New(handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers}))) | ||
|
||
t.Run("mutation with correct input doesn't return error", func(t *testing.T) { | ||
var resp map[string]interface{} | ||
input := map[string]interface{}{ | ||
"nesting": map[string]interface{}{ | ||
"field": "[email protected]", | ||
}, | ||
} | ||
err := c.Post( | ||
`mutation TestMutation($input: SpecialInput!) { updateSomething(input: $input) }`, | ||
&resp, | ||
client.Var("input", input), | ||
) | ||
require.Equal(t, resp["updateSomething"], "Hello world") | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("mutation with incorrect input returns full path", func(t *testing.T) { | ||
var resp map[string]interface{} | ||
input := map[string]interface{}{ | ||
"nesting": map[string]interface{}{ | ||
"field": "not-an-email", | ||
}, | ||
} | ||
err := c.Post( | ||
`mutation TestMutation($input: SpecialInput!) { updateSomething(input: $input) }`, | ||
&resp, | ||
client.Var("input", input), | ||
) | ||
require.EqualError(t, err, `[{"message":"invalid email format","path":["updateSomething","input","nesting","field"]}]`) | ||
}) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -148,3 +148,57 @@ models: | |
``` | ||
See the [example/scalars](https://github.com/99designs/gqlgen/tree/master/example/scalars) package for more examples. | ||
## Unmarshaling Errors | ||
The errors that occur as part of custom scalar unmarshaling will return a full path to the field. | ||
For example, given the following schema ... | ||
```graphql | ||
extend type Mutation{ | ||
updateUser(userInput: UserInput!): User! | ||
} | ||
|
||
input UserInput { | ||
name: String! | ||
primaryContactDetails: ContactDetailsInput! | ||
secondaryContactDetails: ContactDetailsInput! | ||
} | ||
|
||
scalar Email | ||
input ContactDetailsInput { | ||
email: Email! | ||
} | ||
``` | ||
|
||
... and the following variables: | ||
|
||
```json | ||
|
||
{ | ||
"userInput": { | ||
"name": "George", | ||
"primaryContactDetails": { | ||
"email": "not-an-email" | ||
}, | ||
"secondaryContactDetails": { | ||
"email": "[email protected]" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
... and an unmarshal function that returns an error if the email is invalid. The mutation will return an error containing the full path: | ||
```json | ||
{ | ||
"message": "email invalid", | ||
"path": [ | ||
"updateUser", | ||
"userInput", | ||
"primaryContactDetails", | ||
"email" | ||
] | ||
} | ||
``` | ||
|
||
|
Oops, something went wrong.