-
Notifications
You must be signed in to change notification settings - Fork 2k
astFromValue - JavaScript BigInt support #4088
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export function isInteger(value: unknown): value is number | bigint { | ||
const valueTypeOf = typeof value; | ||
if (valueTypeOf === 'number') { | ||
return Number.isInteger(value); | ||
} | ||
return valueTypeOf === 'bigint'; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export function isNumeric(value: unknown): value is number | bigint { | ||
const valueTypeOf = typeof value; | ||
if (valueTypeOf === 'number') { | ||
return Number.isFinite(value); | ||
} | ||
return valueTypeOf === 'bigint'; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,16 @@ describe('astFromValue', () => { | |
value: true, | ||
}); | ||
|
||
expect(astFromValue(0n, GraphQLBoolean)).to.deep.equal({ | ||
kind: 'BooleanValue', | ||
value: false, | ||
}); | ||
|
||
expect(astFromValue(1n, GraphQLBoolean)).to.deep.equal({ | ||
kind: 'BooleanValue', | ||
value: true, | ||
}); | ||
|
||
const NonNullBoolean = new GraphQLNonNull(GraphQLBoolean); | ||
expect(astFromValue(0, NonNullBoolean)).to.deep.equal({ | ||
kind: 'BooleanValue', | ||
|
@@ -69,6 +79,11 @@ describe('astFromValue', () => { | |
value: '10000', | ||
}); | ||
|
||
expect(astFromValue(1n, GraphQLInt)).to.deep.equal({ | ||
kind: 'IntValue', | ||
value: '1', | ||
}); | ||
|
||
// GraphQL spec does not allow coercing non-integer values to Int to avoid | ||
// accidental data loss. | ||
expect(() => astFromValue(123.5, GraphQLInt)).to.throw( | ||
|
@@ -80,6 +95,16 @@ describe('astFromValue', () => { | |
'Int cannot represent non 32-bit signed integer value: 1e+40', | ||
); | ||
|
||
// Note: outside the bounds of 32bit signed int. | ||
expect(() => astFromValue(9007199254740991, GraphQLInt)).to.throw( | ||
'Int cannot represent non 32-bit signed integer value: 9007199254740991', | ||
); | ||
|
||
// Note: outside the bounds of 32bit signed int as BigInt. | ||
expect(() => astFromValue(9007199254740991n, GraphQLInt)).to.throw( | ||
'Int cannot represent non 32-bit signed integer value: 9007199254740991', | ||
); | ||
|
||
expect(() => astFromValue(NaN, GraphQLInt)).to.throw( | ||
'Int cannot represent non-integer value: NaN', | ||
); | ||
|
@@ -96,6 +121,11 @@ describe('astFromValue', () => { | |
value: '123', | ||
}); | ||
|
||
expect(astFromValue(9007199254740993n, GraphQLFloat)).to.deep.equal({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The change in type of this test is because astFromValue is not safe, set to be replaced by But a bigint of sufficient size, -- as far as I know -- cannot be converted to a JS float. We also in this PR do not properly handle GraphQLFloat scalar parsing and serialization of small BigInts, I will push a failing test to demonstrate what I mean. We also probably should update the logic within the new |
||
kind: 'IntValue', | ||
value: '9007199254740993', | ||
}); | ||
|
||
expect(astFromValue(123.5, GraphQLFloat)).to.deep.equal({ | ||
kind: 'FloatValue', | ||
value: '123.5', | ||
|
@@ -133,6 +163,11 @@ describe('astFromValue', () => { | |
value: '123', | ||
}); | ||
|
||
expect(astFromValue(9007199254740993n, GraphQLString)).to.deep.equal({ | ||
kind: 'StringValue', | ||
value: '9007199254740993', | ||
}); | ||
|
||
expect(astFromValue(false, GraphQLString)).to.deep.equal({ | ||
kind: 'StringValue', | ||
value: 'false', | ||
|
@@ -183,6 +218,11 @@ describe('astFromValue', () => { | |
value: '01', | ||
}); | ||
|
||
expect(astFromValue(9007199254740993n, GraphQLID)).to.deep.equal({ | ||
kind: 'IntValue', | ||
value: '9007199254740993', | ||
}); | ||
|
||
expect(() => astFromValue(false, GraphQLID)).to.throw( | ||
'ID cannot represent value: false', | ||
); | ||
|
Uh oh!
There was an error while loading. Please reload this page.