I would like to use a type provided by 3rd party package. When the type does not implement neither graphql.Marshaler nor json.Marshaler, the result of MarshalJSON generates an unexpected string in some cases.
For example, assuming using github.com/google/uuid.uuid.UUID in a variable,
This is an example schema.
type Query {
uuidToString(uuid: UUID!): String!
}
scalar UUID
Bind the scalar to in gqlgenc.yaml
models:
UUID:
model: github.com/google/uuid.UUID
Then, gqlgenc generates a code to marshal a variable like this:
vars := map[string]any{
"uuid": uuid.New(),
}
b, err := clientv2.MarshalJSON(vars)
...
fmt.Printf("json: %s\n", b)
So I think it's better to respect encoding.TextMarshaler in MarshalJSON too. The current implementation of MarshalJSON calls json.Marshaler.MarshalJSON if a value implements json.Marshaler interface.
I would like to use a type provided by 3rd party package. When the type does not implement neither graphql.Marshaler nor json.Marshaler, the result of MarshalJSON generates an unexpected string in some cases.
For example, assuming using github.com/google/uuid.uuid.UUID in a variable,
This is an example schema.
Bind the scalar to in gqlgenc.yaml
Then, gqlgenc generates a code to marshal a variable like this:
This generates a json value,
However, if we use json.Marshal directly instead of MarshalJSON, they generate different json values.
This is because json.Marshal's encoder supports encoding.TextMarshaler. The description: https://cs.opensource.google/go/go/+/master:src/encoding/json/encode.go;l=32-40 The implementation: https://cs.opensource.google/go/go/+/master:src/encoding/json/encode.go;l=379-395
So I think it's better to respect encoding.TextMarshaler in MarshalJSON too. The current implementation of MarshalJSON calls json.Marshaler.MarshalJSON if a value implements json.Marshaler interface.