Mark sole __typename selections as @api in all contexts#79
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Expanded the
@apiannotation for sole__typenameselections to apply universally, not just to list fields and first-level mutation fields. This recognizes that selecting only__typenameis an idiomatic pattern for probing an object's presence/non-null without reading actual data back.Key Changes
Simplified
shouldMarkTypenameAsApi()logic inSelectionSetPlanner.php: Removed context-specific checks (list detection and mutation-level detection). Now any sole__typenameselection 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@apito expecting@apion nested single objects with sole__typenameMutationTypenameTest::testSoleTypenameDeeperThanFirstLevelIsApi(): Changed from expecting no@apito expecting@apion nested mutation fields with sole__typenameAdded comprehensive test coverage with new
QueryObjectTypenameTestthat validates:__typenameon regular query object fields is tagged@api__typenameon nested object fields inside inline fragments is tagged@apiImplementation Details
The change recognizes that sole
__typenameselections 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