英文:
Unable to use UUID for ID types within gqlgen's GraphQL models
问题
我正在尝试在Go模型中使用UUID
类型(来自模块github.com/gofrs/uuid
)。通常,除非我知道它们不会被增强,否则我会手动定义模型。这是我目前的代码:
package model
import "github.com/gofrs/uuid"
type Category struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Picture string `json:"picture,omitempty"`
}
# GraphQL Schema
...
type Category {
id: ID!
name: String!
description: String!
picture: String
}
gqlgen
配置文件如下:
autobind:
- <module-id>/path/to/model
...
models:
ID:
model:
- github.com/99designs/gqlgen/graphql.ID
- github.com/gofrs/uuid.UUID
Int:
model:
- github.com/99designs/gqlgen/graphql.Int
- github.com/99designs/gqlgen/graphql.Int32
- github.com/99designs/gqlgen/graphql.Int64
...
schema:
- graph/*.graphql
struct_tag: json
使用这个设置,我收到以下错误消息:
validation failed: packages.Load: /path/to/application/graph/prelude.generated.go:2177:17: ec.unmarshalInputID undefined (type *executionContext has no field or method unmarshalInputID)
/path/to/application/graph/prelude.generated.go:2182:12: ec._ID undefined (type *executionContext has no field or method _ID)
exit status 1
graph/resolver.go:3: running "go": exit status 1
是否有一种方法可以在ID
上 "本地" 使用UUID
类型,而不必使用string
类型并手动进行自动转换?
我曾以为这个一般用例已经被框架覆盖了 🤔
更新
我已经提交了一个拉取请求,希望这项工作对大多数人都有好处,因为UUID现在被更多地使用(或采用)。
英文:
I'm trying to use a UUID
type (from module github.com/gofrs/uuid
) within Go
models. I do usually define models manually, unless I know they won't be augmented. This is what I currently have:
package model
import "github.com/gofrs/uuid"
type Category struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Picture string `json:"picture,omitempty"`
}
# GraphQL Schema
...
type Category {
id: ID!
name: String!
description: String!
picture: String
}
The gqlgen
configuration file looks like:
autobind:
- <module-id>/path/to/model
...
models:
ID:
model:
- github.com/99designs/gqlgen/graphql.ID
- github.com/gofrs/uuid.UUID
Int:
model:
- github.com/99designs/gqlgen/graphql.Int
- github.com/99designs/gqlgen/graphql.Int32
- github.com/99designs/gqlgen/graphql.Int64
...
schema:
- graph/*.graphql
struct_tag: json
With this setup, I get this error message:
validation failed: packages.Load: /path/to/application/graph/prelude.generated.go:2177:17: ec.unmarshalInputID undefined (type *executionContext has no field or method unmarshalInputID)
/path/to/application/graph/prelude.generated.go:2182:12: ec._ID undefined (type *executionContext has no field or method _ID)
exit status 1
graph/resolver.go:3: running "go": exit status 1
Is there a way to use the UUID
type "natively" for ID
s without resorting in string
types and doing the auto-conversion manually?
I was under the assumption that this general use case was covered by the framework 🤔
Updates
I went ahead and send a pull request for this work. I think this should be something most people will benefit from these days, where UUIDs are used (or adopted) more often.
答案1
得分: 0
这个实现应该可以工作(需要对包名称、标识符等进行一些调整):
// uuid.go
package graphql
import (
"errors"
"io"
"github.com/gofrs/uuid"
)
func MarshalUUID(t uuid.UUID) Marshaler {
if t.IsNil() {
return Null
}
return WriterFunc(func(w io.Writer) {
_, _ = io.WriteString(w, t.String())
})
}
func UnmarshalUUID(v interface{}) (uuid.UUID, error) {
if str, ok := v.(string); ok {
return uuid.FromString(str)
}
return uuid.Nil, errors.New("input must be an RFC-4122 formatted string")
}
我创建了一个拉取请求,其中包含这个完全的实现。如果合并,将不再需要这个,因为它将成为内置行为。
英文:
This implementation should work (with some adjustments around packages names, identifiers, etc.):
// uuid.go
package graphql
import (
"errors"
"io"
"github.com/gofrs/uuid"
)
func MarshalUUID(t uuid.UUID) Marshaler {
if t.IsNil() {
return Null
}
return WriterFunc(func(w io.Writer) {
_, _ = io.WriteString(w, t.String())
})
}
func UnmarshalUUID(v interface{}) (uuid.UUID, error) {
if str, ok := v.(string); ok {
return uuid.FromString(str)
}
return uuid.Nil, errors.New("input must be an RFC-4122 formatted string")
}
> I created a pull request with this exact implementation. If merged, this wouldn't be needed anymore because it will be a built-in behavior.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论