Skip to content

Conversation

@smarongiu
Copy link

Fix __typename field handling to use GraphQLString instead of creating new scalar

Description

Fixes a bug where code generation fails with TypeError: Redefinition of reserved type 'String' when processing queries that include __typename fields. The issue occurs because __typename is a GraphQL meta-field that isn't explicitly defined in schema type maps, so the code falls back to creating a field definition. The original implementation incorrectly attempted to create a new GraphQLScalarType(name="String"), which fails because "String" is a reserved built-in type.

Minimal Example

Schema:

type Query {
  user: User
}

type User {
  id: ID!
  name: String!
}

Query:

query GetUser {
  user {
    __typename
    id
    name
  }
}

Before (fails):

return GraphQLField(
    type_=GraphQLNonNull(type_=GraphQLScalarType(name="String"))  # ❌ TypeError
)

After (works):

return GraphQLField(
    type_=GraphQLNonNull(type_=GraphQLString)  # ✅ Uses built-in type
)

Changes

  • Added GraphQLString import to ariadne_codegen/client_generators/result_types.py
  • Updated _get_field_from_schema() to use GraphQLString instead of creating a new scalar for __typename handling
  • Added test case test_get_field_from_schema_handles_typename_correctly() to validate the fix

Testing

  • All existing tests pass (6/6 in test_include_typename.py)
  • New test validates __typename field handling and would fail without the fix

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Checklist

  • Code follows the project's style guidelines
  • Tests pass locally
  • Added tests for the fix
  • No breaking changes introduced

@DamianCzajkowski
Copy link
Collaborator

Hi @smarongiu, nice pull request! Can you run the linters so the GitHub Actions aren't failing?

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 this pull request may close these issues.

2 participants