Skip to content

Commit

Permalink
Exclude unnecessary extend schema from federation schema printer
Browse files Browse the repository at this point in the history
  • Loading branch information
cappuc authored Nov 28, 2023
1 parent 198afbf commit 12090c3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/Federation/SchemaPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ class SchemaPrinter extends GraphQLSchemaPrinter
{
protected static function printSchemaDefinition(Schema $schema): string
{
$schemaExtensionDirectives = self::printDirectives(FederationHelper::schemaExtensionDirectives($schema));
$result = "extend schema{$schemaExtensionDirectives}\n";
$result = '';

$schemaExtensionDirectives = FederationHelper::schemaExtensionDirectives($schema);
if ($schemaExtensionDirectives !== []) {
$result .= 'extend schema' . self::printDirectives($schemaExtensionDirectives);
}

$directivesToCompose = FederationHelper::directivesToCompose($schema);

Expand All @@ -30,7 +34,7 @@ protected static function printSchemaDefinition(Schema $schema): string
->definition(),
$directivesToCompose,
);
$result .= "\n" . implode("\n\n", $directivesToComposeDefinitions);
$result .= "\n\n" . implode("\n\n", $directivesToComposeDefinitions);
}

return $result;
Expand Down
7 changes: 5 additions & 2 deletions tests/Console/PrintFederationSchemaCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ public function testPrintsSchemaAsGraphQLSDL(): void
$tester = $this->commandTester(new PrintSchemaCommand());
$tester->execute(['--federation' => true]);

$this->assertStringContainsString(self::SCHEMA_TYPE, $tester->getDisplay());
$this->assertStringContainsString(self::SCHEMA_QUERY, $tester->getDisplay());
$sdl = $tester->getDisplay();

$this->assertStringContainsString(self::SCHEMA_TYPE, $sdl);
$this->assertStringContainsString(self::SCHEMA_QUERY, $sdl);
$this->assertStringNotContainsString('extend schema', $sdl);
}

public function testWritesSchema(): void
Expand Down

0 comments on commit 12090c3

Please sign in to comment.