登录
首页 >  Golang >  Go问答

gqlgen 无法处理自定义类型

来源:stackoverflow

时间:2024-03-22 19:09:28 199浏览 收藏

在使用 gqlgen 时,遇到“未定义类型 int64”错误。这是因为 gqlgen 无法处理自定义类型。解决方案是将自定义类型直接添加到架构中,如在 schema.graphqls 中添加标量 Int64。

问题内容

我的 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!
}

我不断收到:无法加载架构:graph/schema.graphqls:97:未定义类型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)
}

但是同样的问题发生了。有什么想法吗?


正确答案


我找出了问题所在,似乎您需要将自定义类型直接添加到您的架构中。

scalar Int64 添加到我的 schema.graphqls 解决了该问题。

以上就是《gqlgen 无法处理自定义类型》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>