Skip to content

Commit ad3c589

Browse files
Add scalar json
1 parent ebb1e19 commit ad3c589

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

boilergql/scaler_json.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package boilergql
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
)
8+
9+
type JSON map[string]interface{}
10+
11+
func (b *JSON) UnmarshalGQL(v interface{}) error {
12+
bytes, err := json.Marshal(v)
13+
if err != nil {
14+
return err
15+
}
16+
return json.Unmarshal(bytes, b)
17+
}
18+
19+
func (b JSON) MarshalGQL(w io.Writer) {
20+
bytes, err := json.Marshal(b)
21+
if err != nil {
22+
fmt.Fprintf(w, `"%s"`, err.Error())
23+
return
24+
}
25+
_, _ = w.Write(bytes)
26+
}

0 commit comments

Comments
 (0)