Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gqlgen failing to recognize custom type

Tags:

go

gqlgen

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?

like image 932
Nicole Staline Avatar asked Sep 16 '25 04:09

Nicole Staline


1 Answers

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.

like image 116
Nicole Staline Avatar answered Sep 17 '25 19:09

Nicole Staline