Skip to content

Commit

Permalink
Fix handling of integers longer than 32 bits. Treat them as floats.
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelZoerner committed Jun 21, 2018
1 parent 0eb6d43 commit 5eabad8
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/gatsby/src/schema/data-tree-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const invariant = require(`invariant`)
const createKey = require(`./create-key`)
const { typeConflictReporter } = require(`./type-conflict-reporter`)
const DateType = require(`./types/type-date`)
const is32BitInteger = require(`../utils/is-32-bit-integer`)

import type { TypeEntry } from "./type-conflict-reporter"

Expand Down Expand Up @@ -118,7 +119,7 @@ const getExampleScalarFromArray = values =>
values,
(value, nextValue) => {
// Prefer floats over ints as they're more specific.
if (nextValue && _.isNumber(nextValue) && !_.isInteger(nextValue)) {
if (nextValue && _.isNumber(nextValue) && !is32BitInteger(nextValue)) {
return nextValue
} else if (value === null) {
return nextValue
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby/src/schema/infer-graphql-input-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {

const { findLinkedNode } = require(`./infer-graphql-type`)
const { getNodes } = require(`../redux`)
const is32BitInteger = require(`../utils/is-32-bit-integer`)

import type {
GraphQLInputFieldConfig,
Expand Down Expand Up @@ -76,7 +77,7 @@ function inferGraphQLInputFields({
let headType = typeOf(headValue)

if (headType === `number`)
headType = _.isInteger(headValue) ? `int` : `float`
headType = is32BitInteger(headValue) ? `int` : `float`

// Determine type for in operator.
let inType
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby/src/schema/infer-graphql-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const createKey = require(`./create-key`)
const { getExampleValues, isEmptyObjectOrArray } = require(`./data-tree-utils`)
const DateType = require(`./types/type-date`)
const FileType = require(`./types/type-file`)
const is32BitInteger = require(`../utils/is-32-bit-integer`)

import type { GraphQLOutputType } from "graphql"
import type {
Expand Down Expand Up @@ -117,7 +118,7 @@ function inferGraphQLType({
}),
}
case `number`:
return _.isInteger(exampleValue)
return is32BitInteger(exampleValue)
? { type: GraphQLInt }
: { type: GraphQLFloat }
default:
Expand Down
3 changes: 3 additions & 0 deletions packages/gatsby/src/utils/is-32-bit-integer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(x) {
return (x | 0) === x
}

0 comments on commit 5eabad8

Please sign in to comment.