Skip to content

Commit

Permalink
Merge pull request #252 from nerdmed/master
Browse files Browse the repository at this point in the history
use function reference instead of string for concatenateTypeDefs
  • Loading branch information
helfer authored Jan 22, 2017
2 parents 534b934 + cea1872 commit 36d3d1c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/schemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -516,4 +517,5 @@ export {
buildSchemaFromTypeDefinitions,
addSchemaLevelResolveFunction,
attachConnectorsToContext,
concatenateTypeDefs,
};
25 changes: 25 additions & 0 deletions src/test/testSchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
attachConnectorsToContext,
assertResolveFunctionsPresent,
chainResolvers,
concatenateTypeDefs,
} from '../schemaGenerator';
import { IResolverValidationOptions, IResolvers } from '../Interfaces';
import 'mocha';
Expand Down Expand Up @@ -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>): 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 {
Expand Down

0 comments on commit 36d3d1c

Please sign in to comment.