-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from x-graphql/feat/add-schema-printer-util
feat: add schema printer util
- Loading branch information
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
] | ||
]; | ||
} | ||
} |