cppgraphqlgen icon indicating copy to clipboard operation
cppgraphqlgen copied to clipboard

Unable to create BigInt scalar

Open gitmodimo opened this issue 10 months ago • 4 comments

Both main and next does not really allow to implement custom scalars that cannot be represented by Value type. One example would be BigInt scalar. I should be possible to implement custom storage/serialize/deserialize for user defined scalars.

gitmodimo avatar Feb 27 '25 15:02 gitmodimo

The GraphQL spec was a little vague about custom scalars, IIRC. I think it said something about how they always have to be able to serialize to a string, but things like JSON objects work too for examples like the geo-coordinates.

In this case, wouldn't the BigInt scalar just need to use a string?

wravery avatar Mar 08 '25 01:03 wravery

I did't find any info about required serialization to string. String is only one example of scalar. I think serializing any custom acalar to string is a walkaround and not real scalar implementation. One could define Positive scalar that would represent nonnegative integer and serializing it as string does not make sense. AFAIK JSON specification of integer is not limited to 32bit int. So BigInt should be serialized to JSON as numer not as string. I think scalar token should be of abstract type that allow defining custom serialization/deserialization.

https://spec.graphql.org/October2021/#sec-Scalars.Custom-Scalars

gitmodimo avatar Mar 08 '25 11:03 gitmodimo

Ah, right. The bit about serialization to strings was specific to ID.

I think this could be done with a custom token/FieldResult type of std::any, perhaps carrying the scalar typename. Serialization would require plugging in a handler/callback somewhere. In the case of JSON variables, that would probably mean you'd have to parse the scalar type from a JSON fragment as a string.

Does this match what you're suggesting?

wravery avatar Mar 10 '25 18:03 wravery

Yes it does

gitmodimo avatar Mar 10 '25 18:03 gitmodimo