Skip to content

Commit 5457f60

Browse files
Accepting value Json in parameter of request's body in custom Scalar (#467)
Accept JSON value in resolver args
1 parent af5bb93 commit 5457f60

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

example/scalar_map/server.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
8+
graphql "github.com/graph-gophers/graphql-go"
9+
"github.com/graph-gophers/graphql-go/example/scalar_map/types"
10+
"github.com/graph-gophers/graphql-go/relay"
11+
)
12+
13+
14+
type Args struct {
15+
Name string
16+
Data types.Map
17+
}
18+
19+
20+
type mutation struct{}
21+
22+
func (_ *mutation) Hello(args Args) string {
23+
24+
fmt.Println(args)
25+
26+
return "Args accept!"
27+
}
28+
29+
func main() {
30+
s := `
31+
scalar Map
32+
33+
type Query {}
34+
35+
type Mutation {
36+
hello(
37+
name: String!
38+
data: Map!
39+
): String!
40+
}
41+
`
42+
schema := graphql.MustParseSchema(s, &mutation{})
43+
http.Handle("/query", &relay.Handler{Schema: schema})
44+
45+
log.Println("Listen in port :8080")
46+
log.Fatal(http.ListenAndServe(":8080", nil))
47+
}

example/scalar_map/types/map.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package types
2+
3+
import "fmt"
4+
5+
type Map map[string]interface {}
6+
7+
func (Map) ImplementsGraphQLType(name string) bool {
8+
return name == "Map"
9+
}
10+
11+
func (j *Map) UnmarshalGraphQL(input interface{}) error {
12+
json, ok := input.(map[string]interface{})
13+
if !ok {
14+
return fmt.Errorf("wrong type")
15+
}
16+
17+
*j = json
18+
return nil
19+
}

internal/validation/validation.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,9 @@ func validateValueType(c *opContext, v types.Value, t types.Type) (bool, string)
778778
if validateBasicLit(lit, t) {
779779
return true, ""
780780
}
781+
return false, fmt.Sprintf("Expected type %q, found %s.", t, v)
781782
}
783+
return true, ""
782784

783785
case *types.List:
784786
list, ok := v.(*types.ListValue)

0 commit comments

Comments
 (0)