Created
May 17, 2023 14:37
-
-
Save huantt/649fe4478d30164df845001eae45d956 to your computer and use it in GitHub Desktop.
Json scalar implementation for github.com/graph-gophers/graphql-go
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
package graphql | |
import ( | |
"encoding/json" | |
) | |
type Json map[string]interface{} | |
func (Json) ImplementsGraphQLType(name string) bool { | |
return name == "Json" | |
} | |
func (m *Json) UnmarshalGraphQL(input interface{}) error { | |
var bytes []byte | |
switch input.(type) { | |
case []byte: | |
bytes = input.([]byte) | |
default: | |
var err error | |
bytes, err = json.Marshal(input) | |
if err != nil { | |
return err | |
} | |
} | |
return json.Unmarshal(bytes, m) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use it: