Replies: 9 comments
-
Obviously not optimal, but this worked for specifically handling named diff --git a/codegen/config/binder.go b/codegen/config/binder.go
index 72956de..b420ec0 100644
--- a/codegen/config/binder.go
+++ b/codegen/config/binder.go
@@ -206,6 +206,17 @@ func (ref *TypeReference) Elem() *TypeReference {
}
if ref.IsSlice() {
+ if ref.IsNamed() {
+ return &TypeReference{
+ GO: ref.GO.Underlying().(*types.Slice).Elem(),
+ GQL: ref.GQL.Elem,
+ CastType: ref.CastType,
+ Definition: ref.Definition,
+ Unmarshaler: ref.Unmarshaler,
+ Marshaler: ref.Marshaler,
+ IsMarshaler: ref.IsMarshaler,
+ }
+ }
return &TypeReference{
GO: ref.GO.(*types.Slice).Elem(),
GQL: ref.GQL.Elem,
@@ -232,6 +243,10 @@ func (t *TypeReference) IsNilable() bool {
}
func (t *TypeReference) IsSlice() bool {
+ if t.IsNamed() {
+ _, isSlice := t.GO.Underlying().(*types.Slice)
+ return t.GQL.Elem != nil && isSlice
+ }
_, isSlice := t.GO.(*types.Slice)
return t.GQL.Elem != nil && isSlice
}
diff --git a/codegen/type.gotpl b/codegen/type.gotpl
index cb2782c..4d74abc 100644
--- a/codegen/type.gotpl
+++ b/codegen/type.gotpl
@@ -17,14 +17,25 @@
}
}
var err error
- res := make([]{{$type.GO.Elem | ref}}, len(vSlice))
- for i := range vSlice {
- res[i], err = ec.{{ $type.Elem.UnmarshalFunc }}(ctx, vSlice[i])
- if err != nil {
- return nil, err
+ {{- if $type.IsNamed }}
+ res := make([]{{$type.GO.Underlying.Elem | ref}}, len(vSlice))
+ for i := range vSlice {
+ res[i], err = ec.{{ $type.Elem.UnmarshalFunc }}(ctx, vSlice[i])
+ if err != nil {
+ return nil, err
+ }
}
- }
- return res, nil
+ return res, nil
+ {{- else }}
+ res := make([]{{$type.GO.Elem | ref}}, len(vSlice))
+ for i := range vSlice {
+ res[i], err = ec.{{ $type.Elem.UnmarshalFunc }}(ctx, vSlice[i])
+ if err != nil {
+ return nil, err
+ }
+ }
+ return res, nil
+ {{- end }}
{{- else }}
{{- if $type.Unmarshaler }}
{{- if $type.CastType }}
diff --git a/internal/code/compare.go b/internal/code/compare.go
index dce9aea..9425f39 100644
--- a/internal/code/compare.go
+++ b/internal/code/compare.go
@@ -25,6 +25,14 @@ func CompatibleTypes(expected types.Type, actual types.Type) error {
}
}
+ {
+ if actual, ok := actual.(*types.Named); ok {
+ if actual, ok := actual.Underlying().(*types.Slice); ok {
+ return CompatibleTypes(expected, actual)
+ }
+ }
+ }
+
switch expected := expected.(type) {
case *types.Slice:
if actual, ok := actual.(*types.Slice); ok { Maybe it'll be useful to someone for ideas. |
Beta Was this translation helpful? Give feedback.
-
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Beta Was this translation helpful? Give feedback.
-
I can confirm this is still an issue in v0.9.3 |
Beta Was this translation helpful? Give feedback.
-
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Beta Was this translation helpful? Give feedback.
-
It would be interesting for mapping of |
Beta Was this translation helpful? Give feedback.
-
I have a named slice I think this is related |
Beta Was this translation helpful? Give feedback.
-
Any plan on supporting this in some future version? |
Beta Was this translation helpful? Give feedback.
-
Temporary solution:
|
Beta Was this translation helpful? Give feedback.
-
What happened?
I was hoping that gqlgen would transparently handle resolution of a struct field of an opaque slice type:
Which I am using in my models to have them deserialize from a PostgreSQL array column. For example:
With a schema of:
However, gqlgen expects a resolver to be generated for this even though the it could transparently handle marshaling and unmarshaling as it supports doing so with the underlying
[]string
.It appears opaque string types are specifically handed to avoid needing to defining a field resolver.
gqlgen/codegen/config/binder.go
Lines 364 to 377 in 414a4d3
So there is some precedence.
I poked around trying to add this to the binder, but couldn't get it working quite right. I figured I'd open this issue at least to track and see if this would be helpful to others.
What did you expect?
gqlgen would generate resolvers to marshal a
StringSlice
.Minimal graphql.schema and models to reproduce
versions
gqlgen version 0.9.1
go version 12.6.1
Beta Was this translation helpful? Give feedback.
All reactions