From 0d173b91c79d3236ba6e53bf20db753bed2c02b0 Mon Sep 17 00:00:00 2001 From: yaacovCR Date: Tue, 2 Jul 2019 22:55:12 -0400 Subject: [PATCH] fix(stitching): fix lists of enum and custom scalars, closes #9 --- src/stitching/checkResultAndHandleErrors.ts | 19 +++++++++------- src/test/testMergeSchemas.ts | 24 ++++++++++++++++++++- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/stitching/checkResultAndHandleErrors.ts b/src/stitching/checkResultAndHandleErrors.ts index cd9fca996f1..cc409dfa703 100644 --- a/src/stitching/checkResultAndHandleErrors.ts +++ b/src/stitching/checkResultAndHandleErrors.ts @@ -8,6 +8,7 @@ import { isScalarType, ExecutionResult, GraphQLError, + GraphQLType, } from 'graphql'; import { getResponseKeyFromInfo } from './getResponseKeyFromInfo'; import { @@ -61,14 +62,16 @@ export function handleResult( if (isObjectType(nullableType) || isListType(nullableType)) { annotateWithChildrenErrors(resultObject, errors); - } else if (isEnumType(nullableType)) { - const value = nullableType.getValue(resultObject); - if (value) { - return value.value; - } - } else if (isScalarType(nullableType)) { - return nullableType.parseValue(resultObject); } - return resultObject; + return parseOutputValue(nullableType, resultObject); +} + +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)) { + return type.parseValue(value); + } + return value; } diff --git a/src/test/testMergeSchemas.ts b/src/test/testMergeSchemas.ts index 4343d6c9c8c..1c3f7f85097 100644 --- a/src/test/testMergeSchemas.ts +++ b/src/test/testMergeSchemas.ts @@ -73,6 +73,7 @@ let scalarTest = ` type Query { testingScalar(input: TestScalar): TestingScalar + listTestingScalar(input: TestScalar): [TestingScalar] } `; @@ -94,6 +95,11 @@ scalarSchema = makeExecutableSchema({ value: args.input[0] === '_' ? args.input : null }; }, + listTestingScalar(parent, args) { + return [{ + value: args.input[0] === '_' ? args.input : null + }]; + }, }, }, }); @@ -131,6 +137,7 @@ let enumTest = ` type Query { color(input: Color): Color numericEnum: NumericEnum + listNumericEnum: [NumericEnum] wrappedEnum: EnumWrapper } `; @@ -153,6 +160,9 @@ enumSchema = makeExecutableSchema({ numericEnum() { return 1; }, + listNumericEnum() { + return [1]; + }, wrappedEnum() { return { color: '#EA3232', @@ -573,6 +583,9 @@ testCombinations.forEach(async combination => { testingScalar(input: "test") { value } + listTestingScalar(input: "test") { + value + } } `, ); @@ -584,6 +597,9 @@ testCombinations.forEach(async combination => { testingScalar(input: "test") { value } + listTestingScalar(input: "test") { + value + } } `, ); @@ -592,7 +608,10 @@ testCombinations.forEach(async combination => { data: { testingScalar: { value: 'test' - } + }, + listTestingScalar: [{ + value: 'test' + }] }, }); expect(mergedResult).to.deep.equal(scalarResult); @@ -605,6 +624,7 @@ testCombinations.forEach(async combination => { query { color(input: RED) numericEnum + listNumericEnum numericEnumInfo: __type(name: "NumericEnum") { enumValues(includeDeprecated: true) { name @@ -633,6 +653,7 @@ testCombinations.forEach(async combination => { query { color(input: RED) numericEnum + listNumericEnum numericEnumInfo: __type(name: "NumericEnum") { enumValues(includeDeprecated: true) { name @@ -659,6 +680,7 @@ testCombinations.forEach(async combination => { data: { color: 'RED', numericEnum: 'TEST', + listNumericEnum: ['TEST'], numericEnumInfo: { enumValues: [ {