-
Notifications
You must be signed in to change notification settings - Fork 490
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Accepting value Json in parameter of request's body in custom Scalar (#…
…467) Accept JSON value in resolver args
- Loading branch information
1 parent
af5bb93
commit 5457f60
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters