evo-company / hiku

Library to write servers for GraphQL-like query languages
http://hiku.readthedocs.io
BSD 3-Clause "New" or "Revised" License
60 stars 13 forks source link

Proper handling of custom scalar serialization/deserialization errors #154

Open slavikovbasa opened 3 months ago

slavikovbasa commented 3 months ago

Having a scheme:

class Long(Scalar):
    @classmethod
    def parse(cls, value: typing.Any) -> int:
        return int(value)

    @classmethod
    def serialize(cls, value: typing.Any) -> int:
        return int(value)

GRAPH = Graph([
    Root([
        Field("longStub", Long, lambda fs: [fs[0].options["id"]], options=[Option("id", Long)])
    ])
])

Such a query is possible and results in a ValueError being thrown:

{
  longStub(id:"asdf")
}

It is possible to catch errors in parse/serialize methods of Scalar, but there is no way to return any validation errors to a client. Would be nice to have either:

  1. Custom error that can be thrown from serialization methods and process it in hiku accordingly;
  2. Or a separate validation method for Scalar

Possibly there is the same problem with enums, haven't checked though.

kindermax commented 3 months ago

So basically we need to error propagation mechanism in denormalization ?

slavikovbasa commented 3 months ago

not sure if denormalization is the best place to do it, maybe somewhere after parsing?

kindermax commented 3 months ago

Yep, in case when we are parsing arguments we should catch parse errors and report them to user.

My msg about denormalization makes sense when we are serializing scalars. Also being able to report errors from denormalization phase needed for another case when value completion failed (because of null in not-nullable field) and we need to tell user that there is an error.