From e08738db8835b276191871e19dbdad9867ff8b20 Mon Sep 17 00:00:00 2001 From: Stephane Rufer Date: Tue, 2 Jun 2020 04:33:31 -0700 Subject: [PATCH] Fix stitchSchemas for when empty array of typeDefs are passed (#1575) If an empty array is passed, the GraphQL parser fails, because the source is empty. Switch the typeDef check to check for length as well, since an empty array is truthy. --- CHANGELOG.md | 1 + packages/stitch/src/stitchSchemas.ts | 2 +- packages/stitch/tests/stitchSchemas.test.ts | 30 +++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d5af23f822..7698f45bd12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Fix visitEnumValue to allow modifying the enum value
[@danielrearden](https://github.com/danielrearden) in [#1003](https://github.com/ardatan/graphql-tools/pull/1391) - Export `generateProxyingResolvers` from `@graphql-tools/wrap`. +- Fix `stitchSchemas` from `@graphql-tools/stitch` from the case there the typeDefs array is empty. [#1575](https://github.com/ardatan/graphql-tools/pull/1575) ### 5.0.0 diff --git a/packages/stitch/src/stitchSchemas.ts b/packages/stitch/src/stitchSchemas.ts index 27ad05f31a3..da547adc9b1 100644 --- a/packages/stitch/src/stitchSchemas.ts +++ b/packages/stitch/src/stitchSchemas.ts @@ -51,7 +51,7 @@ export function stitchSchemas({ } let schemaLikeObjects: Array = [...subschemas]; - if (typeDefs) { + if ((typeDefs && !Array.isArray(typeDefs)) || (Array.isArray(typeDefs) && typeDefs.length)) { schemaLikeObjects.push(buildDocumentFromTypeDefinitions(typeDefs, parseOptions)); } if (types != null) { diff --git a/packages/stitch/tests/stitchSchemas.test.ts b/packages/stitch/tests/stitchSchemas.test.ts index 65e53038d2d..928682c3c3f 100644 --- a/packages/stitch/tests/stitchSchemas.test.ts +++ b/packages/stitch/tests/stitchSchemas.test.ts @@ -2982,6 +2982,36 @@ fragment BookingFragment on Booking { }); }); + describe('empty typeDefs array', () => { + test('works', async () => { + const typeDefs = ` + type Query { + book: Book + } + type Book { + category: String! + } + `; + let schema = makeExecutableSchema({ typeDefs }); + + const resolvers = { + Query: { + book: () => ({ category: 'Test' }), + }, + }; + + schema = stitchSchemas({ + schemas: [schema], + resolvers, + typeDefs: [], + }); + + const result = await graphql(schema, '{ book { cat: category } }'); + + expect(result.data.book.cat).toBe('Test'); + }); + }); + describe('new root type name', () => { test('works', async () => { let bookSchema = makeExecutableSchema({