Skip to content

Commit

Permalink
Add comments and docs for pointer scalars
Browse files Browse the repository at this point in the history
  • Loading branch information
lwc committed Aug 13, 2020
1 parent 997efd0 commit bef9c8b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
6 changes: 3 additions & 3 deletions codegen/config/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ func (b *Binder) PointerTo(ref *TypeReference) *TypeReference {
type TypeReference struct {
Definition *ast.Definition
GQL *ast.Type
GO types.Type
Target types.Type
GO types.Type // Type of the field being bound. Could be a pointer or a value type of Target.
Target types.Type // The actual type that we know how to bind to. May require pointer juggling when traversing to fields.
CastType types.Type // Before calling marshalling functions cast from/to this base type
Marshaler *types.Func // When using external marshalling functions this will point to the Marshal function
Unmarshaler *types.Func // When using external marshalling functions this will point to the Unmarshal function
Expand Down Expand Up @@ -357,7 +357,7 @@ func (b *Binder) TypeReference(schemaType *ast.Type, bindTarget types.Type) (ret
ref.GO = obj.Type()
ref.IsMarshaler = true
} else if underlying := basicUnderlying(obj.Type()); def.IsLeafType() && underlying != nil && underlying.Kind() == types.String {
// Special case for named types wrapping strings. Used by default enum implementations.
// TODO delete before v1. Backwards compatibility case for named types wrapping strings (see #595)

ref.GO = obj.Type()
ref.CastType = underlying
Expand Down
12 changes: 7 additions & 5 deletions docs/content/reference/scalars.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ package mypkg
import (
"fmt"
"io"
"strings"
)

type YesNo bool
Expand All @@ -69,7 +68,7 @@ type YesNo bool
func (y *YesNo) UnmarshalGQL(v interface{}) error {
yes, ok := v.(string)
if !ok {
return fmt.Errorf("points must be strings")
return fmt.Errorf("YesNo must be a string")
}

if yes == "yes" {
Expand All @@ -90,7 +89,7 @@ func (y YesNo) MarshalGQL(w io.Writer) {
}
```

and then in .gqlgen.yml point to the name without the Marshal|Unmarshal in front:
and then wire up the type in .gqlgen.yml or via directives like normal:

```yaml
models:
Expand All @@ -100,8 +99,8 @@ models:
## Custom scalars with third party types
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:
Sometimes you are unable to add add methods to a type - perhaps you don't own the type, or it is part of the standard
library (eg string or time.Time). To support this we can build an external marshaler:
```go
package mypkg
Expand Down Expand Up @@ -147,6 +146,9 @@ models:
model: github.com/me/mypkg.MyCustomBooleanScalar
```
**Note:** you also can un/marshal to pointer types via this approach, simply accept a pointer in your
`Marshal...` func and return one in your `Unmarshal...` func.

See the [example/scalars](https://github.com/99designs/gqlgen/tree/master/example/scalars) package for more examples.

## Unmarshaling Errors
Expand Down

0 comments on commit bef9c8b

Please sign in to comment.