Skip to content

Commit 24a5038

Browse files
robzhuleebyron
authored andcommitted
Throw TypeError when coercing an array into a GraphQLString (#925)
* Throw TypeError when coercing an array into a GraphQLString * Add coerceString to GraphQLString serialize flow
1 parent 85bfd13 commit 24a5038

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

src/execution/__tests__/variables-test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import { expect } from 'chai';
1414
import { describe, it } from 'mocha';
1515
import { execute } from '../execute';
16+
import { coerceValue } from '../values';
1617
import { parse } from '../../language';
1718
import {
1819
GraphQLSchema,
@@ -592,6 +593,56 @@ describe('Execute: Handles inputs', () => {
592593
});
593594
});
594595

596+
it('reports error for array passed into string input', async () => {
597+
const doc = `
598+
query SetsNonNullable($value: String!) {
599+
fieldWithNonNullableStringInput(input: $value)
600+
}
601+
`;
602+
const ast = parse(doc);
603+
const variables = {value: [ 1, 2, 3 ]};
604+
605+
expect(
606+
await execute(schema, ast, null, null, variables)
607+
).to.deep.equal({
608+
errors: [ {
609+
message:
610+
'Variable "$value" got invalid value [1,2,3].\nExpected type ' +
611+
'"String", found [1,2,3]: String cannot represent an array value: [1,2,3]',
612+
locations: [ { line: 2, column: 31 } ],
613+
path: undefined,
614+
} ]
615+
});
616+
});
617+
618+
it('coercing an array to GraphQLString throws TypeError', async () => {
619+
let caughtError;
620+
try {
621+
coerceValue(GraphQLString, [ 1, 2, 3 ]);
622+
} catch (error) {
623+
caughtError = error;
624+
}
625+
626+
expect(caughtError instanceof TypeError).to.equal(true);
627+
expect(caughtError && caughtError.message).to.equal(
628+
'String cannot represent an array value: [1,2,3]'
629+
);
630+
});
631+
632+
it('serializing an array via GraphQLString throws TypeError', async () => {
633+
let caughtError;
634+
try {
635+
GraphQLString.serialize([ 1, 2, 3 ]);
636+
} catch (error) {
637+
caughtError = error;
638+
}
639+
640+
expect(caughtError instanceof TypeError).to.equal(true);
641+
expect(caughtError && caughtError.message).to.equal(
642+
'String cannot represent an array value: [1,2,3]'
643+
);
644+
});
645+
595646
it('reports error for non-provided variables for non-nullable inputs', async () => {
596647
// Note: this test would typically fail validation before encountering
597648
// this execution error, however for queries which previously validated

src/execution/values.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export function getDirectiveValues(
190190
/**
191191
* Given a type and any value, return a runtime value coerced to match the type.
192192
*/
193-
function coerceValue(type: GraphQLInputType, value: mixed): mixed {
193+
export function coerceValue(type: GraphQLInputType, value: mixed): mixed {
194194
// Ensure flow knows that we treat function params as const.
195195
const _value = value;
196196

src/type/scalars.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,23 @@ export const GraphQLFloat = new GraphQLScalarType({
8888
}
8989
});
9090

91+
function coerceString(value: mixed): ?string {
92+
if (Array.isArray(value)) {
93+
throw new TypeError(
94+
`String cannot represent an array value: [${String(value)}]`
95+
);
96+
}
97+
return String(value);
98+
}
99+
91100
export const GraphQLString = new GraphQLScalarType({
92101
name: 'String',
93102
description:
94103
'The `String` scalar type represents textual data, represented as UTF-8 ' +
95104
'character sequences. The String type is most often used by GraphQL to ' +
96105
'represent free-form human-readable text.',
97-
serialize: String,
98-
parseValue: String,
106+
serialize: coerceString,
107+
parseValue: coerceString,
99108
parseLiteral(ast) {
100109
return ast.kind === Kind.STRING ? ast.value : null;
101110
}

0 commit comments

Comments
 (0)