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

Custom scalars and built-in graphql types #316

Open
tsimoshka opened this issue Sep 19, 2024 · 0 comments
Open

Custom scalars and built-in graphql types #316

tsimoshka opened this issue Sep 19, 2024 · 0 comments

Comments

@tsimoshka
Copy link

Hi,

I'm trying to generate a client for a server that has a special format of the ID scalar, which I want to parse into a custom type in the result model. I've tried to override the str type with Custom scalars, but it works only for the client function argument, not for the result type. I've added a simplified sample at the end of the issue for illustration.

Upon further investigation, I found the code responsible for generating annotations for result models. It appears that the reason for this issue is that in the parse_scalar_type function, the type is checked against SIMPLE_TYPE_MAP first, and custom_scalars aren't used with built-in scalar types. Swapping the first two if statements in this function fixes the issue for me.

So my questions are:

  • Is this behavior by design, or would you consider a PR with the fix for this?
  • If this is by design, could someone point me to ways of achieving this?

Example

schema.graphql

schema {
  query: Query
}

type Query {
  getUser(id: ID!): User
}

type User {
  id: ID!
  firstName: String
  lastName: String
}

queries.graphql

query getUserById($id: ID!) {
  getUser(id: $id) {
    id
    firstName
    lastName
  }
}

pyproject.toml

[tool.ariadne-codegen]
schema_path = "schema.graphql"
queries_path = "queries.graphql"

[tool.ariadne-codegen.scalars.ID]
type = "pydantic.AnyHttpUrl"

client.py

# Generated by ariadne-codegen
# Source: queries.graphql

from typing import Any, Dict

from pydantic import AnyHttpUrl

from .async_base_client import AsyncBaseClient
from .get_user_by_id import GetUserById


def gql(q: str) -> str:
    return q


class Client(AsyncBaseClient):
    async def get_user_by_id(self, id: AnyHttpUrl, **kwargs: Any) -> GetUserById:
        query = gql(
            """
            query getUserById($id: ID!) {
              getUser(id: $id) {
                id
                firstName
                lastName
              }
            }
            """
        )
        variables: Dict[str, object] = {"id": id}
        response = await self.execute(
            query=query, operation_name="getUserById", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return GetUserById.model_validate(data)

get_user_by_id.py

# Generated by ariadne-codegen
# Source: queries.graphql

from typing import Optional

from pydantic import Field

from .base_model import BaseModel


class GetUserById(BaseModel):
    get_user: Optional["GetUserByIdGetUser"] = Field(alias="getUser")


class GetUserByIdGetUser(BaseModel):
    id: str
    first_name: Optional[str] = Field(alias="firstName")
    last_name: Optional[str] = Field(alias="lastName")


GetUserById.model_rebuild()
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

No branches or pull requests

1 participant