Skip to content

Commit fd4a69c

Browse files
IvanGoncharovleebyron
authored andcommitted
Printer: create special function to add description (#1175)
1 parent 94234c8 commit fd4a69c

File tree

1 file changed

+67
-112
lines changed

1 file changed

+67
-112
lines changed

src/language/printer.js

+67-112
Original file line numberDiff line numberDiff line change
@@ -110,113 +110,68 @@ const printDocASTReducer = {
110110

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

113-
ScalarTypeDefinition: ({ description, name, directives }) =>
114-
join(
115-
[description, join(['scalar', name, join(directives, ' ')], ' ')],
116-
'\n',
117-
),
118-
119-
ObjectTypeDefinition: ({
120-
description,
121-
name,
122-
interfaces,
123-
directives,
124-
fields,
125-
}) =>
126-
join(
127-
[
128-
description,
129-
join(
130-
[
131-
'type',
132-
name,
133-
wrap('implements ', join(interfaces, ' & ')),
134-
join(directives, ' '),
135-
block(fields),
136-
],
137-
' ',
138-
),
139-
],
140-
'\n',
141-
),
142-
143-
FieldDefinition: ({ description, name, arguments: args, type, directives }) =>
144-
join(
145-
[
146-
description,
147-
name +
148-
wrap('(', join(args, ', '), ')') +
149-
': ' +
150-
type +
151-
wrap(' ', join(directives, ' ')),
152-
],
153-
'\n',
154-
),
155-
156-
InputValueDefinition: ({
157-
description,
158-
name,
159-
type,
160-
defaultValue,
161-
directives,
162-
}) =>
163-
join(
164-
[
165-
description,
166-
join(
167-
[name + ': ' + type, wrap('= ', defaultValue), join(directives, ' ')],
168-
' ',
169-
),
170-
],
171-
'\n',
172-
),
173-
174-
InterfaceTypeDefinition: ({ description, name, directives, fields }) =>
113+
ScalarTypeDefinition: addDescription(({ name, directives }) =>
114+
join(['scalar', name, join(directives, ' ')], ' '),
115+
),
116+
117+
ObjectTypeDefinition: addDescription(
118+
({ name, interfaces, directives, fields }) =>
119+
join(
120+
[
121+
'type',
122+
name,
123+
wrap('implements ', join(interfaces, ' & ')),
124+
join(directives, ' '),
125+
block(fields),
126+
],
127+
' ',
128+
),
129+
),
130+
131+
FieldDefinition: addDescription(
132+
({ name, arguments: args, type, directives }) =>
133+
name +
134+
wrap('(', join(args, ', '), ')') +
135+
': ' +
136+
type +
137+
wrap(' ', join(directives, ' ')),
138+
),
139+
140+
InputValueDefinition: addDescription(
141+
({ name, type, defaultValue, directives }) =>
142+
join(
143+
[name + ': ' + type, wrap('= ', defaultValue), join(directives, ' ')],
144+
' ',
145+
),
146+
),
147+
148+
InterfaceTypeDefinition: addDescription(({ name, directives, fields }) =>
149+
join(['interface', name, join(directives, ' '), block(fields)], ' '),
150+
),
151+
152+
UnionTypeDefinition: addDescription(({ name, directives, types }) =>
175153
join(
176154
[
177-
description,
178-
join(['interface', name, join(directives, ' '), block(fields)], ' '),
155+
'union',
156+
name,
157+
join(directives, ' '),
158+
types && types.length !== 0 ? '= ' + join(types, ' | ') : '',
179159
],
180-
'\n',
160+
' ',
181161
),
162+
),
182163

183-
UnionTypeDefinition: ({ description, name, directives, types }) =>
184-
join(
185-
[
186-
description,
187-
join(
188-
[
189-
'union',
190-
name,
191-
join(directives, ' '),
192-
types && types.length !== 0 ? '= ' + join(types, ' | ') : '',
193-
],
194-
' ',
195-
),
196-
],
197-
'\n',
198-
),
164+
EnumTypeDefinition: addDescription(({ name, directives, values }) =>
165+
join(['enum', name, join(directives, ' '), block(values)], ' '),
166+
),
199167

200-
EnumTypeDefinition: ({ description, name, directives, values }) =>
201-
join(
202-
[
203-
description,
204-
join(['enum', name, join(directives, ' '), block(values)], ' '),
205-
],
206-
'\n',
207-
),
168+
EnumValueDefinition: addDescription(({ name, directives }) =>
169+
join([name, join(directives, ' ')], ' '),
170+
),
208171

209-
EnumValueDefinition: ({ description, name, directives }) =>
210-
join([description, join([name, join(directives, ' ')], ' ')], '\n'),
211-
212-
InputObjectTypeDefinition: ({ description, name, directives, fields }) =>
213-
join(
214-
[
215-
description,
216-
join(['input', name, join(directives, ' '), block(fields)], ' '),
217-
],
218-
'\n',
219-
),
172+
InputObjectTypeDefinition: addDescription(({ name, directives, fields }) =>
173+
join(['input', name, join(directives, ' '), block(fields)], ' '),
174+
),
220175

221176
ScalarTypeExtension: ({ name, directives }) =>
222177
join(['extend scalar', name, join(directives, ' ')], ' '),
@@ -253,20 +208,20 @@ const printDocASTReducer = {
253208
InputObjectTypeExtension: ({ name, directives, fields }) =>
254209
join(['extend input', name, join(directives, ' '), block(fields)], ' '),
255210

256-
DirectiveDefinition: ({ description, name, arguments: args, locations }) =>
257-
join(
258-
[
259-
description,
260-
'directive @' +
261-
name +
262-
wrap('(', join(args, ', '), ')') +
263-
' on ' +
264-
join(locations, ' | '),
265-
],
266-
'\n',
267-
),
211+
DirectiveDefinition: addDescription(
212+
({ name, arguments: args, locations }) =>
213+
'directive @' +
214+
name +
215+
wrap('(', join(args, ', '), ')') +
216+
' on ' +
217+
join(locations, ' | '),
218+
),
268219
};
269220

221+
function addDescription(cb) {
222+
return node => join([node.description, cb(node)], '\n');
223+
}
224+
270225
/**
271226
* Given maybeArray, print an empty string if it is null or empty, otherwise
272227
* print all items together separated by separator if provided

0 commit comments

Comments
 (0)