mirumee / ariadne-codegen

Generate fully typed Python client for any GraphQL API from schema, queries and mutations
BSD 3-Clause "New" or "Revised" License
275 stars 35 forks source link

field_name for generated class is sometimes set to a snake case version #322

Open flonou opened 1 month ago

flonou commented 1 month ago

Hi,

Here is an extract of a schema that I generated code for:

...
                          {
                            "name": "tournaments",
                            "description": null,
                            "args": [
                                {
                                    "name": "query",
                                    "description": null,
                                    "type": {
                                        "kind": "NON_NULL",
                                        "name": null,
                                        "ofType": {
                                            "kind": "INPUT_OBJECT",
                                            "name": "TournamentsQuery",
                                            "ofType": null
                                        }
                                    },
                                    "defaultValue": null
                                },
                                {
                                    "name": "page",
                                    "description": null,
                                    "type": {
                                        "kind": "NON_NULL",
                                        "name": null,
                                        "ofType": {
                                            "kind": "INPUT_OBJECT",
                                            "name": "PageInfo",
                                            "ofType": null
                                        }
                                    },
                                    "defaultValue": null
                                }
                            ],
                            "type": {
                                "kind": "NON_NULL",
                                "name": null,
                                "ofType": {
                                    "kind": "OBJECT",
                                    "name": "Tournaments",
                                    "ofType": null
                                }
                            },
                            "isDeprecated": false,
                            "deprecationReason": null
                        },
...

For some reason, the generated code is:

...
class TournamentsFields(GraphQLField):
    @classmethod
    def edges(cls) -> "TournamentEdgeFields":
        return TournamentEdgeFields("edges")

    @classmethod
    def nodes(cls) -> "TournamentFields":
        return TournamentFields("nodes")

    @classmethod
    def page_info(cls) -> "ResponsePageInfoFields":
        return ResponsePageInfoFields("page_info")

    total_count: "TournamentsGraphQLField" = TournamentsGraphQLField("totalCount")

    def fields(
        self,
        *subfields: Union[
            TournamentsGraphQLField,
            "ResponsePageInfoFields",
            "TournamentEdgeFields",
            "TournamentFields",
        ]
    ) -> "TournamentsFields":
        """Subfields should come from the TournamentsFields class"""
        self._subfields.extend(subfields)
        return self

    def alias(self, alias: str) -> "TournamentsFields":
        self._alias = alias
        return self
...

ResponsePageInfoField("page_info") will result in an error as the api is expecting pageInfo instead of page_info. Any reason why some field names are generated this way ? How to prevent it ?

RyPeck commented 4 weeks ago

This looks like it was fixed in https://github.com/mirumee/ariadne-codegen/commit/11bfe35bd62b2489927e0e93c6891bccc29c7f37 but a new release was not created.

flonou commented 3 weeks ago

I'm not sure I followed the right step but I tried to remove ariadne-codgen with pip uninstall and install the last version with pip install git+https://github.com/mirumee/ariadne-codegen.git and the issue is still present

McleanWorkshield commented 3 weeks ago

Came across the same issue and pulling from git worked. Don't forget to enable the setting in the pyproject file.

flonou commented 3 weeks ago

If I set convert_to_snake_case to true, both the fields and their _fieldname have , if I set it to true, none of them has it. I guess I'm missing something ? the _field_name should never be changed from what the schema defines

McleanWorkshield commented 3 weeks ago

Maybe your scenario is different than mine - I actually do not want snake case, and convert_to_snake_case is true by default. To get mine to work, I actually set it to false and use camel case.

flonou commented 3 weeks ago

yeah I wanted snake_case for python variables but not for the field_names because that just doesn't follow the schema in that case