-
Notifications
You must be signed in to change notification settings - Fork 158
feat(plan): auto-split root field datasources in NewPlanner #1422
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
Merged
jensneuse
merged 14 commits into
feat/add-caching-support
from
jensneuse/split-ds-root-cache
Mar 5, 2026
Merged
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6fa1cdb
chore: move cost tests into separate file (#1418)
ysmolski 049a895
Merge remote-tracking branch 'origin/master' into jensneuse/split-ds-…
jensneuse 087d0a7
feat(plan): auto-split root field datasources in NewPlanner
jensneuse f9fb69d
Merge branch 'feat/add-caching-support' into jensneuse/split-ds-root-…
jensneuse 30848cc
fix: resolve lint issues (gci formatting, staticcheck)
jensneuse 21d8f74
fix: remove root field caching from field info test
jensneuse 36f278a
refactor(plan): replace datasource pre-split with planner-level root …
jensneuse fd15005
docs: add inline comments to root field isolation logic
jensneuse 00662a0
test(plan): verify cache configs on isolated root field fetches
jensneuse b8f684c
fix: correct indentation in graphql_datasource_test.go
jensneuse b7b6e5d
Merge branch 'feat/add-caching-support' into jensneuse/split-ds-root-…
jensneuse 577cee2
fix: use exact assertions and add doc comment for isolatedRootField
jensneuse 987d94e
test: use full plan assertions and complete cache log comparisons
jensneuse 0483679
fix: add explicit FetchID: 0 to first fetch in planner isolation tests
jensneuse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| package plan | ||
|
|
||
| import "fmt" | ||
|
|
||
| // dataSourceSplitter is implemented by dataSourceConfiguration[T] to enable | ||
| // cloning a datasource with new ID and metadata during root field splitting. | ||
| type dataSourceSplitter interface { | ||
| cloneForSplit(newID string, metadata *DataSourceMetadata) (DataSource, error) | ||
| } | ||
|
|
||
| // SplitDataSourcesByRootFieldCaching splits datasources that have root field caching | ||
| // configured into separate per-field datasources. This ensures each cacheable root field | ||
| // gets its own fetch, enabling independent L2 caching per field. | ||
| // | ||
| // Why split? The planner merges root fields from the same datasource into a single fetch. | ||
| // This means a query like { me { id } cat { name } } produces one request to the subgraph. | ||
| // However, configureFetchCaching requires all root fields in a fetch to have identical | ||
| // cache configs. By splitting each cached root field into its own datasource, the planner | ||
| // creates separate fetches, and each fetch can have its own TTL and cache key. | ||
| // | ||
| // The split produces up to N+1 datasources from the original: | ||
| // - One datasource per cached root field (each with its own RootFieldCaching entry) | ||
| // - One remainder datasource for all uncached root fields (no RootFieldCaching) | ||
| // | ||
| // All split datasources share the same non-Query root nodes (entity types, Mutation, | ||
| // Subscription), child nodes, entity caching config, and federation metadata (keys, | ||
| // requires, provides). This preserves entity resolution capability across all splits. | ||
| func SplitDataSourcesByRootFieldCaching(dataSources []DataSource) ([]DataSource, error) { | ||
| var result []DataSource | ||
| for _, ds := range dataSources { | ||
| split, err := splitSingleDataSourceByRootFieldCaching(ds) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to split data source %s by root field caching: %w", ds.Id(), err) | ||
| } | ||
| result = append(result, split...) | ||
| } | ||
| return result, nil | ||
| } | ||
|
|
||
| func splitSingleDataSourceByRootFieldCaching(ds DataSource) ([]DataSource, error) { | ||
| fedConfig := ds.FederationConfiguration() | ||
|
|
||
| // No root field caching configured — nothing to split | ||
| if len(fedConfig.RootFieldCaching) == 0 { | ||
| return []DataSource{ds}, nil | ||
| } | ||
|
|
||
| // Check if the datasource supports cloning (all dataSourceConfiguration[T] do) | ||
| splitter, ok := ds.(dataSourceSplitter) | ||
| if !ok { | ||
| return []DataSource{ds}, nil | ||
| } | ||
|
|
||
| nodesAccess, ok := ds.(NodesAccess) | ||
| if !ok { | ||
| return []DataSource{ds}, nil | ||
| } | ||
|
|
||
| // Find the Query root node — we only split Query fields, not Mutation/Subscription | ||
| rootNodes := nodesAccess.ListRootNodes() | ||
| queryNodeIdx := -1 | ||
| for i, node := range rootNodes { | ||
| if node.TypeName == "Query" { | ||
| queryNodeIdx = i | ||
| break | ||
| } | ||
| } | ||
| if queryNodeIdx == -1 { | ||
| // No Query root node — nothing to split (entity-only datasource) | ||
| return []DataSource{ds}, nil | ||
| } | ||
|
|
||
| // Partition Query fields into cached and uncached buckets | ||
| queryNode := rootNodes[queryNodeIdx] | ||
| var cachedFields, uncachedFields []string | ||
| for _, fieldName := range queryNode.FieldNames { | ||
| if fedConfig.RootFieldCaching.FindByTypeAndField("Query", fieldName) != nil { | ||
| cachedFields = append(cachedFields, fieldName) | ||
| } else { | ||
| uncachedFields = append(uncachedFields, fieldName) | ||
| } | ||
| } | ||
|
|
||
| // Skip splitting when there's only a single cached field and no uncached fields. | ||
| // A single-field datasource already gets its own fetch — splitting adds no benefit. | ||
| if len(cachedFields) <= 1 && len(uncachedFields) == 0 { | ||
| return []DataSource{ds}, nil | ||
| } | ||
|
|
||
| childNodes := nodesAccess.ListChildNodes() | ||
|
|
||
| // Collect non-Query root nodes (e.g. User entity, Mutation) — these are shared | ||
| // across all split datasources so entity resolution continues to work | ||
| var nonQueryRootNodes TypeFields | ||
| for _, node := range rootNodes { | ||
| if node.TypeName != "Query" { | ||
| nonQueryRootNodes = append(nonQueryRootNodes, node) | ||
| } | ||
| } | ||
|
|
||
| var result []DataSource | ||
|
|
||
| // Create one datasource per cached Query root field. | ||
| // Each gets a unique ID (original_rf_fieldName) and only its own cache config. | ||
| for _, fieldName := range cachedFields { | ||
| // Build root nodes: single Query field + all non-Query root nodes | ||
| splitRootNodes := make(TypeFields, 0, len(nonQueryRootNodes)+1) | ||
| splitRootNodes = append(splitRootNodes, TypeField{ | ||
| TypeName: "Query", | ||
| FieldNames: []string{fieldName}, | ||
| ExternalFieldNames: queryNode.ExternalFieldNames, | ||
| FetchReasonFields: queryNode.FetchReasonFields, | ||
| }) | ||
| splitRootNodes = append(splitRootNodes, nonQueryRootNodes...) | ||
|
|
||
| // Attach only this field's cache config to the new datasource | ||
| cacheConfig := fedConfig.RootFieldCaching.FindByTypeAndField("Query", fieldName) | ||
| metadata := cloneMetadataForSplit(ds, splitRootNodes, childNodes) | ||
| metadata.FederationMetaData.RootFieldCaching = RootFieldCacheConfigurations{*cacheConfig} | ||
|
|
||
| splitID := fmt.Sprintf("%s_rf_%s", ds.Id(), fieldName) | ||
| splitDS, err := splitter.cloneForSplit(splitID, metadata) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| result = append(result, splitDS) | ||
| } | ||
|
|
||
| // Create a remainder datasource for uncached fields (if any). | ||
| // This keeps the original datasource ID so existing planner behavior is preserved. | ||
| if len(uncachedFields) > 0 { | ||
| remainderRootNodes := make(TypeFields, 0, len(nonQueryRootNodes)+1) | ||
| remainderRootNodes = append(remainderRootNodes, TypeField{ | ||
| TypeName: "Query", | ||
| FieldNames: uncachedFields, | ||
| ExternalFieldNames: queryNode.ExternalFieldNames, | ||
| FetchReasonFields: queryNode.FetchReasonFields, | ||
| }) | ||
| remainderRootNodes = append(remainderRootNodes, nonQueryRootNodes...) | ||
|
|
||
| metadata := cloneMetadataForSplit(ds, remainderRootNodes, childNodes) | ||
| // Explicitly clear root field caching — uncached fields should not inherit cache config | ||
| metadata.FederationMetaData.RootFieldCaching = nil | ||
|
|
||
| remainderDS, err := splitter.cloneForSplit(ds.Id(), metadata) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| result = append(result, remainderDS) | ||
| } | ||
|
|
||
| return result, nil | ||
| } | ||
|
|
||
| // cloneMetadataForSplit creates new DataSourceMetadata with the given root nodes | ||
| // while preserving all federation metadata, child nodes, and directives from the original. | ||
| func cloneMetadataForSplit(original DataSource, rootNodes, childNodes TypeFields) *DataSourceMetadata { | ||
| origFed := original.FederationConfiguration() | ||
| origDirectives := original.DirectiveConfigurations() | ||
|
|
||
| return &DataSourceMetadata{ | ||
| RootNodes: rootNodes, | ||
| ChildNodes: childNodes, | ||
| Directives: origDirectives, | ||
| FederationMetaData: FederationMetaData{ | ||
| Keys: origFed.Keys, | ||
| Requires: origFed.Requires, | ||
| Provides: origFed.Provides, | ||
| EntityInterfaces: origFed.EntityInterfaces, | ||
| InterfaceObjects: origFed.InterfaceObjects, | ||
| EntityCaching: origFed.EntityCaching, | ||
| RootFieldCaching: origFed.RootFieldCaching, | ||
| SubscriptionEntityPopulation: origFed.SubscriptionEntityPopulation, | ||
| }, | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: wundergraph/graphql-go-tools
Length of output: 15548
Filter
FetchReasonFieldsto only include fields present in the split.When splitting a TypeField,
FetchReasonFieldsshould be filtered to only include field names that exist in the split'sFieldNamesarray. Currently, the entire array from the originalqueryNodeis copied, which causes field coordinates to be registered for fields not actually present in the split.For example, if the original has
FetchReasonFields: ["userProfile"]and the split contains onlyFieldNames: ["settings"], the split'sFetchReasonFieldsshould be empty, not include "userProfile".This happens at lines 108-113 and again at lines 133-138 where remainder datasources are created.
🤖 Prompt for AI Agents