gqlgen无法识别自定义类型。

huangapple go评论66阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2022年7月28日 21:09:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/73153517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定