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

Conversion from snake_case to camelCase in subfields is not happening

Open wiredmatt opened this issue 10 months ago • 2 comments

Hi, it seems like subquery fields are not being converted to camelcase when sending the request to graphql.

Repro

from codex import CodexClient, TokenInput
from codex.custom_queries import Query
from codex.custom_fields import SocialLinksFields, EnhancedTokenFields, TokenInfoFields, ExchangeFields
from uw_ai_backend.core.config import get_settings
import asyncio
import logging
import httpx

settings = get_settings()

async def log_request(request: httpx.Request):
    print(f"Request event hook: {request.method} {request.url} {request.content} - Waiting for response")

http_client = httpx.AsyncClient(event_hooks={
    'request': [log_request]
})

codex_client = CodexClient(
    url=settings.CODEX_URL, 
    headers={ "Authorization": settings.CODEX_API_KEY },
    http_client=http_client
)

logging.basicConfig(
    format="%(levelname)s [%(asctime)s] %(name)s - %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S",
    level=logging.DEBUG
)

def get_token_details_query(dto: TokenInput):
    return Query.token(dto).fields(
        # METADATA
        EnhancedTokenFields.info().fields(
            TokenInfoFields.address,
            TokenInfoFields.name,
            TokenInfoFields.symbol,
            TokenInfoFields.network_id,
            TokenInfoFields.description,
            TokenInfoFields.is_scam,
            TokenInfoFields.total_supply,
            TokenInfoFields.circulating_supply,
            TokenInfoFields.image_thumb_url,
            TokenInfoFields.image_small_url,
        ),
        # METADATA

        # EXCHANGES
        EnhancedTokenFields.exchanges().fields(
            ExchangeFields.id,
            ExchangeFields.address,
            ExchangeFields.network_id,
            ExchangeFields.name,
            ExchangeFields.trade_url
        ),
        # EXCHANGES

        # SOCIAL LINKS
        EnhancedTokenFields.social_links().fields(
            SocialLinksFields.website
        ),
        # SOCIAL LINKS
    )

async def test_get_token_details():
    dto = TokenInput(
        address="0x532f27101965dd16442e59d40670faf5ebb142e4", # brett
        networkId=8453 # base
    )

    query = get_token_details_query(dto)

    response = await codex_client.query(
        query,
        operation_name="get_token_details"
    )

    print(response)

def main():
    asyncio.run(test_get_token_details())

social_links should be converted to socialLinks, however see the logs:

Request event hook: POST https://graph.defined.fi/graphql b'{"query": "query get_token_details($input_0: TokenInput!) {\\n  token(input: $input_0) {\\n    info {\\n      address\\n      name\\n      symbol\\n      networkId\\n      description\\n      isScam\\n      totalSupply\\n      circulatingSupply\\n      imageThumbUrl\\n      imageSmallUrl\\n    }\\n    exchanges {\\n      id\\n      address\\n      networkId\\n      name\\n      tradeUrl\\n    }\\n    social_links {\\n      website\\n    }\\n  }\\n}", "operationName": "get_token_details", "variables": {"input_0": {"address": "0x532f27101965dd16442e59d40670faf5ebb142e4", "networkId": 8453}}}' - Waiting for response

I tried using the convert_to_snake_case config parameter but that seems to be ignored, as the generated code is still outputted in camelcase.

[tool.ariadne-codegen]
schema_path = "graphql_src/codex/schema.graphql"
convert_to_snake_case = false # this is not making any difference
client_name = "CodexClient"
client_file_name = "client"
target_package_name = "codex"
enable_custom_operations = true

This is the graphql query with which this is happening:

query {
  token(input: {address:"0x532f27101965dd16442e59d40670faf5ebb142e4", networkId:8453}) {
    info {
      address
      name
      symbol
      description
      isScam
      totalSupply
      circulatingSupply
      imageThumbUrl
      imageSmallUrl
    }
    socialLinks {
      telegram
      twitter
      website
    }
  }
}

wiredmatt avatar Jan 24 '25 17:01 wiredmatt

https://github.com/mirumee/ariadne-codegen/blob/main/ariadne_codegen/client_generators/package.py#L185 Perhaps related to this hardcoded convert_to_snake_case=True?

wiredmatt avatar Jan 24 '25 19:01 wiredmatt

Yup that seems to be it, if you set it to convert_to_snake_case=self.convert_to_snake_case the subquery gets named correctly in the generated code.

class EnhancedTokenFields(GraphQLField):
    address: "EnhancedTokenGraphQLField" = EnhancedTokenGraphQLField("address")
    cmcId: "EnhancedTokenGraphQLField" = EnhancedTokenGraphQLField("cmcId")
    decimals: "EnhancedTokenGraphQLField" = EnhancedTokenGraphQLField("decimals")
    id: "EnhancedTokenGraphQLField" = EnhancedTokenGraphQLField("id")
    isScam: "EnhancedTokenGraphQLField" = EnhancedTokenGraphQLField("isScam")
    name: "EnhancedTokenGraphQLField" = EnhancedTokenGraphQLField("name")
    networkId: "EnhancedTokenGraphQLField" = EnhancedTokenGraphQLField("networkId")
    symbol: "EnhancedTokenGraphQLField" = EnhancedTokenGraphQLField("symbol")
    totalSupply: "EnhancedTokenGraphQLField" = EnhancedTokenGraphQLField("totalSupply")

    @classmethod
    def socialLinks(cls) -> "SocialLinksFields":
        return SocialLinksFields("socialLinks") # good here

wiredmatt avatar Jan 24 '25 20:01 wiredmatt