Skip to content

Commit 5ae0394

Browse files
committed
fix(utils): print comments as blocks correctly
1 parent 36a81a5 commit 5ae0394

File tree

3 files changed

+45
-110
lines changed

3 files changed

+45
-110
lines changed

.changeset/three-houses-cry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-tools/utils': patch
3+
---
4+
5+
Print comments as blocks
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Kind, type StringValueNode } from 'graphql';
2+
3+
interface ObjectWithDescription {
4+
astNode?: {
5+
description?: StringValueNode | null;
6+
} | null;
7+
description?: string | null;
8+
}
9+
10+
export function getDescriptionNode(obj: ObjectWithDescription): StringValueNode | undefined {
11+
if (obj.astNode?.description) {
12+
return {
13+
...obj.astNode.description,
14+
block: true,
15+
};
16+
}
17+
if (obj.description) {
18+
return {
19+
kind: Kind.STRING,
20+
value: obj.description,
21+
block: true,
22+
};
23+
}
24+
}

packages/utils/src/print-schema-with-directives.ts

Lines changed: 16 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ import {
4242
ScalarTypeDefinitionNode,
4343
SchemaDefinitionNode,
4444
SchemaExtensionNode,
45-
StringValueNode,
4645
TypeDefinitionNode,
4746
TypeExtensionNode,
4847
UnionTypeDefinitionNode,
4948
} from 'graphql';
5049
import { astFromType } from './astFromType.js';
5150
import { astFromValue } from './astFromValue.js';
5251
import { astFromValueUntyped } from './astFromValueUntyped.js';
52+
import { getDescriptionNode } from './descriptionFromObject.js';
5353
import { getDirectivesInExtensions } from './get-directives.js';
5454
import { isSome } from './helpers.js';
5555
import { getRootTypeMap } from './rootTypes.js';
@@ -182,17 +182,10 @@ export function astFromSchema(
182182
directives: directives as any,
183183
};
184184

185-
// This code is so weird because it needs to support GraphQL.js 14
186-
// In GraphQL.js 14 there is no `description` value on schemaNode
187-
(schemaNode as unknown as { description?: StringValueNode }).description =
188-
(schema.astNode as unknown as { description: string })?.description ??
189-
(schema as unknown as { description: string }).description != null
190-
? {
191-
kind: Kind.STRING,
192-
value: (schema as unknown as { description: string }).description,
193-
block: true,
194-
}
195-
: undefined;
185+
const descriptionNode = getDescriptionNode(schema);
186+
if (descriptionNode) {
187+
(schemaNode as any).description = descriptionNode;
188+
}
196189

197190
return schemaNode;
198191
}
@@ -204,14 +197,7 @@ export function astFromDirective(
204197
): DirectiveDefinitionNode {
205198
return {
206199
kind: Kind.DIRECTIVE_DEFINITION,
207-
description:
208-
directive.astNode?.description ??
209-
(directive.description
210-
? {
211-
kind: Kind.STRING,
212-
value: directive.description,
213-
}
214-
: undefined),
200+
description: getDescriptionNode(directive),
215201
name: {
216202
kind: Kind.NAME,
217203
value: directive.name,
@@ -311,15 +297,7 @@ export function astFromArg(
311297
): InputValueDefinitionNode {
312298
return {
313299
kind: Kind.INPUT_VALUE_DEFINITION,
314-
description:
315-
arg.astNode?.description ??
316-
(arg.description
317-
? {
318-
kind: Kind.STRING,
319-
value: arg.description,
320-
block: true,
321-
}
322-
: undefined),
300+
description: getDescriptionNode(arg),
323301
name: {
324302
kind: Kind.NAME,
325303
value: arg.name,
@@ -341,15 +319,7 @@ export function astFromObjectType(
341319
): ObjectTypeDefinitionNode {
342320
return {
343321
kind: Kind.OBJECT_TYPE_DEFINITION,
344-
description:
345-
type.astNode?.description ??
346-
(type.description
347-
? {
348-
kind: Kind.STRING,
349-
value: type.description,
350-
block: true,
351-
}
352-
: undefined),
322+
description: getDescriptionNode(type),
353323
name: {
354324
kind: Kind.NAME,
355325
value: type.name,
@@ -371,15 +341,7 @@ export function astFromInterfaceType(
371341
): InterfaceTypeDefinitionNode {
372342
const node: InterfaceTypeDefinitionNode = {
373343
kind: Kind.INTERFACE_TYPE_DEFINITION,
374-
description:
375-
type.astNode?.description ??
376-
(type.description
377-
? {
378-
kind: Kind.STRING,
379-
value: type.description,
380-
block: true,
381-
}
382-
: undefined),
344+
description: getDescriptionNode(type),
383345
name: {
384346
kind: Kind.NAME,
385347
value: type.name,
@@ -406,15 +368,7 @@ export function astFromUnionType(
406368
): UnionTypeDefinitionNode {
407369
return {
408370
kind: Kind.UNION_TYPE_DEFINITION,
409-
description:
410-
type.astNode?.description ??
411-
(type.description
412-
? {
413-
kind: Kind.STRING,
414-
value: type.description,
415-
block: true,
416-
}
417-
: undefined),
371+
description: getDescriptionNode(type),
418372
name: {
419373
kind: Kind.NAME,
420374
value: type.name,
@@ -432,15 +386,7 @@ export function astFromInputObjectType(
432386
): InputObjectTypeDefinitionNode {
433387
return {
434388
kind: Kind.INPUT_OBJECT_TYPE_DEFINITION,
435-
description:
436-
type.astNode?.description ??
437-
(type.description
438-
? {
439-
kind: Kind.STRING,
440-
value: type.description,
441-
block: true,
442-
}
443-
: undefined),
389+
description: getDescriptionNode(type),
444390
name: {
445391
kind: Kind.NAME,
446392
value: type.name,
@@ -460,15 +406,7 @@ export function astFromEnumType(
460406
): EnumTypeDefinitionNode {
461407
return {
462408
kind: Kind.ENUM_TYPE_DEFINITION,
463-
description:
464-
type.astNode?.description ??
465-
(type.description
466-
? {
467-
kind: Kind.STRING,
468-
value: type.description,
469-
block: true,
470-
}
471-
: undefined),
409+
description: getDescriptionNode(type),
472410
name: {
473411
kind: Kind.NAME,
474412
value: type.name,
@@ -506,15 +444,7 @@ export function astFromScalarType(
506444

507445
return {
508446
kind: Kind.SCALAR_TYPE_DEFINITION,
509-
description:
510-
type.astNode?.description ??
511-
(type.description
512-
? {
513-
kind: Kind.STRING,
514-
value: type.description,
515-
block: true,
516-
}
517-
: undefined),
447+
description: getDescriptionNode(type),
518448
name: {
519449
kind: Kind.NAME,
520450
value: type.name,
@@ -531,15 +461,7 @@ export function astFromField(
531461
): FieldDefinitionNode {
532462
return {
533463
kind: Kind.FIELD_DEFINITION,
534-
description:
535-
field.astNode?.description ??
536-
(field.description
537-
? {
538-
kind: Kind.STRING,
539-
value: field.description,
540-
block: true,
541-
}
542-
: undefined),
464+
description: getDescriptionNode(field),
543465
name: {
544466
kind: Kind.NAME,
545467
value: field.name,
@@ -558,15 +480,7 @@ export function astFromInputField(
558480
): InputValueDefinitionNode {
559481
return {
560482
kind: Kind.INPUT_VALUE_DEFINITION,
561-
description:
562-
field.astNode?.description ??
563-
(field.description
564-
? {
565-
kind: Kind.STRING,
566-
value: field.description,
567-
block: true,
568-
}
569-
: undefined),
483+
description: getDescriptionNode(field),
570484
name: {
571485
kind: Kind.NAME,
572486
value: field.name,
@@ -585,15 +499,7 @@ export function astFromEnumValue(
585499
): EnumValueDefinitionNode {
586500
return {
587501
kind: Kind.ENUM_VALUE_DEFINITION,
588-
description:
589-
value.astNode?.description ??
590-
(value.description
591-
? {
592-
kind: Kind.STRING,
593-
value: value.description,
594-
block: true,
595-
}
596-
: undefined),
502+
description: getDescriptionNode(value),
597503
name: {
598504
kind: Kind.NAME,
599505
value: value.name,

0 commit comments

Comments
 (0)