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

Adds default custom scalar of interface{} #686

Merged
merged 5 commits into from
May 8, 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
1 change: 1 addition & 0 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ func (c *Config) InjectBuiltins(s *ast.Schema) {
extraBuiltins := TypeMap{
"Time": {Model: StringList{"github.com/99designs/gqlgen/graphql.Time"}},
"Map": {Model: StringList{"github.com/99designs/gqlgen/graphql.Map"}},
"Any": {Model: StringList{"github.com/99designs/gqlgen/graphql.Any"}},
}

for typeName, entry := range extraBuiltins {
Expand Down
14 changes: 11 additions & 3 deletions docs/content/reference/scalars.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ menu: { main: { parent: 'reference' } }

## 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.
gqlgen ships with three built-in helpers for common custom scalar use-cases, `Time`, `Any` and `Map`. Adding any of these to a schema will automatically add the marshalling behaviour to Go types.

### Time

Expand All @@ -25,6 +25,14 @@ scalar Map

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

### Any

```graphql
scalar Any
```

Maps an arbitrary GraphQL value to a `interface{}` Go type.

## Custom scalars with user defined types

For user defined types you can implement the graphql.Marshal and graphql.Unmarshal interfaces and they will be called.
Expand Down Expand Up @@ -75,7 +83,7 @@ models:

## Custom scalars with third party types

Sometimes you cant add methods to a type because its in another repo, part of the standard
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
Expand All @@ -85,7 +93,7 @@ import (
"fmt"
"io"
"strings"

"github.com/99designs/gqlgen/graphql"
)

Expand Down
19 changes: 19 additions & 0 deletions graphql/any.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package graphql

import (
"encoding/json"
"io"
)

func MarshalAny(v interface{}) Marshaler {
return WriterFunc(func(w io.Writer) {
err := json.NewEncoder(w).Encode(v)
if err != nil {
panic(err)
}
})
}

func UnmarshalAny(v interface{}) (interface{}, error) {
return v, nil
}