Skip to content

Commit

Permalink
Add docs for built-in scalar implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathew Byrne committed Mar 18, 2019
1 parent 7f87a03 commit ae14198
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions docs/content/reference/scalars.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
---
linkTitle: Custom Scalars
title: Using custom graphql types in golang
description: Defining custom GraphQL scalar types using gqlgen
linkTitle: Scalars
title: Mapping GraphQL scalar types to Go types
description: Mapping GraphQL scalar types to Go types
menu: { main: { parent: 'reference' } }
---

There are two different ways to implement scalars in gqlgen, depending on your need.
## Built-in helpers

gqlgen ships with two built-in helpers for common custom scalar use-cases, `Time` and `Map`. Adding either of these to a schema will automatically add the marshalling behaviour to Go types.

### Time

```graphql
scalar Time
```

Maps a `Time` GraphQL scalar to a Go `time.Time` struct.

### Map

```graphql
scalar Map
```

Maps an arbitrary GraphQL value to a `map[string]{interface}` Go type.

## Custom scalars with user defined types

## With user defined types
For user defined types you can implement the graphql.Marshal and graphql.Unmarshal interfaces and they will be called.

```go
Expand Down Expand Up @@ -55,7 +73,7 @@ models:
```
## Custom scalars for types you don't control
## 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:
Expand Down

0 comments on commit ae14198

Please sign in to comment.