ariadne-codegen icon indicating copy to clipboard operation
ariadne-codegen copied to clipboard

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

Open flonou opened this issue 1 year ago • 6 comments
trafficstars

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 ?

flonou avatar Oct 09 '24 21:10 flonou

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

RyPeck avatar Oct 31 '24 14:10 RyPeck

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

flonou avatar Nov 04 '24 22:11 flonou

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

McleanWorkshield avatar Nov 06 '24 18:11 McleanWorkshield

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

flonou avatar Nov 06 '24 19:11 flonou

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.

McleanWorkshield avatar Nov 06 '24 22:11 McleanWorkshield

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 avatar Nov 06 '24 22:11 flonou

@flonou I'm running into the same issue.



class UpdateProgramNotesResultFields(GraphQLField):
    @classmethod
    def program_notes(cls) -> "ProgramNoteFields":
        return ProgramNoteFields("program_notes")

should be



class UpdateProgramNotesResultFields(GraphQLField):
    @classmethod
    def program_notes(cls) -> "ProgramNoteFields":
        return ProgramNoteFields("programNotes")

This happens in numerous locations in the custom_fields.py.

When I pull from git, the issue persists. This tool is great, and saves us a lot of development time with boilerplate code, I hope we can get this fixed and merged.

davner avatar May 05 '25 07:05 davner

You can try my PR above to fix this

flonou avatar May 09 '25 21:05 flonou

I applied a plugin to patch those changes you made in #326 thanks for figuring that out. It is working as expected!

davner avatar May 10 '25 00:05 davner

Since #326 doesn't seem to be going anywhere I've just gone ahead and created a plugin that patches out this annoying behaviour. for anyone that would like to use it the plugin is called: ariadne-codegen-field-fix

ewen-lorimer avatar Sep 04 '25 15:09 ewen-lorimer

Fixed in https://github.com/mirumee/ariadne-codegen/pull/362!

DamianCzajkowski avatar Oct 08 '25 14:10 DamianCzajkowski