Skip to content

Commit

Permalink
Printer: create special function to add description (#1175)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored and leebyron committed Dec 21, 2017
1 parent 94234c8 commit fd4a69c
Showing 1 changed file with 67 additions and 112 deletions.
179 changes: 67 additions & 112 deletions src/language/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,113 +110,68 @@ const printDocASTReducer = {

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

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

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

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

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

InterfaceTypeDefinition: ({ description, name, directives, fields }) =>
ScalarTypeDefinition: addDescription(({ name, directives }) =>
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 +
wrap('(', join(args, ', '), ')') +
': ' +
type +
wrap(' ', join(directives, ' ')),
),

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

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

UnionTypeDefinition: addDescription(({ name, directives, types }) =>
join(
[
description,
join(['interface', name, join(directives, ' '), block(fields)], ' '),
'union',
name,
join(directives, ' '),
types && types.length !== 0 ? '= ' + join(types, ' | ') : '',
],
'\n',
' ',
),
),

UnionTypeDefinition: ({ description, name, directives, types }) =>
join(
[
description,
join(
[
'union',
name,
join(directives, ' '),
types && types.length !== 0 ? '= ' + join(types, ' | ') : '',
],
' ',
),
],
'\n',
),
EnumTypeDefinition: addDescription(({ name, directives, values }) =>
join(['enum', name, join(directives, ' '), block(values)], ' '),
),

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

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

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

ScalarTypeExtension: ({ name, directives }) =>
join(['extend scalar', name, join(directives, ' ')], ' '),
Expand Down Expand Up @@ -253,20 +208,20 @@ const printDocASTReducer = {
InputObjectTypeExtension: ({ name, directives, fields }) =>
join(['extend input', name, join(directives, ' '), block(fields)], ' '),

DirectiveDefinition: ({ description, name, arguments: args, locations }) =>
join(
[
description,
'directive @' +
name +
wrap('(', join(args, ', '), ')') +
' on ' +
join(locations, ' | '),
],
'\n',
),
DirectiveDefinition: addDescription(
({ name, arguments: args, locations }) =>
'directive @' +
name +
wrap('(', join(args, ', '), ')') +
' on ' +
join(locations, ' | '),
),
};

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

0 comments on commit fd4a69c

Please sign in to comment.