Skip to content

fix(PageConnection): fallback to 0, not -1 on empty pages#9412

Open
Hanskrogh wants to merge 5 commits intoChilliCream:mainfrom
Hanskrogh:fix/page-connection-totalcount
Open

fix(PageConnection): fallback to 0, not -1 on empty pages#9412
Hanskrogh wants to merge 5 commits intoChilliCream:mainfrom
Hanskrogh:fix/page-connection-totalcount

Conversation

@Hanskrogh
Copy link
Contributor

This pull request makes a minor change to the default value for the TotalCount property in the PageConnection class. The default value is now 0 instead of -1 when the total count is not available.

Copilot AI review requested due to automatic review settings March 21, 2026 18:34
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR changes the default TotalCount value exposed by PageConnection<TNode> when the underlying GreenDonut.Data.Page<TNode>.TotalCount is not available, switching the fallback from -1 to 0.

Changes:

  • Update PageConnection<TNode>.TotalCount to return 0 when _page.TotalCount is null (previously -1).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

/// </summary>
[GraphQLDescription("Identifies the total count of items in the connection.")]
public int TotalCount => _page.TotalCount ?? -1;
public int TotalCount => _page.TotalCount ?? 0;
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the sentinel value for "total count not available" from -1 to 0 for PageConnection, but other paging implementations in this repo still use -1 when the total count is unknown (e.g. CursorPagingHandler and OffsetPaginationAlgorithm). Consider aligning the behavior across paging result types or documenting the differing semantics to avoid surprising API consumers.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michaelstaib Is this a valid argument? should I change it, so only Page.Empty adds a 0 count?

    /// <summary>
    /// An empty page.
    /// </summary>
    public static new ValueCursorPage<T> Empty { get; } = new([], false, false, _ => string.Empty, 0);

Hanskrogh and others added 2 commits March 21, 2026 20:55
Comment on lines +76 to +79
/// Identifies the total count of items in the connection.
/// </summary>
[GraphQLDescription("Identifies the total count of items in the connection.")]
public int TotalCount => _page.TotalCount ?? -1;
public int TotalCount => _page.TotalCount ?? _page.Count;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. I thought we had this fallback in a different location.

TotalCount should not be displayed if, like not be in the typesystem if the page does not provide it. If you have this it seems there is something misconfigured.

Actually, what we should do here is throw a GraphQLException that no count is available.

@Hanskrogh
Copy link
Contributor Author

@michaelstaib
Added Graphql Exception and setting Page.TotalCount Explicitly to 0 on Page.Empty

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +79 to +80
public int TotalCount =>
_page.TotalCount ?? throw new GraphQLException("Total count is not available for this connection.");
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation no longer “falls back to 0” when the total count is unavailable; it now throws a GraphQLException when _page.TotalCount is null. That’s a behavior change vs both the PR description/title and existing Page->Connection conversion which uses page.TotalCount ?? 0 (e.g. HotChocolatePaginationResultExtensions.CreateConnection: page.TotalCount ?? 0). Consider returning 0 (or keeping the existing sentinel) instead of throwing, or make the GraphQL field nullable if “not available” must be represented explicitly.

Copilot uses AI. Check for mistakes.
/// An empty page.
/// </summary>
public static new ValueCursorPage<T> Empty { get; } = new([], false, false, _ => string.Empty);
public static new ValueCursorPage<T> Empty { get; } = new([], false, false, _ => string.Empty, 0);
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting ValueCursorPage<T>.Empty to have totalCount = 0 changes the public behavior of Page<T>.Empty.TotalCount from null (unknown) to a concrete value (known 0). Since Page<T>.Empty is returned in paging code paths when no items are fetched (e.g. PagingQueryableExtensions.ToPageAsync returns Page<T>.Empty when builder.Count == 0, even when the underlying dataset might not be empty), this can cause TotalCount to be reported as 0 when it’s actually unknown/non-zero. Consider keeping TotalCount as null on Empty and handling any GraphQL-facing fallback (0 vs -1) at the connection layer instead.

Suggested change
public static new ValueCursorPage<T> Empty { get; } = new([], false, false, _ => string.Empty, 0);
public static new ValueCursorPage<T> Empty { get; } = new([], false, false, _ => string.Empty);

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants