99designs / 99designs/gqlgen

Embedded type with MarshalGQL and UnmarshalGQL funcs doesn't work to implement an enum

未關閉
#2,364 0 則留言 5 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Go
星號
10.8k
分支
1.3k
平均合併
2 天 36 分鐘
30 天內合併 PR
26

描述

### What happened?
I have a struct X that implements MarshalGQL, UnmarshalGQL, String, and IsValid funcs. I would like to create a struct Y using X as an embedded field and then gqlgen could recognize this composition to create a custom GQL enum. But gqlgen ignores the embedded field setting struct Y as not marshaler type.

Something like that:

```golang
type X[T any] struct{
ID string
Config Config
Value T
}

func (x X[T]) IsValid() bool
func (x X[T]) String() string
func (x *X[T]) UnmarshalGQL(v interface{}) error
func (x X[T]) MarshalGQL(w io.Writer)

type Y struct{
X[int64]
}
```

When I run generate command, it creates an invalid func:

```golang
func (ec *executionContext) unmarshalOYᚖgithubᚗcomᚋtestᚋmodelsᚐY(ctx context.Context, v interface{}) (*models.OthersFinantialIndex, error) {
if v == nil {
return nil, nil
}
res, err := ec.unmarshalInputY(ctx, v) // it doesn't compile because should use res.UnmarshalGQL(v)
return &res, graphql.ErrorOnPath(ctx, err)
}
```

### What did you expect?
When I run generate command, it recognizes MarshalGQL and UnmarshalGQL of the embedded field to create custom enums. The generated func could be like that:
```golang
func (ec *executionContext) unmarshalOYᚖgithubᚗcomᚋtestᚋmodelsᚐY(ctx context.Context, v interface{}) {
if v == nil {
return nil, nil
}
var res = new(models.Y)
err := res.UnmarshalGQL(v)
return res, graphql.ErrorOnPath(ctx, err)
}
```

### Minimal graphql.schema and models to reproduce
```graphql
enum Y {
TEST1
TEST2
TEST3
}
```

### versions
- `go run github.com/99designs/gqlgen version`?
v0.17.16
- `go version`?
1.18.3

Is it possible to use a struct with an embedded field with MarshalGQL func or would It be wrong?

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。