Skip to content

Commit

Permalink
Fix stitchSchemas for when empty array of typeDefs are passed (#1575)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rufman committed Jun 2, 2020
1 parent b493948 commit e08738d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
- Fix visitEnumValue to allow modifying the enum value <br/>
[@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

Expand Down
2 changes: 1 addition & 1 deletion packages/stitch/src/stitchSchemas.ts
Expand Up @@ -51,7 +51,7 @@ export function stitchSchemas({
}

let schemaLikeObjects: Array<GraphQLSchema | SubschemaConfig | DocumentNode | GraphQLNamedType> = [...subschemas];
if (typeDefs) {
if ((typeDefs && !Array.isArray(typeDefs)) || (Array.isArray(typeDefs) && typeDefs.length)) {
schemaLikeObjects.push(buildDocumentFromTypeDefinitions(typeDefs, parseOptions));
}
if (types != null) {
Expand Down
30 changes: 30 additions & 0 deletions packages/stitch/tests/stitchSchemas.test.ts
Expand Up @@ -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({
Expand Down

0 comments on commit e08738d

Please sign in to comment.