99designs / 99designs/gqlgen

Proposal: Remove use of graphql.Omittable in unmarshallers

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

描述

### Proposal: Remove use of [graphql.Omittable](https://github.com/99designs/gqlgen/blob/master/graphql/omittable.go) in unmarshallers

_Originally posted by @deitrix in [#3227](https://github.com/99designs/gqlgen/issues/3227#issuecomment-4060287904)_ where they wrote:

> I propose foregoing the use of `graphql.Omittable` in unmarshallers. This potentially removes a library dependency where it may not be needed, and makes the use of generics optional too.

For concrete types that are omittable, the function signature could be:
```go
// play.ground/types
package types

import "play.ground/pkg/null"

func UnmarshalOptionalString(s *string, isSet bool) null.String {
...
}
```

And then for the generic case
```go
// play.ground/types
package types

import "play.ground/pkg/optional"

func UnmarshalOptional[T any](v *T, isSet bool) optional.Of[T] {
...
}
```

For this, I'm also assuming we don't need any `MarshalXXX` functions, like with regular type mappers, as it only makes sense to use the omittable flag on input types.

As for configuration, I think `omittableTypes` should be reserved for the generic case, and then fall back to using `models` for overriding the default global omittable configuration.
```yaml
omittableTypes:
- play.ground/types.Optional

models:
String:
model:
- github.com/99designs/gqlgen/graphql.String
omittableTypes:
- play.ground/types.OptionalString
- play.ground/types.Optional[string] # also possible
```

Like with `model` configuration, the first type will be the default used for model generation, and then gqlgen will figure out which is the best one to use for the case of custom model binding. For example, given the above config, modelgen would generate:
```go
package model

type T struct {
Field types.OptionalString
}
```

But, if you were to bind the graphql type `T` to a Go type `T` with a `Field` of type `types.Optional[string]`, it would use the appropriate unmarshal func.

The default configuration would effectively be
```yaml
omittableTypes:
- github.com/99designs/gqlgen/graphql.Omittable
```

The `goField` directive can be extended with two new fields, similar to the `goModel` directive:
```graphql
directive @goField(
...
omittableType: String
omittableTypes: [String!]
)
```

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

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

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