Skip to content

Instantly share code, notes, and snippets.

@renepardon
Created October 22, 2019 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renepardon/f2a8cc78ea24c66c0a90bcb6a7c0bd94 to your computer and use it in GitHub Desktop.
Save renepardon/f2a8cc78ea24c66c0a90bcb6a7c0bd94 to your computer and use it in GitHub Desktop.
<?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