Skip to content

Commit

Permalink
Accepting value Json in parameter of request's body in custom Scalar (#…
Browse files Browse the repository at this point in the history
…467)

Accept JSON value in resolver args
  • Loading branch information
GustavoDelfim committed Sep 13, 2021
1 parent af5bb93 commit 5457f60
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
47 changes: 47 additions & 0 deletions example/scalar_map/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"fmt"
"log"
"net/http"

graphql "github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/example/scalar_map/types"
"github.com/graph-gophers/graphql-go/relay"
)


type Args struct {
Name string
Data types.Map
}


type mutation struct{}

func (_ *mutation) Hello(args Args) string {

fmt.Println(args)

return "Args accept!"
}

func main() {
s := `
scalar Map
type Query {}
type Mutation {
hello(
name: String!
data: Map!
): String!
}
`
schema := graphql.MustParseSchema(s, &mutation{})
http.Handle("/query", &relay.Handler{Schema: schema})

log.Println("Listen in port :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
19 changes: 19 additions & 0 deletions example/scalar_map/types/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package types

import "fmt"

type Map map[string]interface {}

func (Map) ImplementsGraphQLType(name string) bool {
return name == "Map"
}

func (j *Map) UnmarshalGraphQL(input interface{}) error {
json, ok := input.(map[string]interface{})
if !ok {
return fmt.Errorf("wrong type")
}

*j = json
return nil
}
2 changes: 2 additions & 0 deletions internal/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,9 @@ func validateValueType(c *opContext, v types.Value, t types.Type) (bool, string)
if validateBasicLit(lit, t) {
return true, ""
}
return false, fmt.Sprintf("Expected type %q, found %s.", t, v)
}
return true, ""

case *types.List:
list, ok := v.(*types.ListValue)
Expand Down

0 comments on commit 5457f60

Please sign in to comment.