-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
112 additions
and
112 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 |
---|---|---|
|
@@ -13,3 +13,6 @@ menu: | |
main: | ||
- name: Tutorials | ||
identifier: tutorial | ||
weight: 10 | ||
- name: Reference | ||
identifier: reference |
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,106 @@ | ||
--- | ||
title: "Custom Scalars" | ||
description: Using custom graphql types in golang | ||
date: 2018-03-17T13:06:47+11:00 | ||
menu: {"main": {"parent": "reference"}} | ||
--- | ||
|
||
There are two different ways to implement scalars in gqlgen, depending on your need. | ||
|
||
|
||
## With user defined types | ||
For user defined types you can implement the graphql.Marshal and graphql.Unmarshal interfaces and they will be called. | ||
|
||
```go | ||
package mypkg | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"strings" | ||
) | ||
|
||
type YesNo bool | ||
|
||
// UnmarshalGQL implements the graphql.Marshaler interface | ||
func (y *YesNo) UnmarshalGQL(v interface{}) error { | ||
yes, ok := v.(string) | ||
if !ok { | ||
return fmt.Errorf("points must be strings") | ||
} | ||
|
||
if yes == "yes" { | ||
*y = true | ||
} else { | ||
*y = false | ||
} | ||
return nil | ||
} | ||
|
||
// MarshalGQL implements the graphql.Marshaler interface | ||
func (y YesNo) MarshalGQL(w io.Writer) { | ||
if y { | ||
w.Write([]byte(`"yes"`)) | ||
} else { | ||
w.Write([]byte(`"no"`)) | ||
} | ||
} | ||
``` | ||
|
||
and then in types.json point to the name without the Marshal|Unmarshal in front: | ||
```json | ||
{ | ||
"YesNo": "github.com/me/mypkg.YesNo" | ||
} | ||
``` | ||
|
||
|
||
## Custom scalars for types you don't control | ||
|
||
Sometimes you cant add methods to a type because its in another repo, part of the standard | ||
library (eg string or time.Time). To do this we can build an external marshaler: | ||
|
||
```go | ||
package mypkg | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
"github.com/vektah/gqlgen/graphql" | ||
) | ||
|
||
|
||
func MarshalMyCustomBooleanScalar(b bool) graphql.Marshaler { | ||
return graphql.WriterFunc(func(w io.Writer) { | ||
if b { | ||
w.Write([]byte("true")) | ||
} else { | ||
w.Write([]byte("false")) | ||
} | ||
}) | ||
} | ||
|
||
func UnmarshalMyCustomBooleanScalar(v interface{}) (bool, error) { | ||
switch v := v.(type) { | ||
case string: | ||
return "true" == strings.ToLower(v), nil | ||
case int: | ||
return v != 0, nil | ||
case bool: | ||
return v, nil | ||
default: | ||
return false, fmt.Errorf("%T is not a bool", v) | ||
} | ||
} | ||
``` | ||
|
||
and then in types.json point to the name without the Marshal|Unmarshal in front: | ||
```json | ||
{ | ||
"MyCustomBooleanScalar": "github.com/me/mypkg.MyCustomBooleanScalar" | ||
} | ||
``` | ||
|
||
see the [example/scalars](https://github.com/vektah/gqlgen/tree/master/example/scalars) package for more examples. |
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
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.