Repository navigation
Fix UseConnectionPaging on a nested EF collection - #576
Merged
Merged
Conversation
A connection paged collection on a parent object (e.g. a navigation property) is part of the parent's projection, so EF has to translate the paging. It failed with "The LINQ expression 'edgeNode => ...' could not be translated": - edges were paged with our Skip/Take(int?) helpers. Use System.Linq's, as #573 did for offset paging. - cursors were assigned by ApplyCursors while enumerating, which EF can't translate inside a projection. For a nested collection the cursor is selected as null and set by ConnectionHelper.SetCursors once EF has materialized the edges, via a new IFieldExtension.ProcessExpressionPostSelection hook (default interface method / virtual on BaseFieldExtension). - last without before used arguments.TotalCount, one value per request. Page from the end of each parent's own collection with Reverse().Take(last) (Skip(Count() - last) puts the outer row in a correlated subquery SQLite and others reject) and flip the page back after materializing. - pageInfo { hasNextPage } alone called PageHasNext, which EF would run in memory over the whole collection. Build Skip().Any() so it is EXISTS. Also: - the edges field (and its ConnectionEdgeExtension) is shared by every field paging the same type, and used whether the first one configured was IQueryable for all. Decide it per use from the expression. - hasPreviousPage was false for last without before when exactly one item precedes the page. Root field paging is unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
- last with after on a nested collection took the last N of the whole collection, ignoring after (last: 5, after: 8 gave Movie6-10, not Movie9-10). Skip(after).Reverse() needs SQL APPLY, which SQLite does not support, so keep Reverse().Take(last) in SQL and have SetCursors drop the rows up to after, using the per-parent Count() EF projects. - cache the cursor FieldInfos per (type, field names) in SetCursors. - obsolete ConnectionEdgeExtension(Type, bool) - isQueryable is unused - for ConnectionEdgeExtension(Type), to be removed in 7.0. - document ProcessExpressionPostSelection in the custom extension docs. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Like the pre-selection, selection and scalar hooks, the extension loop lives in BaseGraphQLField (ProcessExtensionsPostSelection) rather than inline in GraphQLListSelectionField. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… Changes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… one EXISTS - last + before with the cursor past the end of the collection returned too few items. Root paging limits before to the counted total; ConnectionPageInfo does the same with its own count. A nested collection's parents share the cursor, so each parent's page is picked from its own count: the page ending at the cursor, or its last items (also selected) when it does not reach the cursor. Taking the first before-1 then reversing needs SQL APPLY, which SQLite lacks. - hasNextPage with before was false when the cursor is the last item. - A non-null field read off an object built in the same expression (pageInfo) is not null checked, so EF no longer translates its EXISTS twice. Checked against SQLite, Postgres 16 and SQL Server 2022. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lection Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
4ac897a skipped it for any non-null field read off an object built in the expression. A user field like new Wrapper { Inner = null }.Inner declared non-null then failed with a NullReferenceException instead of keeping its previous behaviour. Limit it to Connection<T>.PageInfo, which is never null. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.
Fixes #574.
Problem
UseConnectionPaging()on a field that isn't on the root query type failed when its collection comes from EF (e.g. a navigation property) withThe LINQ expression 'edgeNode => ...' could not be translated. The collection is part of the parent's projection, so EF has to translate the paging, and it couldn't.Changes
System.Linq'sSkip/Take, the same as Fix UseOffsetPaging on a nested EF collection #573 did for offset paging.ApplyCursorsassigns cursors while it enumerates the page, and EF can't translate it inside a projection. For a nested collection the cursor is now selected asnull, andConnectionHelper.SetCursorsfills it in once EF has materialised the edges (rowskip + i + 1). EF runs a client method in the final projection when it gets a materialised collection.IFieldExtension.ProcessExpressionPostSelectionlets an extension wrap a list field's projected result. It's a default interface method and a virtual onBaseFieldExtension, so existing extensions don't break.lastwithoutbefore: this readarguments.TotalCount, a single value for the whole request, so it can't work per parent. Each parent's page now comes from the end of its own collection viaReverse().Take(last), and the page is flipped back after it's materialised.Skip(Count() - last)doesn't work: EF puts the outer row into a correlated subquery inside theLEFT JOIN, which SQLite rejects (and other providers would needAPPLY).Reverse()needs an ordered collection, butlasthas no defined meaning on an unordered one anyway.pageInfo { hasNextPage }alone: this calledPageHasNext, which EF would have run in memory over the whole collection. It's nowSkip(...).Any(), which translates toEXISTS.ConnectionEdgeExtension). The extension used theisQueryableof whichever field was configured first, so a rootIQueryableconnection plus a nested one of the same type failed (No generic method 'Skip' on type 'QueryableExtensions'). It's now decided each time from the expression. The constructor parameter is kept for API compatibility and no longer used.hasPreviousPageoff by one: forlastwithoutbeforeit wasfalsewhen exactly one item comes before the page (e.g.last: 2of 3). This affected root fields too.Root field paging builds the same expressions as before. The only change on root fields is the
hasPreviousPagefix. Nothing is breaking: public methods keep their signatures and the additions are new members.Tests
PagingTests(EF/SQLite), all with nested collections:firstwithtotalCountandpageInfoafterlaston two actors with different countslast+beforePagingSqlTests:ROW_NUMBER) with unused columns pruned, for bothfirst/afterandlasthasNextPagealone isEXISTS, with noCOUNTPageInfoTests: thehasPreviousPageoff-by-one.Full suite passes on net8.0, net9.0 and net10.0.
Note:
dotnet tool restorefails onmain..config/dotnet-tools.jsonlists the commanddotnet-csharpier, but csharpier 1.0.0 names itcsharpier. I formatted withdnx csharpier@1.0.0and left the manifest alone.🤖 Generated with Claude Code