profusion / sgqlc

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

Code Completion In Pycharm #205

Open ashnap123 opened 2 years ago

ashnap123 commented 2 years ago

This is a great library and almost exactly what I've been looking for. Do you know if there is a way to make code completion work in PyCharm for the auto generated schema? A functioning autocomplete would make this an zero fiction way to interact with graphql APIs.

barbieri commented 2 years ago

could you elaborate? You mean completion when building the query (creating an operation) or interpreting the results?

ashnap123 commented 2 years ago

Ideally both, but I think most value would come from code completion when building operations.

ashnap123 commented 2 years ago

When generating the schema would it be enough to add type hints?

Replacing:

class MarketingBudget(sgqlc.types.Type):
    __schema__ = shopify_schema
    __field_names__ = ('budget_type', 'total')
    budget_type = sgqlc.types.Field(sgqlc.types.non_null(MarketingBudgetBudgetType), graphql_name='budgetType')

With:

class MarketingBudget(sgqlc.types.Type):
    __schema__ = shopify_schema
    __field_names__ = ('budget_type', 'total')
    budget_type: MarketingBudgetBudgetType = sgqlc.types.Field(sgqlc.types.non_null(MarketingBudgetBudgetType), graphql_name='budgetType')

Although I am not familiar with the details or differences between Type and Field classes. But this is similar to how dataclasses work.

barbieri commented 2 years ago

this is unfeasible as it is. I'd need to check if we can make sgqlc.types.Field into a generic that returns a wrapped type... but in reality we abuse metaclasses (just like dataclasses do), we'd need to write a specific mypy support.

This is one of the weakest spots of typechecking in python... if you do metaclasses transformations (like in my case it will get a class attribute and create ANOTHER instance attribute based on that... but it's not the same (one is a description, the other is a value based on that description). And Python ecosystem is full of metaclasses to save typing and autogenerate code (ex: all ORMs, such as Django...)

AFAIU dataclasses and similar are manually crafted in the type checking... but for not-so-famous projects we're hopeless :-(

ashnap123 commented 2 years ago

Thanks for taking time to think about this. If it's ever implemented I will definitely use this library in future.

barbieri commented 2 years ago

yeah, I feel your pain... at work I'm using mostly TypeScript and when I have to go typeless it's a major issue. OTOH in TypeScript such things are a bit easier to write, since there is no "metaclass" stuff are derived from object mapping and one can easily do something like "for each object key, if the value type is X, do something, if it's Y, do another thing"...

barbieri commented 2 years ago

btw, stay tuned in #129, it's almost the same issue