Skip to content

Commit

Permalink
Merge pull request 99designs#635 from 99designs/fix-inject-builtin-sc…
Browse files Browse the repository at this point in the history
…alars

Only Inject Builtin Scalars if Defined in Schema
  • Loading branch information
Mathew Byrne authored Mar 18, 2019
2 parents 22b622c + 3142152 commit 21449e5
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 8 deletions.
14 changes: 12 additions & 2 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,6 @@ func (c *Config) InjectBuiltins(s *ast.Schema) {
"Float": {Model: StringList{"github.com/99designs/gqlgen/graphql.Float"}},
"String": {Model: StringList{"github.com/99designs/gqlgen/graphql.String"}},
"Boolean": {Model: StringList{"github.com/99designs/gqlgen/graphql.Boolean"}},
"Time": {Model: StringList{"github.com/99designs/gqlgen/graphql.Time"}},
"Map": {Model: StringList{"github.com/99designs/gqlgen/graphql.Map"}},
"Int": {Model: StringList{
"github.com/99designs/gqlgen/graphql.Int",
"github.com/99designs/gqlgen/graphql.Int32",
Expand All @@ -362,6 +360,18 @@ func (c *Config) InjectBuiltins(s *ast.Schema) {
c.Models[typeName] = entry
}
}

// These are additional types that are injected if defined in the schema as scalars.
extraBuiltins := TypeMap{
"Time": {Model: StringList{"github.com/99designs/gqlgen/graphql.Time"}},
"Map": {Model: StringList{"github.com/99designs/gqlgen/graphql.Map"}},
}

for typeName, entry := range extraBuiltins {
if t, ok := s.Types[typeName]; ok && t.Kind == ast.Scalar {
c.Models[typeName] = entry
}
}
}

func (c *Config) LoadSchema() (*ast.Schema, map[string]string, error) {
Expand Down
8 changes: 8 additions & 0 deletions codegen/testserver/builtinscalar.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

"""
Since gqlgen defines default implementation for a Map scalar, this tests that the builtin is _not_
added to the TypeMap
"""
type Map {
id: ID!
}
74 changes: 74 additions & 0 deletions codegen/testserver/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions codegen/testserver/models-gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 21449e5

Please sign in to comment.