Skip to content

Add the maxTokens parser option - #1990

Open
Amoifr wants to merge 4 commits into
webonyx:masterfrom
Amoifr:feat-1905-parser-max-tokens
Open

Amoifr wants to merge 4 commits into
webonyx:masterfrom
Amoifr:feat-1905-parser-max-tokens

Conversation

@Amoifr

@Amoifr Amoifr commented Oct 4, 2026 •

Copy link
Copy Markdown

Resolves #1905.

Adds the maxTokens parser option from graphql-js (graphql/graphql-js#3684): the parser counts the tokens it consumes and throws a SyntaxError once the document exceeds the limit. There is no limit by default.

All the Lexer::advance() calls of the parser now go through a single advanceLexer() method that does the counting, as in the reference implementation. The error message follows the current graphql-js wording ("more than", the original "more that" typo was fixed there), and the graphql-js test is ported with its @see it(...) annotation.

Changelog

Added

Copilot AI 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.

Copilot review overview

🟢 Approval recommended

The implementation is complete and tested; only a non-blocking documentation grammar correction remains.

Review effort: Balanced
Findings: 1 Low severity

Open (1)
What changed in this PR

Adds an opt-in parser token limit to protect parsing resources.

Changes:

  • Centralizes lexer advancement and token counting.
  • Throws SyntaxError when maxTokens is exceeded.
  • Adds boundary/default-limit tests and changelog documentation.
File Description
src/​Language/​Parser.php Implements and documents maxTokens.
tests/​Language/​ParserTest.php Tests limits and unlimited defaults.
CHANGELOG.md Records the new option.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/Language/Parser.php Outdated
Comment on lines +106 to +108
* Parser CPU and memory usage is linear to the number of tokens in a document,
* and parsing happens before validation, so even an invalid document can use a lot of resources.
* Set this option to limit the number of tokens a document may contain, parsing then fails with a syntax error.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed, thanks.

This branch has not been deployed

No deployments
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.

Add maxTokens option to Parser

3 participants