From 5ae039445b07fd11dea1f2b5a6a4154ad4f2a6ab Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Mon, 25 Dec 2023 15:54:51 +0300 Subject: [PATCH] fix(utils): print comments as blocks correctly --- .changeset/three-houses-cry.md | 5 + packages/utils/src/descriptionFromObject.ts | 24 ++++ .../utils/src/print-schema-with-directives.ts | 126 +++--------------- 3 files changed, 45 insertions(+), 110 deletions(-) create mode 100644 .changeset/three-houses-cry.md create mode 100644 packages/utils/src/descriptionFromObject.ts diff --git a/.changeset/three-houses-cry.md b/.changeset/three-houses-cry.md new file mode 100644 index 00000000000..b96fda2007a --- /dev/null +++ b/.changeset/three-houses-cry.md @@ -0,0 +1,5 @@ +--- +'@graphql-tools/utils': patch +--- + +Print comments as blocks diff --git a/packages/utils/src/descriptionFromObject.ts b/packages/utils/src/descriptionFromObject.ts new file mode 100644 index 00000000000..d2b77c64e96 --- /dev/null +++ b/packages/utils/src/descriptionFromObject.ts @@ -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, + }; + } +} diff --git a/packages/utils/src/print-schema-with-directives.ts b/packages/utils/src/print-schema-with-directives.ts index 23224e2857d..995ea84eac3 100644 --- a/packages/utils/src/print-schema-with-directives.ts +++ b/packages/utils/src/print-schema-with-directives.ts @@ -42,7 +42,6 @@ import { ScalarTypeDefinitionNode, SchemaDefinitionNode, SchemaExtensionNode, - StringValueNode, TypeDefinitionNode, TypeExtensionNode, UnionTypeDefinitionNode, @@ -50,6 +49,7 @@ import { 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'; @@ -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; } @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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,