Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions src/type/__tests__/scalars-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,14 @@ describe('Type System: Specified scalar types', () => {
expect(() => serialize(-1e100)).to.throw(
'Int cannot represent non 32-bit signed integer value: -1e+100',
);
expect(() => serialize('one')).to.throw(
'Int cannot represent non-integer value: "one"',
);

// Doesn't represent number
expect(() => serialize('')).to.throw(
'Int cannot represent non-integer value: ""',
);
expect(() => serialize(NaN)).to.throw(
'Int cannot represent non-integer value: NaN',
);

expect(serialize(NaN)).to.equal(null);

expect(() => serialize(Infinity)).to.throw(
'Int cannot represent non-integer value: Infinity',
);
Expand Down Expand Up @@ -293,18 +290,11 @@ describe('Type System: Specified scalar types', () => {
};
expect(serialize(customValueOfObj)).to.equal(5.5);

expect(() => serialize(NaN)).to.throw(
'Float cannot represent non numeric value: NaN',
);
expect(serialize(NaN)).to.equal(null);

expect(() => serialize(Infinity)).to.throw(
'Float cannot represent non numeric value: Infinity',
);
expect(() => serialize('one')).to.throw(
'Float cannot represent non numeric value: "one"',
);
expect(() => serialize('')).to.throw(
'Float cannot represent non numeric value: ""',
);
expect(() => serialize([5])).to.throw(
'Float cannot represent non numeric value: [5]',
);
Expand Down
12 changes: 10 additions & 2 deletions src/type/scalars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const GRAPHQL_MAX_INT = 2147483647;
* */
export const GRAPHQL_MIN_INT = -2147483648;

export const GraphQLInt = new GraphQLScalarType<number>({
export const GraphQLInt = new GraphQLScalarType<number | null>({
name: 'Int',
description:
'The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.',
Expand All @@ -38,6 +38,10 @@ export const GraphQLInt = new GraphQLScalarType<number>({
num = Number(coercedValue);
}

if (typeof num === 'number' && Number.isNaN(num)) {
return null;
}

if (typeof num !== 'number' || !Number.isInteger(num)) {
throw new GraphQLError(
`Int cannot represent non-integer value: ${inspect(coercedValue)}`,
Expand Down Expand Up @@ -84,7 +88,7 @@ export const GraphQLInt = new GraphQLScalarType<number>({
},
});

export const GraphQLFloat = new GraphQLScalarType<number>({
export const GraphQLFloat = new GraphQLScalarType<number | null>({
name: 'Float',
description:
'The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).',
Expand All @@ -101,6 +105,10 @@ export const GraphQLFloat = new GraphQLScalarType<number>({
num = Number(coercedValue);
}

if (typeof num === 'number' && Number.isNaN(num)) {
return null;
}

if (typeof num !== 'number' || !Number.isFinite(num)) {
throw new GraphQLError(
`Float cannot represent non numeric value: ${inspect(coercedValue)}`,
Expand Down
4 changes: 0 additions & 4 deletions src/utilities/__tests__/astFromValue-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ describe('astFromValue', () => {
expect(() => astFromValue(1e40, GraphQLInt)).to.throw(
'Int cannot represent non 32-bit signed integer value: 1e+40',
);

expect(() => astFromValue(NaN, GraphQLInt)).to.throw(
'Int cannot represent non-integer value: NaN',
);
});

it('converts Float values to Int/Float ASTs', () => {
Expand Down
Loading