-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
1,001 additions
and
234 deletions.
There are no files selected for viewing
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,63 @@ | ||
package testserver | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"github.com/99designs/gqlgen/client" | ||
"github.com/99designs/gqlgen/graphql/handler" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
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@example.com", | ||
}, | ||
} | ||
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), | ||
) | ||
jsonErr, ok := err.(client.RawJsonError) | ||
require.True(t, ok) | ||
var errDetails []map[string]interface{} | ||
err = json.Unmarshal(jsonErr.RawMessage, &errDetails) | ||
require.NoError(t, err) | ||
require.Len(t, errDetails, 1) | ||
firstErr := errDetails[0] | ||
path, ok := firstErr["path"].([]interface{}) | ||
require.Equal(t, path, []interface{}{ | ||
"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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.