[EE-3024] support inline fragments #56
Merged
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.
Add inline fragments support
This PR fixes issue - #52
Summary
This PR implements comprehensive support for GraphQL inline fragments (
... on TypeName) in the graphql-query-to-json library. Inline fragments enable conditional field selection based on GraphQL union and interface types, making this library a complete complement to the json-to-graphql-query library.Problem
The library previously did not support GraphQL inline fragments, which are essential for working with union types and interfaces. This limitation prevented users from processing queries that conditionally select fields based on concrete types.
Solution
Implementation Details
Core Changes:
Selectioninterface to includetypeConditionfor inline fragmentsgetSelections()function to processInlineFragmentAST nodesJSON Output Format:
__on: { __typeName: "TypeName", ...fields }__on: [{ __typeName: "Type1", ...}, { __typeName: "Type2", ...}]This format mirrors the
json-to-graphql-querylibrary's approach, ensuring seamless bidirectional conversion.Features Supported
Examples
Single Inline Fragment
Output:
{ "query": { "posts": { "title": true, "__on": { "__typeName": "TextPost", "content": true, "wordCount": true } } } }Multiple Inline Fragments
Output:
{ "query": { "media": { "__on": [ { "__typeName": "TextPost", "content": true }, { "__typeName": "ImagePost", "imageUrl": true, "altText": true }, { "__typeName": "VideoPost", "videoUrl": true, "duration": true } ] } } }Testing
Comprehensive Test Coverage
8 new test cases covering all inline fragment scenarios:
3 README example tests ensuring documentation accuracy
Updated existing tests to reflect inline fragments now work (converted error test to success test)
Test Results: ✅ All 75 tests passing
Documentation Updates
README.md Changes
Compatibility
__onformat for seamless interoperabilityRelated Issues
Resolves the inline fragments limitation discussed in PR #23.
Breaking Changes
None. This is a purely additive feature that maintains full backward compatibility.
🤖 Generated with Claude Code
Co-Authored-By: Claude noreply@anthropic.com