From 6b0c467cd6308185a35d8b59a9e1ea8cb03454b0 Mon Sep 17 00:00:00 2001 From: Ahmed Hassan Date: Fri, 6 Jan 2017 17:42:43 +0100 Subject: [PATCH 1/2] use function reference instead of string for concatenateTypeDefs --- src/schemaGenerator.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/schemaGenerator.ts b/src/schemaGenerator.ts index 8dbb4e5629b..dc8f720ab9c 100644 --- a/src/schemaGenerator.ts +++ b/src/schemaGenerator.ts @@ -110,16 +110,17 @@ function makeExecutableSchema({ return jsSchema; } -function concatenateTypeDefs(typeDefinitionsAry: ITypedef[], functionsCalled = {}): string { +function concatenateTypeDefs(typeDefinitionsAry: ITypedef[], calledFunctionRefs = [] as any ): string { let resolvedTypeDefinitions: string[] = []; typeDefinitionsAry.forEach((typeDef: ITypedef) => { if (typeof typeDef === 'function') { - if (!(typeDef as any in functionsCalled)) { - functionsCalled[typeDef as any] = 1; + if (calledFunctionRefs.indexOf(typeDef) === -1) { + calledFunctionRefs.push(typeDef); resolvedTypeDefinitions = resolvedTypeDefinitions.concat( - concatenateTypeDefs(typeDef(), functionsCalled) + concatenateTypeDefs(typeDef(), calledFunctionRefs) ); } + } else if (typeof typeDef === 'string') { resolvedTypeDefinitions.push(typeDef.trim()); } else { From 2fc31c7c8f7e679349194c235d7b85ea6722c81b Mon Sep 17 00:00:00 2001 From: Ahmed Hassan Date: Sat, 14 Jan 2017 15:49:24 +0100 Subject: [PATCH 2/2] add test for concatenateTypeDefs via refs --- src/schemaGenerator.ts | 1 + src/test/testSchemaGenerator.ts | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/schemaGenerator.ts b/src/schemaGenerator.ts index dc8f720ab9c..fc8e94f8ffe 100644 --- a/src/schemaGenerator.ts +++ b/src/schemaGenerator.ts @@ -517,4 +517,5 @@ export { buildSchemaFromTypeDefinitions, addSchemaLevelResolveFunction, attachConnectorsToContext, + concatenateTypeDefs, }; diff --git a/src/test/testSchemaGenerator.ts b/src/test/testSchemaGenerator.ts index a2adc1d174b..968bed119ee 100644 --- a/src/test/testSchemaGenerator.ts +++ b/src/test/testSchemaGenerator.ts @@ -24,6 +24,7 @@ import { attachConnectorsToContext, assertResolveFunctionsPresent, chainResolvers, + concatenateTypeDefs, } from '../schemaGenerator'; import { IResolverValidationOptions, IResolvers } from '../Interfaces'; import 'mocha'; @@ -286,6 +287,30 @@ describe('generating schema from shorthand', () => { expect(jsSchema.getQueryType().getFields()).to.have.all.keys('foo', 'bar'); }); + it('can concatenateTypeDefs created by a function inside a closure', () => { + const typeA = { typeDefs: () => ['type TypeA { foo: String }'] }; + const typeB = { typeDefs: () => ['type TypeB { bar: String }'] }; + const typeC = { typeDefs: () => ['type TypeC { foo: String }'] }; + const typeD = { typeDefs: () => ['type TypeD { bar: String }'] }; + + function combineTypeDefs(...args: Array): any { + return { typeDefs: () => args.map(o => o.typeDefs) }; + } + + const combinedAandB = combineTypeDefs(typeA, typeB); + const combinedCandD = combineTypeDefs(typeC, typeD); + + const result = concatenateTypeDefs([ + combinedAandB.typeDefs, + combinedCandD.typeDefs + ]); + + expect(result).to.contain('type TypeA'); + expect(result).to.contain('type TypeB'); + expect(result).to.contain('type TypeC'); + expect(result).to.contain('type TypeD'); + }); + it('properly deduplicates the array of type DefinitionNodes', () => { const typeDefAry = [` type Query {