Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only Inject Builtin Scalars if Defined in Schema #635

Merged
merged 3 commits into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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