Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
flonou opened this issue Oct 9, 2024 · 6 comments · May be fixed by #326
Open

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

flonou opened this issue Oct 9, 2024 · 6 comments · May be fixed by #326

Comments

@flonou
Copy link

flonou commented Oct 9, 2024

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
Copy link

RyPeck commented Oct 31, 2024

This looks like it was fixed in 11bfe35 but a new release was not created.

@flonou
Copy link
Author

flonou commented Nov 4, 2024

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
Copy link

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

@flonou
Copy link
Author

flonou commented Nov 6, 2024

If I set convert_to_snake_case to true, both the fields and their _field_name 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
Copy link

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
Copy link
Author

flonou commented Nov 6, 2024

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

@flonou flonou linked a pull request Nov 14, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants