Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add Twig CS Fixer rule to auto-format GraphQL blocks#72

Merged
ruudk merged 2 commits into
mainfrom
claude/graphql-twig-fixer-uvi0a
May 15, 2026
Merged

Add Twig CS Fixer rule to auto-format GraphQL blocks#72
ruudk merged 2 commits into
mainfrom
claude/graphql-twig-fixer-uvi0a

Conversation

@ruudk
Copy link
Copy Markdown
Owner

@ruudk ruudk commented May 15, 2026

Summary

Adds a Twig CS Fixer fixer that automatically formats the GraphQL operations inside {% graphql %}...{% endgraphql %} blocks in Twig templates.

  • src/Twig/GraphQLFormatterRule.php — a fixable Twig CS Fixer rule that reconstructs the raw GraphQL from a block, reformats it via GraphQL\Language\Printer::doPrint(Parser::parse(...)), and re-indents every line to match the block's own indentation. Unparseable GraphQL is reported as an error and left untouched; blocks that embed Twig (variables/tags/comments) are skipped since they can't be safely round-tripped.
  • Registered in .twig-cs-fixer.php, and the existing project templates were normalized to the canonical 2-space webonyx/graphql-php format (README auto-synced to match).
  • Added the vincentlanglet/twig-cs-fixer dev-dependency-in-prod exception in composer-dependency-analyser.php, mirroring the existing twig/twig entry.
  • Documented in the README under a new "Auto-Format GraphQL in Twig" section, with the .twig-cs-fixer.php config kept in sync via the readme-examples-sync source directive.

Test plan

  • tests/GraphQLFormatterRule/GraphQLFormatterRuleTest.php — functional test driving the real twig-cs-fixer Linter/Fixer pipeline over fixture templates:
    • unformatted block → fixed to canonical output
    • already-formatted block → idempotent, zero violations
    • unformatted block → one error violation reported
    • invalid GraphQL → error reported, content left unchanged
  • Fixtures use a .html.twig.fixture extension so the pre-commit twig-cs-fixer fix hook (globs *.twig) can't mutate the intentionally-malformed inputs
  • Full suite green: PHPUnit (159 tests), PHPStan, php-cs-fixer, twig-cs-fixer lint, composer-dependency-analyser

https://claude.ai/code/session_0172zkGgfrZ2KQ8ngMJ1X3zr


Generated by Claude Code

claude added 2 commits May 15, 2026 17:18
Introduces GraphQLFormatterRule which reformats the GraphQL operation
inside `{% graphql %}...{% endgraphql %}` blocks using
webonyx/graphql-php Printer::doPrint, indented to match the block.
Invalid GraphQL is reported as an error and left untouched.

Wired into .twig-cs-fixer.php and applied to the existing fixture
templates so they use the canonical formatting. Covered by a
functional test that runs the real twig-cs-fixer Linter/Fixer
pipeline over fixture templates.

https://claude.ai/code/session_0172zkGgfrZ2KQ8ngMJ1X3zr
Adds an "Auto-Format GraphQL in Twig" section explaining
GraphQLFormatterRule and showing the .twig-cs-fixer.php setup
(kept in sync via the readme-examples-sync source directive).

https://claude.ai/code/session_0172zkGgfrZ2KQ8ngMJ1X3zr
@ruudk ruudk merged commit 3c9fa34 into main May 15, 2026
3 checks passed
@ruudk ruudk deleted the claude/graphql-twig-fixer-uvi0a branch May 15, 2026 17:27
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.

2 participants