英文:
gqlgen failing to recognize custom type
问题
我的gqlgen
模型:
models:
Int64:
model:
- github.com/99designs/gqlgen/graphql.Int64
ID:
model:
- github.com/99designs/gqlgen/graphql.ID
- github.com/99designs/gqlgen/graphql.Int
- github.com/99designs/gqlgen/graphql.Int64
- github.com/99designs/gqlgen/graphql.Int32
Int:
model:
- github.com/99designs/gqlgen/graphql.Int
- github.com/99designs/gqlgen/graphql.Int32
- github.com/99designs/gqlgen/graphql.Int64
在schema.graphql
中:
type Value {
t: Int64!
}
我一直得到错误信息:failed to load schema: graph/schema.graphqls:97: Undefined type Int64.
我不确定原因是什么。我尝试添加一个自定义类型并在models
中引用它,但是问题仍然存在。
func MarshalInt64(t int64) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, _ = io.WriteString(w, strconv.FormatInt(t, 10))
})
}
func UnmarshalInt64(v interface{}) (int64, error) {
if res, ok := v.(json.Number); ok {
return res.Int64()
}
if res, ok := v.(string); ok {
return json.Number(res).Int64()
}
if res, ok := v.(int64); ok {
return res, nil
}
if res, ok := v.(*int64); ok {
return *res, nil
}
return 0, fmt.Errorf("could not convert %v of type %T to Int64", v, v)
}
但是仍然出现相同的问题。有什么想法吗?
英文:
My gqlgen
models:
models:
Int64:
model:
- github.com/99designs/gqlgen/graphql.Int64
ID:
model:
- github.com/99designs/gqlgen/graphql.ID
- github.com/99designs/gqlgen/graphql.Int
- github.com/99designs/gqlgen/graphql.Int64
- github.com/99designs/gqlgen/graphql.Int32
Int:
model:
- github.com/99designs/gqlgen/graphql.Int
- github.com/99designs/gqlgen/graphql.Int32
- github.com/99designs/gqlgen/graphql.Int64
in schema.graphql:
type Value{
t: Int64!
}
I keep getting : failed to load schema: graph/schema.graphqls:97: Undefined type Int64.
and I'm unsure as to why. I tried adding a custom type myself and referencing that in models
func MarshalInt64(t int64) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, _ = io.WriteString(w, strconv.FormatInt(t, 10))
})
}
func UnmarshalInt64(v interface{}) (int64, error) {
if res, ok := v.(json.Number); ok {
return res.Int64()
}
if res, ok := v.(string); ok {
return json.Number(res).Int64()
}
if res, ok := v.(int64); ok {
return res, nil
}
if res, ok := v.(*int64); ok {
return *res, nil
}
return 0, fmt.Errorf("could not convert %v of type %T to Int64", v, v)
}
But same problem happens. Any ideas?
答案1
得分: 1
我找到了问题所在,似乎你需要直接将自定义类型添加到你的模式中。
将 scalar Int64
添加到我的 schema.graphqls
中解决了这个问题。
英文:
I figured out what the issue was, seems like you need to add the custom type directly to your schema.
Adding scalar Int64
to my schema.graphqls
fixed the issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论