Skip to content

Allow empty selection sets - flag version - #4852

Open
martinbonnin wants to merge 7 commits into
17.x.xfrom
empty-selection-sets
Open

Allow empty selection sets - flag version#4852
martinbonnin wants to merge 7 commits into
17.x.xfrom
empty-selection-sets

Conversation

@martinbonnin

Copy link
Copy Markdown
Contributor

@vercel

vercel Bot commented Aug 31, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
graphql-js Ignored Ignored Preview Sep 8, 2026 9:32am UTC

Request Review

@magicmark magicmark left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🥳

@benjie benjie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Solid changes 👍

Comment thread src/validation/validate.ts Outdated
Comment thread src/validation/ValidationContext.ts
@martinbonnin
martinbonnin requested a review from benjie September 3, 2026 14:58

@benjie benjie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM; the options object does add some garbage collection overhead so @yaacovCR might want to trigger the benchmarks, but I imagine it's an incredibly lightweight cost so unlikely to be a big deal.

@martinbonnin martinbonnin changed the title Allow empty selection sets Allow empty selection sets - experimental flag version Sep 4, 2026
@martinbonnin martinbonnin changed the title Allow empty selection sets - experimental flag version Allow empty selection sets - flag version Sep 4, 2026
@glen-84

glen-84 commented Sep 8, 2026

Copy link
Copy Markdown

With the parser now accepting { }, subscription { } parses and validates cleanly, but I think it should still fail validation.

The spec's Single Root Field rule says the collected fields map "must have exactly one entry", and this PR doesn't change that rule. SingleFieldSubscriptionsRule only checks the upper bound though:

if (groupedFieldSet.size > 1) {

That was enough while the grammar guaranteed at least one selection. Now it lets an empty subscription through, and the same goes for subscription { ...Empty } with an empty fragment, since that also collects to zero fields.

Suggestion: report when groupedFieldSet.size === 0 as well, e.g. Subscription "X" must select a top level field. (or fold both into size !== 1), plus a case in SingleFieldSubscriptionsRule-test.ts for the empty operation and the empty-fragment variant.

For reference, Hot Chocolate's implementation of this change (ChilliCream/graphql-platform#10337) allows empty selection sets on query and mutation roots and on composite fields, but keeps rejecting an empty subscription root.

Drafted by Claude (Anthropic AI assistant).

Comment on lines -107 to +114
if (groupedFieldSet.size > 1) {
if (node.selectionSet.selections.length === 0) {
context.reportError(
new GraphQLError(
'Subscription must select exactly one top level field.',
{ nodes: node.selectionSet },
),
);
} else if (groupedFieldSet.size > 1) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I would just do if (groupedFieldSet.size !== 1) {, and update the error message to "must select exactly one".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, I like the "exactly one" too. I would also always put the selection set in the GraphQLErrorOptions

export interface GraphQLErrorOptions {
  /** AST node or nodes associated with this error. */
  nodes?: ReadonlyArray<ASTNode> | ASTNode | null | undefined;

(compared to now the list of extra fields).

Would that be an OK thing to change @yaacovCR @benjie ? Or do we prefer to keep things as is?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants