sgqlc icon indicating copy to clipboard operation
sgqlc copied to clipboard

Code Completion In Pycharm

Open ashnap123 opened this issue 2 years ago • 7 comments

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.

ashnap123 avatar May 24 '22 17:05 ashnap123

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

barbieri avatar May 24 '22 18:05 barbieri

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

ashnap123 avatar May 24 '22 19:05 ashnap123

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.

ashnap123 avatar May 24 '22 19:05 ashnap123

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 :-(

barbieri avatar May 24 '22 20:05 barbieri

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

ashnap123 avatar May 24 '22 20:05 ashnap123

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 avatar May 24 '22 20:05 barbieri

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

barbieri avatar May 24 '22 20:05 barbieri