lastdragon-ru / graphql-printer
The package allows you to print GraphQL Schema and Queries in a highly customized way eg you can choose indent size, print only used/wanted/all types, print only one type, print used/wanted/all directives and even check which types/directives are used in the Schema/Query.
Installs: 2
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
pkg:composer/lastdragon-ru/graphql-printer
Requires
- php: ^8.3|^8.4
- ext-mbstring: *
- composer/semver: ^3.2
- symfony/deprecation-contracts: ^3.0.0
- symfony/polyfill-php84: ^1.31
- webonyx/graphql-php: ^15.4.0
Requires (Dev)
- composer/class-map-generator: ^1.0
- lastdragon-ru/phpunit-extensions: 10.0.0
- lastdragon-ru/phpunit-graphql: 10.0.0
- mockery/mockery: ^1.6.6
- phpunit/phpunit: ^11.2.0|^12.0.0
Suggests
- lastdragon-ru/phpunit-graphql: Useful assertions for PHPUnit to check printed/exported type/queries.
Conflicts
- thecodingmachine/safe: <3.0.0
- dev-main
- 10.0.0
- 9.x-dev
- 9.3.1
- 9.3.0
- 9.2.0
- 9.1.0
- 9.0.0
- 8.x-dev
- 8.1.1
- 8.1.0
- 8.0.0
- 7.x-dev
- 7.2.0
- 7.1.0
- 7.0.1
- 7.0.0
- 6.x-dev
- 6.4.2
- 6.4.1
- 6.4.0
- 6.3.0
- 6.2.0
- 6.1.0
- 6.0.0
- 5.x-dev
- 5.6.0
- 5.5.0
- 5.4.0
- 5.3.1
- 5.3.0
- 5.2.0
- 5.1.0
- 5.0.0
- 5.0.0-beta.1
- 5.0.0-beta.0
- 4.x-dev
- 4.6.0
- 4.5.2
- 4.5.1
- 4.5.0
- 4.4.0
- 4.3.0
- 4.2.1
- 4.2.0
- 4.1.0
- 4.0.0
- 3.0.0
This package is auto-updated.
Last update: 2026-01-23 13:01:56 UTC
README
The package allows you to print GraphQL Schema and Queries in a highly customized way eg you can choose indent size, print only used/wanted/all types, print only one type, print used/wanted/all directives (it is not possible with standard printer) and even check which types/directives are used in the Schema/Query.
Requirements
| Requirement | Constraint | Supported by |
|---|---|---|
| PHP | ^8.4 |
HEAD ⋯ 8.0.0 |
^8.3 |
HEAD ⋯ 5.0.0 |
|
^8.2 |
7.2.0 ⋯ 3.0.0 |
|
^8.1 |
6.4.2 ⋯ 3.0.0 |
|
^8.0 |
4.6.0 ⋯ 3.0.0 |
|
webonyx/graphql-php |
^15.4.0 |
HEAD ⋯ 4.2.1 |
^15.2.4 |
4.2.0 ⋯ 4.0.0 |
|
^14.11.9 |
3.0.0 |
Installation
composer require lastdragon-ru/graphql-printer
Usage
There are two primary methods: Printer::print() and Printer::export(). The print() will print the current type only, meanwhile export() will print the current type and all used types/directives:
<?php declare(strict_types = 1); use GraphQL\Utils\BuildSchema; use LastDragon_ru\GraphQLPrinter\Printer; use LastDragon_ru\GraphQLPrinter\Settings\DefaultSettings; use LastDragon_ru\LaraASP\Dev\App\Example; $schema = BuildSchema::build( <<<'GRAPHQL' type Query { a: A } type A @a { id: ID! b: [B!] } type B @b { id: ID! } directive @a on OBJECT directive @b on OBJECT GRAPHQL, ); $type = $schema->getType('A'); $settings = new DefaultSettings(); $printer = new Printer($settings, null, $schema); assert($type !== null); Example::raw($printer->print($type), 'graphql'); Example::raw($printer->export($type), 'graphql');
The $printer->print($type) is:
type A @a { b: [B!] id: ID! }
The $printer->export($type) is:
type A @a { b: [B!] id: ID! } directive @a on | OBJECT directive @b on | OBJECT type B @b { id: ID! }
Customization
Please see:
Settingsdirectory to see built-in settings;Settingsinterface to see all supported settings;DirectiveResolverinterface to define your own way to find all available directives and their definitions;
Filtering
Note
By default, built-in/internal type/directives are not printed, if you want/need them, you should allow them by type/directive definitions filters.
The Printer allows filter out types and directives. This may be useful to exclude them from the schema completely. Filtering also works for queries. Please note that types filtering will work only if the schema is known (the schema is required to determine the type of argument nodes). For some AST node types, their type may also be required.
<?php declare(strict_types = 1); use GraphQL\Language\Parser; use GraphQL\Utils\BuildSchema; use LastDragon_ru\GraphQLPrinter\Contracts\DirectiveFilter; use LastDragon_ru\GraphQLPrinter\Contracts\TypeFilter; use LastDragon_ru\GraphQLPrinter\Printer; use LastDragon_ru\GraphQLPrinter\Settings\DefaultSettings; use LastDragon_ru\LaraASP\Dev\App\Example; $typeFilter = new class() implements TypeFilter { #[Override] public function isAllowedType(string $type, bool $isStandard): bool { return $type !== 'Forbidden'; } }; $directiveFilter = new class() implements DirectiveFilter { #[Override] public function isAllowedDirective(string $directive, bool $isStandard): bool { return $directive !== 'forbidden'; } }; $schema = BuildSchema::build( <<<'GRAPHQL' type Query { allowed: Boolean @forbidden @allowed forbidden: Forbidden } type Forbidden { id: ID! } directive @allowed on FIELD_DEFINITION directive @forbidden on FIELD_DEFINITION GRAPHQL, ); $query = Parser::parse( <<<'GRAPHQL' query { allowed forbidden { id } } GRAPHQL, ); $settings = (new DefaultSettings()) ->setDirectiveFilter($directiveFilter) ->setTypeFilter($typeFilter); $printer = new Printer($settings, null, $schema); Example::raw($printer->print($schema), 'graphql'); Example::raw($printer->print($query), 'graphql');
The $printer->print($schema) is:
directive @allowed on | FIELD_DEFINITION type Query { allowed: Boolean @allowed }
The $printer->print($query) is:
query { allowed }
Laravel/Lighthouse
It is highly recommended to use lastdragon-ru/lara-asp-graphql package to use the Printer within the Laravel/Lighthouse application.
Testing Assertions
There are also a few useful assertions for PHPUnit to check printed/exported type/queries, please see the lastdragon-ru/phpunit-graphql package.
Upgrading
Please follow Upgrade Guide.
Migration from lastdragon-ru/lara-asp-graphql-printer
- Use
lastdragon-ru/graphql-printerinstead oflastdragon-ru/lara-asp-graphql-printerincomposer.json - Use
lastdragon-ru/phpunit-graphqlpackage if you use PHPUnit assertions. - Use
LastDragon_ru\GraphQLPrinter\*instead ofLastDragon_ru\LaraASP\GraphQLPrinter\*in code.
Contributing
Please use the main repository to report issues, send pull requests, or ask questions.