Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
fix(stitching): fix lists of enum and custom scalars, closes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Jul 3, 2019
1 parent 458602d commit 0d173b9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
19 changes: 11 additions & 8 deletions src/stitching/checkResultAndHandleErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
isScalarType,
ExecutionResult,
GraphQLError,
GraphQLType,
} from 'graphql';
import { getResponseKeyFromInfo } from './getResponseKeyFromInfo';
import {
Expand Down Expand Up @@ -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;
}
24 changes: 23 additions & 1 deletion src/test/testMergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ let scalarTest = `
type Query {
testingScalar(input: TestScalar): TestingScalar
listTestingScalar(input: TestScalar): [TestingScalar]
}
`;

Expand All @@ -94,6 +95,11 @@ scalarSchema = makeExecutableSchema({
value: args.input[0] === '_' ? args.input : null
};
},
listTestingScalar(parent, args) {
return [{
value: args.input[0] === '_' ? args.input : null
}];
},
},
},
});
Expand Down Expand Up @@ -131,6 +137,7 @@ let enumTest = `
type Query {
color(input: Color): Color
numericEnum: NumericEnum
listNumericEnum: [NumericEnum]
wrappedEnum: EnumWrapper
}
`;
Expand All @@ -153,6 +160,9 @@ enumSchema = makeExecutableSchema({
numericEnum() {
return 1;
},
listNumericEnum() {
return [1];
},
wrappedEnum() {
return {
color: '#EA3232',
Expand Down Expand Up @@ -573,6 +583,9 @@ testCombinations.forEach(async combination => {
testingScalar(input: "test") {
value
}
listTestingScalar(input: "test") {
value
}
}
`,
);
Expand All @@ -584,6 +597,9 @@ testCombinations.forEach(async combination => {
testingScalar(input: "test") {
value
}
listTestingScalar(input: "test") {
value
}
}
`,
);
Expand All @@ -592,7 +608,10 @@ testCombinations.forEach(async combination => {
data: {
testingScalar: {
value: 'test'
}
},
listTestingScalar: [{
value: 'test'
}]
},
});
expect(mergedResult).to.deep.equal(scalarResult);
Expand All @@ -605,6 +624,7 @@ testCombinations.forEach(async combination => {
query {
color(input: RED)
numericEnum
listNumericEnum
numericEnumInfo: __type(name: "NumericEnum") {
enumValues(includeDeprecated: true) {
name
Expand Down Expand Up @@ -633,6 +653,7 @@ testCombinations.forEach(async combination => {
query {
color(input: RED)
numericEnum
listNumericEnum
numericEnumInfo: __type(name: "NumericEnum") {
enumValues(includeDeprecated: true) {
name
Expand All @@ -659,6 +680,7 @@ testCombinations.forEach(async combination => {
data: {
color: 'RED',
numericEnum: 'TEST',
listNumericEnum: ['TEST'],
numericEnumInfo: {
enumValues: [
{
Expand Down

0 comments on commit 0d173b9

Please sign in to comment.