From 1de25d0c65d549e10684d55e674a1ec1748aea86 Mon Sep 17 00:00:00 2001 From: Quest Henkart Date: Mon, 22 Apr 2019 13:31:47 +0100 Subject: [PATCH 1/5] adds interface scalar type --- codegen/config/config.go | 5 +++-- graphql/interface.go | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 graphql/interface.go diff --git a/codegen/config/config.go b/codegen/config/config.go index 0c72420e602..51c088787d6 100644 --- a/codegen/config/config.go +++ b/codegen/config/config.go @@ -363,8 +363,9 @@ func (c *Config) InjectBuiltins(s *ast.Schema) { // 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"}}, + "Time": {Model: StringList{"github.com/99designs/gqlgen/graphql.Time"}}, + "Map": {Model: StringList{"github.com/99designs/gqlgen/graphql.Map"}}, + "Interface": {Model: StringList{"github.com/99designs/gqlgen/graphql.Interface"}}, } for typeName, entry := range extraBuiltins { diff --git a/graphql/interface.go b/graphql/interface.go new file mode 100644 index 00000000000..1589dc431b7 --- /dev/null +++ b/graphql/interface.go @@ -0,0 +1,21 @@ +package graphql + +import ( + "encoding/json" + "io" + + "github.com/qhenkart/gqlgen/graphql" +) + +func MarshalInterface(v interface{}) Marshaler { + return graphql.WriterFunc(func(w io.Writer) { + err := json.NewEncoder(w).Encode(val) + if err != nil { + panic(err) + } + }) +} + +func UnmarshalInterface(v interface{}) (interface{}, error) { + return v, nil +} From ca96a1550d4581d9aef9532b8eacdbcf6e0122a0 Mon Sep 17 00:00:00 2001 From: Quest Henkart Date: Mon, 22 Apr 2019 13:33:01 +0100 Subject: [PATCH 2/5] update docs --- docs/content/reference/scalars.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/docs/content/reference/scalars.md b/docs/content/reference/scalars.md index ec9a9fcf0b6..0590a7b783d 100644 --- a/docs/content/reference/scalars.md +++ b/docs/content/reference/scalars.md @@ -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 two built-in helpers for common custom scalar use-cases, `Time` `Interface` and `Map`. Adding either of these to a schema will automatically add the marshalling behaviour to Go types. ### Time @@ -25,6 +25,14 @@ scalar Map Maps an arbitrary GraphQL value to a `map[string]{interface}` Go type. +### Interface + +```graphql +scalar Interface +``` + +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. @@ -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 @@ -85,7 +93,7 @@ import ( "fmt" "io" "strings" - + "github.com/99designs/gqlgen/graphql" ) From 85643f5dad6ff410ebb78f26539e845f38df37e1 Mon Sep 17 00:00:00 2001 From: Quest Henkart Date: Mon, 22 Apr 2019 14:08:39 +0100 Subject: [PATCH 3/5] fix import --- graphql/interface.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/graphql/interface.go b/graphql/interface.go index 1589dc431b7..0dfdb80a4dc 100644 --- a/graphql/interface.go +++ b/graphql/interface.go @@ -3,13 +3,11 @@ package graphql import ( "encoding/json" "io" - - "github.com/qhenkart/gqlgen/graphql" ) func MarshalInterface(v interface{}) Marshaler { - return graphql.WriterFunc(func(w io.Writer) { - err := json.NewEncoder(w).Encode(val) + return WriterFunc(func(w io.Writer) { + err := json.NewEncoder(w).Encode(v) if err != nil { panic(err) } From 89c873459212f48d95e16d87d5732460d229255e Mon Sep 17 00:00:00 2001 From: Quest Henkart Date: Mon, 22 Apr 2019 14:12:57 +0100 Subject: [PATCH 4/5] fix grammar in docs --- docs/content/reference/scalars.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/content/reference/scalars.md b/docs/content/reference/scalars.md index 0590a7b783d..888a3bd4d92 100644 --- a/docs/content/reference/scalars.md +++ b/docs/content/reference/scalars.md @@ -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` `Interface` 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`, `Interface` and `Map`. Adding any of these to a schema will automatically add the marshalling behaviour to Go types. ### Time From bf2d07a47fa57b27434a02021ca8ed868019ba9c Mon Sep 17 00:00:00 2001 From: Quest Henkart Date: Wed, 1 May 2019 10:41:59 +0200 Subject: [PATCH 5/5] moves naming convention to a non-go standard --- codegen/config/config.go | 6 +++--- docs/content/reference/scalars.md | 6 +++--- graphql/{interface.go => any.go} | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) rename graphql/{interface.go => any.go} (63%) diff --git a/codegen/config/config.go b/codegen/config/config.go index 51c088787d6..502422de440 100644 --- a/codegen/config/config.go +++ b/codegen/config/config.go @@ -363,9 +363,9 @@ func (c *Config) InjectBuiltins(s *ast.Schema) { // 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"}}, - "Interface": {Model: StringList{"github.com/99designs/gqlgen/graphql.Interface"}}, + "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 { diff --git a/docs/content/reference/scalars.md b/docs/content/reference/scalars.md index 888a3bd4d92..0ac35d4d53a 100644 --- a/docs/content/reference/scalars.md +++ b/docs/content/reference/scalars.md @@ -7,7 +7,7 @@ menu: { main: { parent: 'reference' } } ## Built-in helpers -gqlgen ships with three built-in helpers for common custom scalar use-cases, `Time`, `Interface` and `Map`. Adding any 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 @@ -25,10 +25,10 @@ scalar Map Maps an arbitrary GraphQL value to a `map[string]{interface}` Go type. -### Interface +### Any ```graphql -scalar Interface +scalar Any ``` Maps an arbitrary GraphQL value to a `interface{}` Go type. diff --git a/graphql/interface.go b/graphql/any.go similarity index 63% rename from graphql/interface.go rename to graphql/any.go index 0dfdb80a4dc..6ea8bf2eaeb 100644 --- a/graphql/interface.go +++ b/graphql/any.go @@ -5,7 +5,7 @@ import ( "io" ) -func MarshalInterface(v interface{}) Marshaler { +func MarshalAny(v interface{}) Marshaler { return WriterFunc(func(w io.Writer) { err := json.NewEncoder(w).Encode(v) if err != nil { @@ -14,6 +14,6 @@ func MarshalInterface(v interface{}) Marshaler { }) } -func UnmarshalInterface(v interface{}) (interface{}, error) { +func UnmarshalAny(v interface{}) (interface{}, error) { return v, nil }