Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Mark sole __typename selections as @api in all contexts#79

Merged
ruudk merged 1 commit into
mainfrom
claude/fix-typename-dead-code-Pfg5o
May 18, 2026
Merged

Mark sole __typename selections as @api in all contexts#79
ruudk merged 1 commit into
mainfrom
claude/fix-typename-dead-code-Pfg5o

Conversation

@ruudk
Copy link
Copy Markdown
Owner

@ruudk ruudk commented May 18, 2026

Summary

Expanded the @api annotation for sole __typename selections to apply universally, not just to list fields and first-level mutation fields. This recognizes that selecting only __typename is an idiomatic pattern for probing an object's presence/non-null without reading actual data back.

Key Changes

  • Simplified shouldMarkTypenameAsApi() logic in SelectionSetPlanner.php: Removed context-specific checks (list detection and mutation-level detection). Now any sole __typename selection is marked @api, since the value is never actually read by the caller.

  • Updated test expectations to reflect the new behavior:

    • ListTypenameTest::testSoleTypenameOnSingleObjectIsApi(): Changed from expecting no @api to expecting @api on nested single objects with sole __typename
    • MutationTypenameTest::testSoleTypenameDeeperThanFirstLevelIsApi(): Changed from expecting no @api to expecting @api on nested mutation fields with sole __typename
  • Added comprehensive test coverage with new QueryObjectTypenameTest that validates:

    • Sole __typename on regular query object fields is tagged @api
    • Sole __typename on nested object fields inside inline fragments is tagged @api
    • Includes test schema and generated code examples demonstrating the pattern

Implementation Details

The change recognizes that sole __typename selections serve as a GraphQL idiom for presence/non-null probing without data consumption. This applies consistently across all contexts (queries, mutations, nested fields, inline fragments) rather than being special-cased to specific scenarios. Dead-code analysis tools can now safely ignore these properties since they're marked @api.

https://claude.ai/code/session_01LAjUp8CkPDVCjR2WP2Pith

…ations

A sole `__typename` selection on a regular (non-list) object field in a
query was not tagged @api, so dead-code analysis flagged the generated
`$__typename` property (and transitively `$data`) as never read/written.
Selecting only `__typename` is the idiomatic way to probe an object's
presence/non-null without reading any data back, which holds for every
sole-__typename selection — not just list or first-level mutation fields.

Drop the list/mutation-root narrowing in shouldMarkTypenameAsApi() so it
matches the inline-fragment path, and update the affected snapshots and
expectations. Adds a QueryObjectTypename regression fixture covering a
sole-__typename query object field and one nested inside an inline
fragment.

https://claude.ai/code/session_01LAjUp8CkPDVCjR2WP2Pith
@ruudk ruudk merged commit 840474c into main May 18, 2026
3 checks passed
@ruudk ruudk deleted the claude/fix-typename-dead-code-Pfg5o branch May 18, 2026 08:08
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