From 97cd37760373d61b4785131fd0305c49bb3c5540 Mon Sep 17 00:00:00 2001 From: Kamil Kisiela Date: Mon, 12 Apr 2021 15:33:54 +0000 Subject: [PATCH 1/2] Make replaceExtensions work with inheritTypeDefs --- .../src/testing/test-module.ts | 18 +++--- .../graphql-modules/tests/testing.spec.ts | 63 ++++++++++++++++++- 2 files changed, 71 insertions(+), 10 deletions(-) diff --git a/packages/graphql-modules/src/testing/test-module.ts b/packages/graphql-modules/src/testing/test-module.ts index a3c7393be9..9eed14b220 100644 --- a/packages/graphql-modules/src/testing/test-module.ts +++ b/packages/graphql-modules/src/testing/test-module.ts @@ -76,15 +76,6 @@ export function testModule(testedModule: Module, config?: TestModuleConfig) { function transformModule(mod: Module, config?: TestModuleConfig) { const transforms: ((m: Module) => Module)[] = []; - if (config?.replaceExtensions) { - transforms.push((m) => - moduleFactory({ - ...m.config, - typeDefs: replaceExtensions(m.typeDefs), - }) - ); - } - if (config?.typeDefs) { transforms.push((m) => moduleFactory({ @@ -117,6 +108,15 @@ function transformModule(mod: Module, config?: TestModuleConfig) { }); } + if (config?.replaceExtensions) { + transforms.push((m) => + moduleFactory({ + ...m.config, + typeDefs: replaceExtensions(m.typeDefs), + }) + ); + } + if (transforms) { return transforms.reduce((m, transform) => transform(m), mod); } diff --git a/packages/graphql-modules/tests/testing.spec.ts b/packages/graphql-modules/tests/testing.spec.ts index 3172c8a51b..1e08665867 100644 --- a/packages/graphql-modules/tests/testing.spec.ts +++ b/packages/graphql-modules/tests/testing.spec.ts @@ -1,6 +1,6 @@ import 'reflect-metadata'; import type { TypedDocumentNode } from '@graphql-typed-document-node/core'; -import { concatAST } from 'graphql'; +import { concatAST, printSchema } from 'graphql'; import { createApplication, createModule, @@ -13,6 +13,10 @@ import { Scope, } from '../src'; +function normalizeSDL(sdl: string): string { + return sdl.replace(/\s+/g, ' ').trim(); +} + describe('testModule', () => { test('should replace extensions with definitions on demand', () => { const initialModule = createModule({ @@ -581,4 +585,61 @@ describe('mockApplication', () => { extraEnv: 'mocked', }); }); + + test('replaceExtensions should work with inheritTypeDefs', () => { + const listModule = createModule({ + id: 'list', + typeDefs: gql` + extend type Query { + list: List! + } + + type List { + id: ID! + title: String! + } + `, + }); + + const productModule = createModule({ + id: 'product', + typeDefs: gql` + extend type Query { + product: Product! + } + + type Product { + id: ID! + } + + extend type List { + product: Product! + } + `, + }); + + const app = testkit.testModule(productModule, { + replaceExtensions: true, + inheritTypeDefs: [listModule], + }); + + expect(() => app.schema).not.toThrow(); + expect(normalizeSDL(printSchema(app.schema))).toMatch( + normalizeSDL(` + type Query { + product: Product! + } + + type Product { + id: ID! + } + + type List { + id: ID! + title: String! + product: Product! + } + `) + ); + }); }); From 28be4bb814f0509a989d057ed843f46aeb281a83 Mon Sep 17 00:00:00 2001 From: Kamil Kisiela Date: Mon, 12 Apr 2021 15:35:14 +0000 Subject: [PATCH 2/2] changelog --- .changeset/few-queens-bathe.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/few-queens-bathe.md diff --git a/.changeset/few-queens-bathe.md b/.changeset/few-queens-bathe.md new file mode 100644 index 0000000000..c748210e23 --- /dev/null +++ b/.changeset/few-queens-bathe.md @@ -0,0 +1,5 @@ +--- +'graphql-modules': patch +--- + +Make replaceExtensions work with inheritTypeDefs