Skip to content

Commit

Permalink
Add test to highlight usecase
Browse files Browse the repository at this point in the history
  • Loading branch information
bowd committed Mar 20, 2020
1 parent d98ff1b commit 1aa20f2
Show file tree
Hide file tree
Showing 23 changed files with 1,001 additions and 234 deletions.
592 changes: 479 additions & 113 deletions codegen/testserver/generated.go

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions codegen/testserver/gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ autobind:
- "github.com/99designs/gqlgen/codegen/testserver/introspection"
- "github.com/99designs/gqlgen/codegen/testserver/invalid-packagename"

models:
Email:
model: "github.com/99designs/gqlgen/codegen/testserver.Email"
8 changes: 8 additions & 0 deletions codegen/testserver/models-gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions codegen/testserver/mutation_with_custom_scalar.go
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)
}
13 changes: 13 additions & 0 deletions codegen/testserver/mutation_with_custom_scalar.graphql
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!
}
63 changes: 63 additions & 0 deletions codegen/testserver/mutation_with_custom_scalar_test.go
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",
})
})
}
8 changes: 8 additions & 0 deletions codegen/testserver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func (r *modelMethodsResolver) ResolverField(ctx context.Context, obj *ModelMeth
panic("not implemented")
}

func (r *mutationResolver) UpdateSomething(ctx context.Context, input SpecialInput) (string, error) {
panic("not implemented")
}

func (r *overlappingFieldsResolver) OldFoo(ctx context.Context, obj *OverlappingFields) (int, error) {
panic("not implemented")
}
Expand Down Expand Up @@ -319,6 +323,9 @@ func (r *Resolver) ForcedResolver() ForcedResolverResolver { return &forcedResol
// ModelMethods returns ModelMethodsResolver implementation.
func (r *Resolver) ModelMethods() ModelMethodsResolver { return &modelMethodsResolver{r} }

// Mutation returns MutationResolver implementation.
func (r *Resolver) Mutation() MutationResolver { return &mutationResolver{r} }

// OverlappingFields returns OverlappingFieldsResolver implementation.
func (r *Resolver) OverlappingFields() OverlappingFieldsResolver { return &overlappingFieldsResolver{r} }

Expand All @@ -344,6 +351,7 @@ type backedByInterfaceResolver struct{ *Resolver }
type errorsResolver struct{ *Resolver }
type forcedResolverResolver struct{ *Resolver }
type modelMethodsResolver struct{ *Resolver }
type mutationResolver struct{ *Resolver }
type overlappingFieldsResolver struct{ *Resolver }
type panicsResolver struct{ *Resolver }
type primitiveResolver struct{ *Resolver }
Expand Down
12 changes: 12 additions & 0 deletions codegen/testserver/stub.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 18 additions & 9 deletions example/chat/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 14 additions & 6 deletions example/config/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions example/dataloader/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1aa20f2

Please sign in to comment.