Skip to content

Commit

Permalink
Custom scalar docs
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Mar 17, 2018
1 parent e6ed4de commit d0211a0
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 112 deletions.
3 changes: 3 additions & 0 deletions docs/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ menu:
main:
- name: Tutorials
identifier: tutorial
weight: 10
- name: Reference
identifier: reference
106 changes: 106 additions & 0 deletions docs/content/reference/scalars.md
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.
4 changes: 3 additions & 1 deletion docs/static/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ nav {

header {
background: #e9ebed;
padding: 45px;
padding: 45px 20px;
overflow:hidden;
}

footer {
Expand Down Expand Up @@ -135,6 +136,7 @@ ul.menu {
ul.submenu {
margin-left: 15px;
list-style: none;
margin-bottom: 0;
}

.menu {
Expand Down
1 change: 0 additions & 1 deletion docs/static/syntax.css
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
.chroma code {
overflow: auto;
display: block;
width:100%;
background-color: #e7edef;
padding: 5px;
margin-bottom: 1em;
Expand Down
53 changes: 0 additions & 53 deletions example/scalars/advanced_scalars.md

This file was deleted.

57 changes: 0 additions & 57 deletions example/scalars/readme.md

This file was deleted.

0 comments on commit d0211a0

Please sign in to comment.