From be6fdb8817a2644763895e6ad45b36b4102b26d5 Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Tue, 31 Aug 2021 17:39:47 +0300 Subject: [PATCH] fix(utils): bring back breaking change for fixSchemaAst --- .changeset/wild-owls-drop.md | 5 +++++ packages/utils/src/fixSchemaAst.ts | 26 ++++++++++++++++++++++++++ packages/utils/src/index.ts | 1 + 3 files changed, 32 insertions(+) create mode 100644 .changeset/wild-owls-drop.md create mode 100644 packages/utils/src/fixSchemaAst.ts diff --git a/.changeset/wild-owls-drop.md b/.changeset/wild-owls-drop.md new file mode 100644 index 00000000000..4fe21f6fc18 --- /dev/null +++ b/.changeset/wild-owls-drop.md @@ -0,0 +1,5 @@ +--- +'@graphql-tools/utils': patch +--- + +fix(utils): bring back breaking change for fixSchemaAst diff --git a/packages/utils/src/fixSchemaAst.ts b/packages/utils/src/fixSchemaAst.ts new file mode 100644 index 00000000000..acf7739a848 --- /dev/null +++ b/packages/utils/src/fixSchemaAst.ts @@ -0,0 +1,26 @@ +import { GraphQLSchema, BuildSchemaOptions, buildASTSchema } from 'graphql'; +import { SchemaPrintOptions } from './types'; +import { getDocumentNodeFromSchema } from './print-schema-with-directives'; + +function buildFixedSchema(schema: GraphQLSchema, options: BuildSchemaOptions & SchemaPrintOptions) { + const document = getDocumentNodeFromSchema(schema); + return buildASTSchema(document, { + ...(options || {}), + }); +} + +export function fixSchemaAst(schema: GraphQLSchema, options: BuildSchemaOptions & SchemaPrintOptions) { + // eslint-disable-next-line no-undef-init + let schemaWithValidAst: GraphQLSchema | undefined = undefined; + if (!schema.astNode || !schema.extensionASTNodes) { + schemaWithValidAst = buildFixedSchema(schema, options); + } + + if (!schema.astNode && schemaWithValidAst?.astNode) { + schema.astNode = schemaWithValidAst.astNode; + } + if (!schema.extensionASTNodes && schemaWithValidAst?.astNode) { + schema.extensionASTNodes = schemaWithValidAst.extensionASTNodes; + } + return schema; +} diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 0dac4a5399a..029b032f105 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -46,3 +46,4 @@ export * from './comments'; export * from './collectFields'; export * from './inspect'; export * from './memoize'; +export * from './fixSchemaAst';