From 474540cfb97f98203d16ae853f1b3915fdcb9ae8 Mon Sep 17 00:00:00 2001 From: yaacovCR Date: Fri, 19 Jul 2019 01:04:06 -0400 Subject: [PATCH] fix(stitching): support stitching unions of types with enums Closes #13. --- src/stitching/checkResultAndHandleErrors.ts | 9 +++---- src/test/testMergeSchemas.ts | 28 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/stitching/checkResultAndHandleErrors.ts b/src/stitching/checkResultAndHandleErrors.ts index f4ecf4f5e65..f472bc1dda5 100644 --- a/src/stitching/checkResultAndHandleErrors.ts +++ b/src/stitching/checkResultAndHandleErrors.ts @@ -2,10 +2,9 @@ import { GraphQLResolveInfo, responsePathAsArray, getNullableType, - isObjectType, + isCompositeType, + isLeafType, isListType, - isEnumType, - isScalarType, ExecutionResult, GraphQLError, GraphQLType, @@ -60,7 +59,7 @@ export function handleResult( const nullableType = getNullableType(info.returnType); - if (isObjectType(nullableType) || isListType(nullableType)) { + if (isCompositeType(nullableType) || isListType(nullableType)) { annotateWithChildrenErrors(result, errors); } @@ -70,7 +69,7 @@ export function handleResult( function parseOutputValue(type: GraphQLType, value: any) { if (isListType(type)) { return value.map((v: any) => parseOutputValue(getNullableType(type.ofType), v)); - } else if (isEnumType(type) || isScalarType(type)) { + } else if (isLeafType(type)) { return type.parseValue(value); } return value; diff --git a/src/test/testMergeSchemas.ts b/src/test/testMergeSchemas.ts index 1c3f7f85097..d8c247415ca 100644 --- a/src/test/testMergeSchemas.ts +++ b/src/test/testMergeSchemas.ts @@ -130,6 +130,8 @@ let enumTest = ` numericEnum: NumericEnum } + union UnionWithEnum = EnumWrapper + schema { query: Query } @@ -139,6 +141,7 @@ let enumTest = ` numericEnum: NumericEnum listNumericEnum: [NumericEnum] wrappedEnum: EnumWrapper + unionWithEnum: UnionWithEnum } `; @@ -153,6 +156,9 @@ enumSchema = makeExecutableSchema({ NumericEnum: { TEST: 1, }, + UnionWithEnum: { + __resolveType: () => 'EnumWrapper' + }, Query: { color(parent, args) { return args.input === '#EA3232' ? args.input : null; @@ -169,6 +175,12 @@ enumSchema = makeExecutableSchema({ numericEnum: 1 }; }, + unionWithEnum() { + return { + color: '#EA3232', + numericEnum: 1 + }; + }, }, }, }); @@ -643,6 +655,12 @@ testCombinations.forEach(async combination => { color numericEnum } + unionWithEnum { + ... on EnumWrapper { + color + numericEnum + } + } } `, ); @@ -672,6 +690,12 @@ testCombinations.forEach(async combination => { color numericEnum } + unionWithEnum { + ... on EnumWrapper { + color + numericEnum + } + } } `, ); @@ -703,6 +727,10 @@ testCombinations.forEach(async combination => { color: 'RED', numericEnum: 'TEST', }, + unionWithEnum: { + color: 'RED', + numericEnum: 'TEST', + }, }, }); expect(mergedResult).to.deep.equal(enumResult);