github.com/operandinc/gqlgen@v0.16.1/codegen/testserver/followschema/mutation_with_custom_scalar_test.go (about)

     1  package followschema
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/operandinc/gqlgen/client"
     8  	"github.com/operandinc/gqlgen/graphql/handler"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestErrorInsideMutationArgument(t *testing.T) {
    13  	resolvers := &Stub{}
    14  	resolvers.MutationResolver.UpdateSomething = func(_ context.Context, input SpecialInput) (s string, err error) {
    15  		return "Hello world", nil
    16  	}
    17  
    18  	c := client.New(handler.NewDefaultServer(NewExecutableSchema(Config{Resolvers: resolvers})))
    19  
    20  	t.Run("mutation with correct input doesn't return error", func(t *testing.T) {
    21  		var resp map[string]interface{}
    22  		input := map[string]interface{}{
    23  			"nesting": map[string]interface{}{
    24  				"field": "email@example.com",
    25  			},
    26  		}
    27  		err := c.Post(
    28  			`mutation TestMutation($input: SpecialInput!) { updateSomething(input: $input) }`,
    29  			&resp,
    30  			client.Var("input", input),
    31  		)
    32  		require.Equal(t, resp["updateSomething"], "Hello world")
    33  		require.NoError(t, err)
    34  	})
    35  
    36  	t.Run("mutation with incorrect input returns full path", func(t *testing.T) {
    37  		var resp map[string]interface{}
    38  		input := map[string]interface{}{
    39  			"nesting": map[string]interface{}{
    40  				"field": "not-an-email",
    41  			},
    42  		}
    43  		err := c.Post(
    44  			`mutation TestMutation($input: SpecialInput!) { updateSomething(input: $input) }`,
    45  			&resp,
    46  			client.Var("input", input),
    47  		)
    48  		require.EqualError(t, err, `[{"message":"invalid email format","path":["updateSomething","input","nesting","field"]}]`)
    49  	})
    50  }