Skip to content

Commit

Permalink
fix(utils): print comments as blocks correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Dec 25, 2023
1 parent 36a81a5 commit 5ae0394
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 110 deletions.
5 changes: 5 additions & 0 deletions .changeset/three-houses-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/utils': patch
---

Print comments as blocks
24 changes: 24 additions & 0 deletions packages/utils/src/descriptionFromObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Kind, type StringValueNode } from 'graphql';

interface ObjectWithDescription {
astNode?: {
description?: StringValueNode | null;
} | null;
description?: string | null;
}

export function getDescriptionNode(obj: ObjectWithDescription): StringValueNode | undefined {
if (obj.astNode?.description) {
return {
...obj.astNode.description,
block: true,
};
}
if (obj.description) {
return {
kind: Kind.STRING,
value: obj.description,
block: true,
};
}
}
126 changes: 16 additions & 110 deletions packages/utils/src/print-schema-with-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ import {
ScalarTypeDefinitionNode,
SchemaDefinitionNode,
SchemaExtensionNode,
StringValueNode,
TypeDefinitionNode,
TypeExtensionNode,
UnionTypeDefinitionNode,
} from 'graphql';
import { astFromType } from './astFromType.js';
import { astFromValue } from './astFromValue.js';
import { astFromValueUntyped } from './astFromValueUntyped.js';
import { getDescriptionNode } from './descriptionFromObject.js';
import { getDirectivesInExtensions } from './get-directives.js';
import { isSome } from './helpers.js';
import { getRootTypeMap } from './rootTypes.js';
Expand Down Expand Up @@ -182,17 +182,10 @@ export function astFromSchema(
directives: directives as any,
};

// This code is so weird because it needs to support GraphQL.js 14
// In GraphQL.js 14 there is no `description` value on schemaNode
(schemaNode as unknown as { description?: StringValueNode }).description =
(schema.astNode as unknown as { description: string })?.description ??
(schema as unknown as { description: string }).description != null
? {
kind: Kind.STRING,
value: (schema as unknown as { description: string }).description,
block: true,
}
: undefined;
const descriptionNode = getDescriptionNode(schema);
if (descriptionNode) {
(schemaNode as any).description = descriptionNode;
}

return schemaNode;
}
Expand All @@ -204,14 +197,7 @@ export function astFromDirective(
): DirectiveDefinitionNode {
return {
kind: Kind.DIRECTIVE_DEFINITION,
description:
directive.astNode?.description ??
(directive.description
? {
kind: Kind.STRING,
value: directive.description,
}
: undefined),
description: getDescriptionNode(directive),
name: {
kind: Kind.NAME,
value: directive.name,
Expand Down Expand Up @@ -311,15 +297,7 @@ export function astFromArg(
): InputValueDefinitionNode {
return {
kind: Kind.INPUT_VALUE_DEFINITION,
description:
arg.astNode?.description ??
(arg.description
? {
kind: Kind.STRING,
value: arg.description,
block: true,
}
: undefined),
description: getDescriptionNode(arg),
name: {
kind: Kind.NAME,
value: arg.name,
Expand All @@ -341,15 +319,7 @@ export function astFromObjectType(
): ObjectTypeDefinitionNode {
return {
kind: Kind.OBJECT_TYPE_DEFINITION,
description:
type.astNode?.description ??
(type.description
? {
kind: Kind.STRING,
value: type.description,
block: true,
}
: undefined),
description: getDescriptionNode(type),
name: {
kind: Kind.NAME,
value: type.name,
Expand All @@ -371,15 +341,7 @@ export function astFromInterfaceType(
): InterfaceTypeDefinitionNode {
const node: InterfaceTypeDefinitionNode = {
kind: Kind.INTERFACE_TYPE_DEFINITION,
description:
type.astNode?.description ??
(type.description
? {
kind: Kind.STRING,
value: type.description,
block: true,
}
: undefined),
description: getDescriptionNode(type),
name: {
kind: Kind.NAME,
value: type.name,
Expand All @@ -406,15 +368,7 @@ export function astFromUnionType(
): UnionTypeDefinitionNode {
return {
kind: Kind.UNION_TYPE_DEFINITION,
description:
type.astNode?.description ??
(type.description
? {
kind: Kind.STRING,
value: type.description,
block: true,
}
: undefined),
description: getDescriptionNode(type),
name: {
kind: Kind.NAME,
value: type.name,
Expand All @@ -432,15 +386,7 @@ export function astFromInputObjectType(
): InputObjectTypeDefinitionNode {
return {
kind: Kind.INPUT_OBJECT_TYPE_DEFINITION,
description:
type.astNode?.description ??
(type.description
? {
kind: Kind.STRING,
value: type.description,
block: true,
}
: undefined),
description: getDescriptionNode(type),
name: {
kind: Kind.NAME,
value: type.name,
Expand All @@ -460,15 +406,7 @@ export function astFromEnumType(
): EnumTypeDefinitionNode {
return {
kind: Kind.ENUM_TYPE_DEFINITION,
description:
type.astNode?.description ??
(type.description
? {
kind: Kind.STRING,
value: type.description,
block: true,
}
: undefined),
description: getDescriptionNode(type),
name: {
kind: Kind.NAME,
value: type.name,
Expand Down Expand Up @@ -506,15 +444,7 @@ export function astFromScalarType(

return {
kind: Kind.SCALAR_TYPE_DEFINITION,
description:
type.astNode?.description ??
(type.description
? {
kind: Kind.STRING,
value: type.description,
block: true,
}
: undefined),
description: getDescriptionNode(type),
name: {
kind: Kind.NAME,
value: type.name,
Expand All @@ -531,15 +461,7 @@ export function astFromField(
): FieldDefinitionNode {
return {
kind: Kind.FIELD_DEFINITION,
description:
field.astNode?.description ??
(field.description
? {
kind: Kind.STRING,
value: field.description,
block: true,
}
: undefined),
description: getDescriptionNode(field),
name: {
kind: Kind.NAME,
value: field.name,
Expand All @@ -558,15 +480,7 @@ export function astFromInputField(
): InputValueDefinitionNode {
return {
kind: Kind.INPUT_VALUE_DEFINITION,
description:
field.astNode?.description ??
(field.description
? {
kind: Kind.STRING,
value: field.description,
block: true,
}
: undefined),
description: getDescriptionNode(field),
name: {
kind: Kind.NAME,
value: field.name,
Expand All @@ -585,15 +499,7 @@ export function astFromEnumValue(
): EnumValueDefinitionNode {
return {
kind: Kind.ENUM_VALUE_DEFINITION,
description:
value.astNode?.description ??
(value.description
? {
kind: Kind.STRING,
value: value.description,
block: true,
}
: undefined),
description: getDescriptionNode(value),
name: {
kind: Kind.NAME,
value: value.name,
Expand Down

0 comments on commit 5ae0394

Please sign in to comment.