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, 'type',
$this->join(['scalar', $def->name, $this->join($def->directives, ' ')], ' ') $def->name,
], "\n"); $this->wrap('implements ', $this->join($def->interfaces, ' & ')),
}, $this->join($def->directives, ' '),
NodeKind::OBJECT_TYPE_DEFINITION => function(ObjectTypeDefinitionNode $def) { $this->block($def->fields)
], ' ');
}),
NodeKind::FIELD_DEFINITION => $this->addDescription(function(FieldDefinitionNode $def) {
return $def->name
. $this->wrap('(', $this->join($def->arguments, ', '), ')')
. ': ' . $def->type
. $this->wrap(' ', $this->join($def->directives, ' '));
}),
NodeKind::INPUT_VALUE_DEFINITION => $this->addDescription(function(InputValueDefinitionNode $def) {
return $this->join([ return $this->join([
$def->description, $def->name . ': ' . $def->type,
$this->join([ $this->wrap('= ', $def->defaultValue),
'type', $this->join($def->directives, ' ')
$def->name, ], ' ');
$this->wrap('implements ', $this->join($def->interfaces, ' & ')), }),
$this->join($def->directives, ' '), NodeKind::INTERFACE_TYPE_DEFINITION => $this->addDescription(function(InterfaceTypeDefinitionNode $def) {
$this->block($def->fields)
], ' ')
], "\n");
},
NodeKind::FIELD_DEFINITION => function(FieldDefinitionNode $def) {
return $this->join([ return $this->join([
$def->description, 'interface',
$def->name $def->name,
. $this->wrap('(', $this->join($def->arguments, ', '), ')') $this->join($def->directives, ' '),
. ': ' . $def->type $this->block($def->fields)
. $this->wrap(' ', $this->join($def->directives, ' ')) ], ' ');
], "\n"); }),
}, NodeKind::UNION_TYPE_DEFINITION => $this->addDescription(function(UnionTypeDefinitionNode $def) {
NodeKind::INPUT_VALUE_DEFINITION => function(InputValueDefinitionNode $def) {
return $this->join([ return $this->join([
$def->description, 'union',
$this->join([ $def->name,
$def->name . ': ' . $def->type, $this->join($def->directives, ' '),
$this->wrap('= ', $def->defaultValue), $def->types
$this->join($def->directives, ' ') ? '= ' . $this->join($def->types, ' | ')
], ' ') : ''
], "\n"); ], ' ');
}, }),
NodeKind::INTERFACE_TYPE_DEFINITION => function(InterfaceTypeDefinitionNode $def) { NodeKind::ENUM_TYPE_DEFINITION => $this->addDescription(function(EnumTypeDefinitionNode $def) {
return $this->join([ return $this->join([
$def->description, 'enum',
$this->join([ $def->name,
'interface', $this->join($def->directives, ' '),
$def->name, $this->block($def->values)
$this->join($def->directives, ' '), ], ' ');
$this->block($def->fields) }),
], ' ') NodeKind::ENUM_VALUE_DEFINITION => $this->addDescription(function(EnumValueDefinitionNode $def) {
], "\n"); return $this->join([$def->name, $this->join($def->directives, ' ')], ' ');
}, }),
NodeKind::UNION_TYPE_DEFINITION => function(UnionTypeDefinitionNode $def) { NodeKind::INPUT_OBJECT_TYPE_DEFINITION => $this->addDescription(function(InputObjectTypeDefinitionNode $def) {
return $this->join([ return $this->join([
$def->description, 'input',
$this->join([ $def->name,
'union', $this->join($def->directives, ' '),
$def->name, $this->block($def->fields)
$this->join($def->directives, ' '), ], ' ');
$def->types }),
? '= ' . $this->join($def->types, ' | ')
: ''
], ' ')
], "\n");
},
NodeKind::ENUM_TYPE_DEFINITION => function(EnumTypeDefinitionNode $def) {
return $this->join([
$def->description,
$this->join([
'enum',
$def->name,
$this->join($def->directives, ' '),
$this->block($def->values)
], ' ')
], "\n");
},
NodeKind::ENUM_VALUE_DEFINITION => function(EnumValueDefinitionNode $def) {
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',
$def->name,
$this->join($def->directives, ' '),
$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.