lastdragon-ru/graphql-printer
GraphQL printer for PHP: turns a GraphQL AST/document into well-formatted GraphQL text with configurable indentation and style. Useful for debugging, logging, code generation, and producing consistent query/schema output in Laravel or any PHP app.
Installation Add the package via Composer:
composer require lastdragon-ru/graphql-printer
Publish the config (optional):
php artisan vendor:publish --provider="LastDragon\GraphQLPrinter\GraphQLPrinterServiceProvider"
First Use Case Print a GraphQL schema from a string:
use LastDragon\GraphQLPrinter\GraphQLPrinter;
$schema = <<<'GRAPHQL'
type Query {
hello: String
}
GRAPHQL;
$printer = new GraphQLPrinter();
echo $printer->printSchema($schema);
Where to Look First
config/graphql-printer.php (if published) for default settings.LastDragon\GraphQLPrinter\GraphQLPrinter for core functionality.tests/ for usage examples and edge cases.Printing Entire Schema
$printer = new GraphQLPrinter();
$schema = file_get_contents('schema.graphql');
$formattedSchema = $printer->printSchema($schema);
Filtering Types Print only used types in a query:
$query = '{ hello }';
$formatted = $printer->printSchema($schema, [
'types' => ['used'],
]);
Custom Indentation Override default indentation (e.g., 2 spaces):
$printer->setOptions(['indent' => 2]);
$formatted = $printer->printSchema($schema);
Integrating with Laravel
Bind the printer to the container in AppServiceProvider:
$this->app->singleton(GraphQLPrinter::class, function ($app) {
return new GraphQLPrinter($app['config']['graphql-printer']);
});
Use in controllers/services:
$printer = app(GraphQLPrinter::class);
Dynamic Schema Generation
Combine with graphql/php to print dynamically generated schemas:
use GraphQL\Type\Definition\Type;
$schema = new Type\ObjectType([
'name' => 'Query',
'fields' => [
'hello' => Type\Type::string(),
],
]);
$printer->printSchema($schema->toSDL());
$this->assertStringContainsString('type Query', $printer->printSchema($schema));
Schema Parsing Errors
graphql/php.Directives vs. Types
directives option (e.g., ['directives' => 'all']) may omit critical directives.['directives' => 'used'] for production schemas to avoid clutter.Performance
$cacheKey = 'graphql_schema_formatted';
$formatted = Cache::remember($cacheKey, now()->addHours(1), function () use ($printer, $schema) {
return $printer->printSchema($schema);
});
Config Overrides
config/graphql-printer.php may conflict with runtime options.printSchema() to override defaults:
$printer->printSchema($schema, ['indent' => 4, 'types' => ['wanted' => ['User']]]);
\Log::debug('Raw Schema:', [$schema]);
\Log::debug('Printed Schema:', [$printer->printSchema($schema)]);
graphql/php's Source class to check syntax:
use GraphQL\Language\Parser;
try {
$parser = new Parser();
$parser->parse($schema); // Throws on invalid SDL
} catch (\Exception $e) {
\Log::error('Invalid GraphQL SDL:', ['error' => $e->getMessage()]);
}
Custom Formatters Extend the printer by implementing a custom formatter:
class CustomPrinter extends GraphQLPrinter {
protected function formatType($type) {
// Custom logic
return "/* Custom */ $type";
}
}
Hooks for Pre/Post-Processing
Override prePrint() and postPrint() in a subclass to modify input/output:
class ExtendedPrinter extends GraphQLPrinter {
protected function prePrint($schema) {
return str_replace('oldTerm', 'newTerm', $schema);
}
}
Directive/Type Filters Dynamically filter types/directives at runtime:
$options = [
'types' => ['wanted' => collect($this->getUsedTypes($query))->pluck('name')->toArray()],
];
$printer->printSchema($schema, $options);
How can I help you explore Laravel packages today?