Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 91 additions & 77 deletions src/language/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,64 +106,79 @@ const printDocASTReducer: any = {

// Type System Definitions

SchemaDefinition: addDescription(({ directives, operationTypes }) =>
SchemaDefinition: ({ description, directives, operationTypes }) =>
wrap('', description, '\n') +
join(['schema', join(directives, ' '), block(operationTypes)], ' '),
),

OperationTypeDefinition: ({ operation, type }) => operation + ': ' + type,

ScalarTypeDefinition: addDescription(({ name, directives }) =>
ScalarTypeDefinition: ({ description, name, directives }) =>
wrap('', description, '\n') +
join(['scalar', name, join(directives, ' ')], ' '),
),

ObjectTypeDefinition: addDescription(
({ name, interfaces, directives, fields }) =>
join(
[
'type',
name,
wrap('implements ', join(interfaces, ' & ')),
join(directives, ' '),
block(fields),
],
' ',
),
),

FieldDefinition: addDescription(
({ name, arguments: args, type, directives }) =>
name +
(hasMultilineItems(args)
? wrap('(\n', indent(join(args, '\n')), '\n)')
: wrap('(', join(args, ', '), ')')) +
': ' +
type +
wrap(' ', join(directives, ' ')),
),

InputValueDefinition: addDescription(
({ name, type, defaultValue, directives }) =>
join(
[name + ': ' + type, wrap('= ', defaultValue), join(directives, ' ')],
' ',
),
),

InterfaceTypeDefinition: addDescription(
({ name, interfaces, directives, fields }) =>
join(
[
'interface',
name,
wrap('implements ', join(interfaces, ' & ')),
join(directives, ' '),
block(fields),
],
' ',
),
),

UnionTypeDefinition: addDescription(({ name, directives, types }) =>

ObjectTypeDefinition: ({
description,
name,
interfaces,
directives,
fields,
}) =>
wrap('', description, '\n') +
join(
[
'type',
name,
wrap('implements ', join(interfaces, ' & ')),
join(directives, ' '),
block(fields),
],
' ',
),

FieldDefinition: ({ description, name, arguments: args, type, directives }) =>
wrap('', description, '\n') +
name +
(hasMultilineItems(args)
? wrap('(\n', indent(join(args, '\n')), '\n)')
: wrap('(', join(args, ', '), ')')) +
': ' +
type +
wrap(' ', join(directives, ' ')),

InputValueDefinition: ({
description,
name,
type,
defaultValue,
directives,
}) =>
wrap('', description, '\n') +
join(
[name + ': ' + type, wrap('= ', defaultValue), join(directives, ' ')],
' ',
),

InterfaceTypeDefinition: ({
description,
name,
interfaces,
directives,
fields,
}) =>
wrap('', description, '\n') +
join(
[
'interface',
name,
wrap('implements ', join(interfaces, ' & ')),
join(directives, ' '),
block(fields),
],
' ',
),

UnionTypeDefinition: ({ description, name, directives, types }) =>
wrap('', description, '\n') +
join(
[
'union',
Expand All @@ -173,31 +188,34 @@ const printDocASTReducer: any = {
],
' ',
),
),

EnumTypeDefinition: addDescription(({ name, directives, values }) =>
EnumTypeDefinition: ({ description, name, directives, values }) =>
wrap('', description, '\n') +
join(['enum', name, join(directives, ' '), block(values)], ' '),
),

EnumValueDefinition: addDescription(({ name, directives }) =>
join([name, join(directives, ' ')], ' '),
),
EnumValueDefinition: ({ description, name, directives }) =>
wrap('', description, '\n') + join([name, join(directives, ' ')], ' '),

InputObjectTypeDefinition: addDescription(({ name, directives, fields }) =>
InputObjectTypeDefinition: ({ description, name, directives, fields }) =>
wrap('', description, '\n') +
join(['input', name, join(directives, ' '), block(fields)], ' '),
),

DirectiveDefinition: addDescription(
({ name, arguments: args, repeatable, locations }) =>
'directive @' +
name +
(hasMultilineItems(args)
? wrap('(\n', indent(join(args, '\n')), '\n)')
: wrap('(', join(args, ', '), ')')) +
(repeatable ? ' repeatable' : '') +
' on ' +
join(locations, ' | '),
),

DirectiveDefinition: ({
description,
name,
arguments: args,
repeatable,
locations,
}) =>
wrap('', description, '\n') +
'directive @' +
name +
(hasMultilineItems(args)
? wrap('(\n', indent(join(args, '\n')), '\n)')
: wrap('(', join(args, ', '), ')')) +
(repeatable ? ' repeatable' : '') +
' on ' +
join(locations, ' | '),

SchemaExtension: ({ directives, operationTypes }) =>
join(['extend schema', join(directives, ' '), block(operationTypes)], ' '),
Expand Down Expand Up @@ -247,10 +265,6 @@ const printDocASTReducer: any = {
join(['extend input', name, join(directives, ' '), block(fields)], ' '),
};

function addDescription(cb) {
return (node) => join([node.description, cb(node)], '\n');
}

/**
* Given maybeArray, print an empty string if it is null or empty, otherwise
* print all items together separated by separator if provided
Expand Down