ariadne-codegen
ariadne-codegen copied to clipboard
field_name for generated class is sometimes set to a snake case version
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 ?
This looks like it was fixed in https://github.com/mirumee/ariadne-codegen/commit/11bfe35bd62b2489927e0e93c6891bccc29c7f37 but a new release was not created.
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
Came across the same issue and pulling from git worked. Don't forget to enable the setting in the pyproject file.
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
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.
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 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.
You can try my PR above to fix this
I applied a plugin to patch those changes you made in #326 thanks for figuring that out. It is working as expected!
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
Fixed in https://github.com/mirumee/ariadne-codegen/pull/362!