Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accepting value Json in parameter of request's body in custom Scalar #467

Merged
merged 5 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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