ORM: support json
We are not unpacking JSON by default -- we should consider doing that.
Pydantic supports JSON unpacking (partial & full), details here: https://docs.pydantic.dev/latest/concepts/json/#json-parsing
If we have this in the schema
type User {
name: str;
data: json;
}
we can maybe enable something like this then:
# User subclasses the generated model:
class MyUser(models.default.User):
data: gel.JsonUnpacker[
models.default.User.__typeof__.data,
MyDataModel
]
# User defines their model to parse JSON:
class MyDataModel(pydantic.BaseModel):
...
Or, alternatively:
# User subclasses the generated model to
# specify a *new* computed field to unpack
# JSON into
class MyUser(models.default.User):
parsed_data: gel.JsonUnpackFrom[
models.default.User.__typeof__.data,
MyDataModel
]
# User defines their model to parse JSON:
class MyDataModel(pydantic.BaseModel):
...
I think we have two options:
-
Always unpack JSON -- if there's a type for it setup -- use that. No type -- return whatever
json.loads()returns -
Only unpack JSON when the user asked for it, either with ORM model customization, or with the upcoming
.with_codecs()API.
I'm in favor of (2).
Given time constraints, on day 1, we can just keep the status quo and return strings. If we go with option (2).
- Use Gel schema annotations to specify full qualified name for a Pydantic JSON model -- this is better than making users overload reflected classes - that composes poorly
- @msullivan Need to add
lang::pylang::jstoGel 6.8-- this is to enable this as soon as we have bandwidth - Modify
gel-pythonto unpack JSON into strings NOW (just usejson.loads)
I've doubts about unpacking JSON by default. The whole point of using pydantic is type safety, maybe nobody wants to unpack automatically to non-pydantic raw python dicts?
Consensus reached: we will not unpack JSON automatically if it doesn't have a schema (std::lang::py::annotation). We can add a client config-like thing client.with_auto_json().query(...) and then unpack. But not by default.