profusion / sgqlc

Simple GraphQL Client
https://sgqlc.readthedocs.io/
ISC License
506 stars 85 forks source link

Way to add new properties to object types and print with __to_json_value__() #228

Closed madiganz closed 1 year ago

madiganz commented 1 year ago

I have a couple of cases where I am adding extra data onto the schema types. I would like this data to still be JSON serializable via __to_json_value__(). Is it possible to have new properties added to the class so that they are also serializable and added to the ___json_data___ property?

Basically what I'm trying to do is have non-graphql attributes be accessible through __to_json_value__().

barbieri commented 1 year ago

@madiganz could you clarify the usage/examples?

Currently there is no way, and I think it would break the behavior since we just use __to_json_value__() to create the payload to be sent in the variables block sent to the server.

You could dynamically patch some of the basic classes to wrap the provided method with an extension, but I don't think it fits in the library. But if you share some reasonable use case, this could be done without much trouble.

madiganz commented 1 year ago

An example could be that I query data from my GraphQL API, then I get some data from an external source and want to add it to the type without creating an entirely new wrapper type.

I was able to solve this by creating a to_json wrapper that calls __to_json_value__()

barbieri commented 1 year ago

ok, I'll close this issue. If possible share some example on how you did, so if someone reaches this issue they can see how you solved it.