Skip to content

Commit

Permalink
Return error in MutationFn
Browse files Browse the repository at this point in the history
  • Loading branch information
pyros2097 authored and chris-ramon committed Jan 8, 2016
1 parent 1ee0832 commit 5203db9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
5 changes: 3 additions & 2 deletions examples/starwars/mutation_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package starwars_test

import (
"reflect"
"testing"

"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/testutil"
"github.com/graphql-go/relay/examples/starwars"
"reflect"
"testing"
)

func TestMutation_CorrectlyMutatesTheDataSet(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions examples/starwars/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func init() {
},
},
},
MutateAndGetPayload: func(inputMap map[string]interface{}, info graphql.ResolveInfo, ctx context.Context) map[string]interface{} {
MutateAndGetPayload: func(inputMap map[string]interface{}, info graphql.ResolveInfo, ctx context.Context) (map[string]interface{}, error) {
// `inputMap` is a map with keys/fields as specified in `InputFields`
// Note, that these fields were specified as non-nullables, so we can assume that it exists.
shipName := inputMap["shipName"].(string)
Expand All @@ -304,7 +304,7 @@ func init() {
return map[string]interface{}{
"shipId": newShip.ID,
"factionId": factionId,
}
}, nil
},
})

Expand Down
7 changes: 5 additions & 2 deletions mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"golang.org/x/net/context"
)

type MutationFn func(inputMap map[string]interface{}, info graphql.ResolveInfo, ctx context.Context) map[string]interface{}
type MutationFn func(inputMap map[string]interface{}, info graphql.ResolveInfo, ctx context.Context) (map[string]interface{}, error)

/*
A description of a mutation consumable by mutationWithClientMutationId
Expand Down Expand Up @@ -76,7 +76,10 @@ func MutationWithClientMutationID(config MutationConfig) *graphql.Field {
input = inputVal
}
}
payload := config.MutateAndGetPayload(input, p.Info, p.Context)
payload, err := config.MutateAndGetPayload(input, p.Info, p.Context)
if err != nil {
return nil, err
}
if clientMutationID, ok := input["clientMutationId"]; ok {
payload["clientMutationId"] = clientMutationID
}
Expand Down
8 changes: 4 additions & 4 deletions mutation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ var simpleMutationTest = relay.MutationWithClientMutationID(relay.MutationConfig
Type: graphql.Int,
},
},
MutateAndGetPayload: func(inputMap map[string]interface{}, info graphql.ResolveInfo, ctx context.Context) map[string]interface{} {
MutateAndGetPayload: func(inputMap map[string]interface{}, info graphql.ResolveInfo, ctx context.Context) (map[string]interface{}, error) {
return map[string]interface{}{
"result": 1,
}
}, nil
},
})

Expand All @@ -42,13 +42,13 @@ var simplePromiseMutationTest = relay.MutationWithClientMutationID(relay.Mutatio
Type: graphql.Int,
},
},
MutateAndGetPayload: func(inputMap map[string]interface{}, info graphql.ResolveInfo, ctx context.Context) map[string]interface{} {
MutateAndGetPayload: func(inputMap map[string]interface{}, info graphql.ResolveInfo, ctx context.Context) (map[string]interface{}, error) {
c := make(chan int)
go testAsyncDataMutation(&c)
result := <-c
return map[string]interface{}{
"result": result,
}
}, nil
},
})

Expand Down

0 comments on commit 5203db9

Please sign in to comment.