Printer: created special function to add descriptions

This commit is contained in:
Vladimir Razuvaev 2018-08-08 01:28:34 +07:00
parent ccb9486d21
commit d9aee43129

View File

@ -200,95 +200,68 @@ class Printer
return $def->operation . ': ' . $def->type; return $def->operation . ': ' . $def->type;
}, },
NodeKind::SCALAR_TYPE_DEFINITION => function(ScalarTypeDefinitionNode $def) { NodeKind::SCALAR_TYPE_DEFINITION => $this->addDescription(function(ScalarTypeDefinitionNode $def) {
return $this->join(['scalar', $def->name, $this->join($def->directives, ' ')], ' ');
}),
NodeKind::OBJECT_TYPE_DEFINITION => $this->addDescription(function(ObjectTypeDefinitionNode $def) {
return $this->join([ return $this->join([
$def->description,
$this->join(['scalar', $def->name, $this->join($def->directives, ' ')], ' ')
], "\n");
},
NodeKind::OBJECT_TYPE_DEFINITION => function(ObjectTypeDefinitionNode $def) {
return $this->join([
$def->description,
$this->join([
'type', 'type',
$def->name, $def->name,
$this->wrap('implements ', $this->join($def->interfaces, ' & ')), $this->wrap('implements ', $this->join($def->interfaces, ' & ')),
$this->join($def->directives, ' '), $this->join($def->directives, ' '),
$this->block($def->fields) $this->block($def->fields)
], ' ') ], ' ');
], "\n"); }),
}, NodeKind::FIELD_DEFINITION => $this->addDescription(function(FieldDefinitionNode $def) {
NodeKind::FIELD_DEFINITION => function(FieldDefinitionNode $def) { return $def->name
return $this->join([
$def->description,
$def->name
. $this->wrap('(', $this->join($def->arguments, ', '), ')') . $this->wrap('(', $this->join($def->arguments, ', '), ')')
. ': ' . $def->type . ': ' . $def->type
. $this->wrap(' ', $this->join($def->directives, ' ')) . $this->wrap(' ', $this->join($def->directives, ' '));
], "\n"); }),
}, NodeKind::INPUT_VALUE_DEFINITION => $this->addDescription(function(InputValueDefinitionNode $def) {
NodeKind::INPUT_VALUE_DEFINITION => function(InputValueDefinitionNode $def) {
return $this->join([ return $this->join([
$def->description,
$this->join([
$def->name . ': ' . $def->type, $def->name . ': ' . $def->type,
$this->wrap('= ', $def->defaultValue), $this->wrap('= ', $def->defaultValue),
$this->join($def->directives, ' ') $this->join($def->directives, ' ')
], ' ') ], ' ');
], "\n"); }),
}, NodeKind::INTERFACE_TYPE_DEFINITION => $this->addDescription(function(InterfaceTypeDefinitionNode $def) {
NodeKind::INTERFACE_TYPE_DEFINITION => function(InterfaceTypeDefinitionNode $def) {
return $this->join([ return $this->join([
$def->description,
$this->join([
'interface', 'interface',
$def->name, $def->name,
$this->join($def->directives, ' '), $this->join($def->directives, ' '),
$this->block($def->fields) $this->block($def->fields)
], ' ') ], ' ');
], "\n"); }),
}, NodeKind::UNION_TYPE_DEFINITION => $this->addDescription(function(UnionTypeDefinitionNode $def) {
NodeKind::UNION_TYPE_DEFINITION => function(UnionTypeDefinitionNode $def) {
return $this->join([ return $this->join([
$def->description,
$this->join([
'union', 'union',
$def->name, $def->name,
$this->join($def->directives, ' '), $this->join($def->directives, ' '),
$def->types $def->types
? '= ' . $this->join($def->types, ' | ') ? '= ' . $this->join($def->types, ' | ')
: '' : ''
], ' ') ], ' ');
], "\n"); }),
}, NodeKind::ENUM_TYPE_DEFINITION => $this->addDescription(function(EnumTypeDefinitionNode $def) {
NodeKind::ENUM_TYPE_DEFINITION => function(EnumTypeDefinitionNode $def) {
return $this->join([ return $this->join([
$def->description,
$this->join([
'enum', 'enum',
$def->name, $def->name,
$this->join($def->directives, ' '), $this->join($def->directives, ' '),
$this->block($def->values) $this->block($def->values)
], ' ') ], ' ');
], "\n"); }),
}, NodeKind::ENUM_VALUE_DEFINITION => $this->addDescription(function(EnumValueDefinitionNode $def) {
NodeKind::ENUM_VALUE_DEFINITION => function(EnumValueDefinitionNode $def) { return $this->join([$def->name, $this->join($def->directives, ' ')], ' ');
}),
NodeKind::INPUT_OBJECT_TYPE_DEFINITION => $this->addDescription(function(InputObjectTypeDefinitionNode $def) {
return $this->join([ return $this->join([
$def->description,
$this->join([$def->name, $this->join($def->directives, ' ')], ' ')
], "\n");
},
NodeKind::INPUT_OBJECT_TYPE_DEFINITION => function(InputObjectTypeDefinitionNode $def) {
return $this->join([
$def->description,
$this->join([
'input', 'input',
$def->name, $def->name,
$this->join($def->directives, ' '), $this->join($def->directives, ' '),
$this->block($def->fields) $this->block($def->fields)
], ' ') ], ' ');
], "\n"); }),
},
NodeKind::SCALAR_TYPE_EXTENSION => function(ScalarTypeExtensionNode $def) { NodeKind::SCALAR_TYPE_EXTENSION => function(ScalarTypeExtensionNode $def) {
return $this->join([ return $this->join([
'extend scalar', 'extend scalar',
@ -339,17 +312,23 @@ class Printer
$this->block($def->fields), $this->block($def->fields),
], ' '); ], ' ');
}, },
NodeKind::DIRECTIVE_DEFINITION => function(DirectiveDefinitionNode $def) { NodeKind::DIRECTIVE_DEFINITION => $this->addDescription(function(DirectiveDefinitionNode $def) {
return $this->join([ return 'directive @'
$def->description, . $def->name
'directive @' . $def->name . $this->wrap('(', $this->join($def->arguments, ', '), ')') . $this->wrap('(', $this->join($def->arguments, ', '), ')')
. ' on ' . $this->join($def->locations, ' | ') . ' on ' . $this->join($def->locations, ' | ');
], "\n"); })
}
] ]
]); ]);
} }
public function addDescription(\Closure $cb)
{
return function ($node) use ($cb) {
return $this->join([$node->description, $cb($node)]);
};
}
/** /**
* If maybeString is not null or empty, then wrap with start and end, otherwise * If maybeString is not null or empty, then wrap with start and end, otherwise
* print an empty string. * print an empty string.