You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

gqlgen无法识别自定义Int64类型,报错Undefined type Int64求助

解决gqlgen中Int64类型未定义的报错

核心原因

GraphQL标准标量类型里没有Int64,必须先在Schema中显式声明这个自定义标量,再通过gqlgen配置关联到Go的类型处理逻辑。

步骤1:在Schema中声明Int64标量

修改schema.graphql,先添加自定义标量的声明,再使用该类型:

# 先声明自定义标量Int64
scalar Int64

type Value{
    t: Int64!
}

步骤2:确保gqlgen配置正确映射

检查你的gqlgen.yml配置,根据使用的类型调整映射:

  • 若使用gqlgen自带的graphql.Int64,配置保持现有内容即可:
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
  • 若使用你自己定义的Int64类型(带自定义Marshal/Unmarshal),需将映射指向你自己的类型路径:
models:
    Int64:
        model:
            - your/project/package.Int64  # 替换成自定义Int64类型的实际包路径
    # 其他ID、Int配置不变

步骤3:确保自定义处理逻辑被识别

如果你用了自己写的MarshalInt64和UnmarshalInt64函数,需完成两个操作:

  1. 保证这些函数和自定义Int64类型在同一个包下
  2. 在gqlgen.yml中添加autobind配置,让gqlgen扫描到该类型:
autobind:
  - your/project/package  # 替换成包含自定义Int64类型的包路径

验证操作

执行gqlgen代码生成命令,确认报错消失:

go run github.com/99designs/gqlgen generate

内容的提问来源于stack exchange,提问作者Nicole Staline

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 11:03:20