app-sre / qenerate

Code Generator for GraphQL Query and Fragment Data Classes
Apache License 2.0
9 stars 7 forks source link

pydantic DateTime type #67

Closed j30ng closed 1 year ago

j30ng commented 1 year ago

Hi all.

I'm getting errors for datetime fields in the generated python code, because DateTime is not a python primitive type nor a pydantic type.

https://github.com/app-sre/qenerate/blob/7a544a9d3fadcce5465b88522c77c35b70f3c988/qenerate/plugins/pydantic_v1/mapper.py#L42

fishi0x01 commented 1 year ago

Hi :wave:

You are right. My understanding is this should be datetime and we need to add from datetime import datetime to the generated code. Let me know if you think there is a more appropriate date type for this.

Thank you for reaching out about this!

fishi0x01 commented 1 year ago

Ive opened a PR with a fix and added a test case that uses datetime. Will merge and make a release later today.

fishi0x01 commented 1 year ago

Released version 0.4.6. Please let me know if it didn't solve the issue.

fishi0x01 commented 1 year ago

I gave this some more thought and given that DateTime is not a native GraphQL primitive, it might be a bad idea to actually convert it to datetime.datetime. DateTime is a custom scalar that might be defined differently depending on the schema. Im leaning now more towards casting all custom scalars to str instead - that makes it more compatible towards all schemas.

On the other hand it might be fine to make the pydantic_v1 plugin more opinionated. It is likely to work with most schemas out there even if it assumes DateTime is compatible with datetime.datetime. If the need for a more generic approach arises, we could easily add a new plugin.