Skip to content

Commit f1b829c

Browse files
authored
Merge pull request #44 from indigotech/bugfix/multiple-queries
Bugfix - Just one @query was added to resulting schema
2 parents fdbeb24 + 7b4044c commit f1b829c

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Added contributing guidelines - #30 - [@askmon](https://github.com/askmon)
99
- Fixed @Field arguments handling - #42 - [@felipesabino](https://github.com/felipesabino)
1010
- Fixed @UnionType usage with abstract classes - #43 - [@felipesabino](https://github.com/felipesabino)
11+
- Fixed error that just one @Query was added to resulting schema - #44 - [@felipesabino](https://github.com/felipesabino)
1112

1213
### Breaking changes
1314

src/specs/functional.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,36 @@ describe('Functional', function () {
272272
});
273273

274274
});
275+
276+
describe('Multiple Queries', function () {
277+
278+
@D.ObjectType()
279+
class QueryAType {
280+
@D.Field()
281+
greetingA(): string { return `Hello, world`; }
282+
}
283+
284+
@D.ObjectType()
285+
class QueryBType {
286+
@D.Field()
287+
greetingB(): string { return `Hello, world`; }
288+
}
289+
290+
@D.Schema()
291+
class SchemaType {
292+
@D.Query() queryA: QueryAType;
293+
@D.Query() queryB: QueryBType;
294+
}
295+
296+
it('resolves multiple queries with @Schema and @Query', async function () {
297+
const schema = schemaFactory(SchemaType);
298+
const result = await graphql.graphql(schema, `query { greetingA, greetingB } `);
299+
assert(result.data.greetingA === 'Hello, world');
300+
assert(result.data.greetingB === 'Hello, world');
301+
});
302+
303+
});
304+
275305
});
276306

277307
describe('Mutation', function () {

src/type-factory/schema.type-factory.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,17 @@ function getEntryObject(
3939
throw new SchemaFactoryError(`Target should have @${type} field`, SchemaFactoryErrorType.NO_QUERY_FIELD);
4040
}
4141

42-
return metadatas.map(metadata => {
42+
const fieldMap = metadatas.map(metadata => {
4343
const fieldTarget = Reflect.getMetadata('design:type', metadata.target, metadata.property) as Function;
4444
const fieldMetadatas = getMetadataBuilder().buildFieldMetadata(fieldTarget.prototype);
4545
return fieldMetadatas.reduce((fields, fieldMetadata) => {
4646
fields[fieldMetadata.name] = fieldTypeFactory(fieldTarget, fieldMetadata, undefined, metadata.isSubscription);
4747
return fields;
4848
}, {} as { [key: string]: any });
4949
})
50-
.map(fields => objectTypeFactory(fields))
51-
.find((value, index) => index === 0);
50+
.reduce((map, fields) => ({ ...map, ...fields }), {});
51+
52+
return Object.keys(fieldMap).length > 0 ? objectTypeFactory(fieldMap) : undefined;
5253

5354
}
5455

0 commit comments

Comments
 (0)