Skip to content

Commit

Permalink
Merge pull request #3 from x-graphql/feat/add-schema-printer-util
Browse files Browse the repository at this point in the history
feat: add schema printer util
  • Loading branch information
vuongxuongminh authored Feb 22, 2024
2 parents 32daca3 + 4c29f4b commit 159227e
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/SchemaPrinter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace XGraphQL\Utils;

use GraphQL\Error\Error;
use GraphQL\Error\SerializationError;
use GraphQL\Language\DirectiveLocation;
use GraphQL\Type\Definition\Directive;
use GraphQL\Type\Definition\NamedType;
use GraphQL\Type\Schema;
use GraphQL\Utils\SchemaPrinter as BaseSchemaPrinter;

final class SchemaPrinter extends BaseSchemaPrinter
{
/**
* @throws SerializationError
* @throws Error
* @throws \JsonException
*/
public static function printSchemaExcludeTypeSystemDirectives(Schema $schema, array $options = []): string
{
$directivesFilter = static function (Directive $directive) {
if (Directive::isSpecifiedDirective($directive)) {
return false;
}

return [] !== array_intersect(DirectiveLocation::EXECUTABLE_LOCATIONS, $directive->locations);
};

return self::printFilteredSchema(
$schema,
$directivesFilter,
static fn(NamedType $type): bool => !$type->isBuiltInType(),
$options
);
}
}
82 changes: 82 additions & 0 deletions tests/SchemaPrinterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace XGraphQL\Utils\Test;

use GraphQL\Utils\BuildSchema;
use PHPUnit\Framework\Attributes\DataProvider;
use XGraphQL\Utils\SchemaPrinter;
use PHPUnit\Framework\TestCase;

class SchemaPrinterTest extends TestCase
{
#[DataProvider(methodName: 'printSchemaExcludeTypeSystemDirectivesTestCaseDataProvider')]
public function testPrintSchemaExcludeSystemDirectives(string $sdl, string $expectSDL): void
{
$schema = BuildSchema::build($sdl);

$this->assertEquals($expectSDL, SchemaPrinter::printSchemaExcludeTypeSystemDirectives($schema));
}

public static function printSchemaExcludeTypeSystemDirectivesTestCaseDataProvider(): array
{
return [
'can exclude type system directives, single position' => [
<<<'SDL'
directive @system_directive on OBJECT
type Query @system_directive {
test: String!
}
SDL,
<<<'SDL'
type Query {
test: String!
}

SDL,
],
'can exclude type system directives, multiple position' => [
<<<'SDL'
directive @system_directive on OBJECT | FIELD_DEFINITION | INTERFACE
type Query @system_directive {
test: String!
}
interface X @system_directive {
id: ID! @system_directive
}
SDL,
<<<'SDL'
type Query {
test: String!
}
interface X {
id: ID!
}

SDL,
],
'should not remove directive if execution location exists' => [
<<<'SDL'
directive @system_directive on OBJECT | FIELD
type Query @system_directive {
test: String!
}
SDL,
<<<'SDL'
directive @system_directive on OBJECT | FIELD
type Query {
test: String!
}

SDL,
]
];
}
}

0 comments on commit 159227e

Please sign in to comment.