Created
October 22, 2019 10:27
-
-
Save renepardon/f2a8cc78ea24c66c0a90bcb6a7c0bd94 to your computer and use it in GitHub Desktop.
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
<?php | |
class SchemaPrinter { | |
/** | |
* @param Schema $schema | |
* @param array $options | |
* | |
* @return string | |
*/ | |
public static function printFederatedSchema(Schema $schema, array $options = []): string | |
{ | |
$fields = $directives = []; | |
$originalQueryType = $schema->getQueryType(); | |
$federationFields = ['_service', '_entities']; | |
$federationDirectives = ['external', 'requires', 'provides', 'key', 'extends']; | |
foreach ($originalQueryType->getFields() as $key => $field) { | |
if (in_array($key, $federationFields)) { | |
continue; | |
} | |
$fields[] = $field; | |
} | |
foreach ($schema->getDirectives() as $directive) { | |
if (in_array($directive->name, $federationDirectives)) { | |
continue; | |
} | |
$directives[] = $directive; | |
} | |
$queryType = new ObjectType([ | |
'name' => 'Query', | |
'fields' => $fields, | |
'interfaces' => $originalQueryType->getInterfaces(), | |
]); | |
$newSchema = new Schema([ | |
'query' => $queryType, | |
'mutation' => $schema->getMutationType(), | |
'subscription' => $schema->getSubscriptionType(), | |
'directives' => $directives, | |
]); | |
return self::printFilteredSchema( | |
$newSchema, | |
static function (Directive $type) { | |
return !Directive::isSpecifiedDirective($type); | |
}, | |
static function ($type) { | |
return !Type::isBuiltInType($type); | |
}, | |
$options | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment