diff --git a/packages/gatsby/src/schema/__tests__/data-tree-utils-test.js b/packages/gatsby/src/schema/__tests__/data-tree-utils-test.js index 849c3cb2f35ff..005c79506984a 100644 --- a/packages/gatsby/src/schema/__tests__/data-tree-utils-test.js +++ b/packages/gatsby/src/schema/__tests__/data-tree-utils-test.js @@ -176,34 +176,50 @@ describe(`Gatsby data tree utils`, () => { it(`prefers float when multiple number types`, () => { let example - // nodes starting with integer + // nodes starting with 32-bit integer ("big" ints are float) example = getExampleValues({ nodes: [{ number: 5 }, { number: 2.5 }] }) expect(example.number).toBeDefined() expect(example.number).toEqual(2.5) + example = getExampleValues({ nodes: [{ number: 5 }, { number: 3000000000 }] }) + expect(example.number).toBeDefined() + expect(example.number).toEqual(3000000000) // with node not containing number field example = getExampleValues({ nodes: [{ number: 5 }, {}, { number: 2.5 }] }) expect(example.number).toBeDefined() expect(example.number).toEqual(2.5) - // nodes starting with float + // nodes starting with float ("big" ints are float) example = getExampleValues({ nodes: [{ number: 2.5 }, { number: 5 }] }) expect(example.number).toBeDefined() expect(example.number).toEqual(2.5) + example = getExampleValues({ nodes: [{ number: 3000000000 }, { number: 5 }] }) + expect(example.number).toBeDefined() + expect(example.number).toEqual(3000000000) - // array of numbers - starting with integer + // array of numbers - starting with float example = getExampleValues({ nodes: [{ numbers: [2.5, 5] }] }) expect(example.numbers).toBeDefined() expect(Array.isArray(example.numbers)).toBe(true) expect(example.numbers.length).toBe(1) expect(example.numbers[0]).toBe(2.5) + example = getExampleValues({ nodes: [{ numbers: [3000000000, 5] }] }) + expect(example.numbers).toBeDefined() + expect(Array.isArray(example.numbers)).toBe(true) + expect(example.numbers.length).toBe(1) + expect(example.numbers[0]).toBe(3000000000) - // array of numbers - starting with float + // array of numbers - starting with 32-bit integer example = getExampleValues({ nodes: [{ numbers: [5, 2.5] }] }) expect(example.numbers).toBeDefined() expect(Array.isArray(example.numbers)).toBe(true) expect(example.numbers.length).toBe(1) expect(example.numbers[0]).toBe(2.5) + example = getExampleValues({ nodes: [{ numbers: [5, 3000000000] }] }) + expect(example.numbers).toBeDefined() + expect(Array.isArray(example.numbers)).toBe(true) + expect(example.numbers.length).toBe(1) + expect(example.numbers[0]).toBe(3000000000) }) it(`handles mix of date strings and date objects`, () => { diff --git a/packages/gatsby/src/schema/__tests__/infer-graphql-input-type-test.js b/packages/gatsby/src/schema/__tests__/infer-graphql-input-type-test.js index f8aa7642bba17..b5d4769d95247 100644 --- a/packages/gatsby/src/schema/__tests__/infer-graphql-input-type-test.js +++ b/packages/gatsby/src/schema/__tests__/infer-graphql-input-type-test.js @@ -266,6 +266,21 @@ describe(`GraphQL Input args`, () => { expect(Object.keys(fields.foo.type.getFields())).toHaveLength(2) }) + it(`infers number types`, () => { + const fields = inferInputObjectStructureFromNodes({ + nodes: [ + { + int32: 42, + float: 2.5, + longint: 3000000000, + }, + ], + }).inferredFields + expect(fields.int32.type.name.endsWith(`Integer`)).toBe(true) + expect(fields.float.type.name.endsWith(`Float`)).toBe(true) + expect(fields.longint.type.name.endsWith(`Float`)).toBe(true) + }) + it(`handles eq operator`, async () => { let result = await queryResult( nodes, diff --git a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js index 7bfd595442544..397ee888ccd3a 100644 --- a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js +++ b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js @@ -124,6 +124,20 @@ describe(`GraphQL type inferance`, () => { ) }) + it(`doesn't throw errors at ints longer than 32-bit`, async () => { + const result = await queryResult( + [ + { + longint: 3000000000, + }, + ], + ` + longint + ` + ) + expect(result.errors).toBeUndefined() + }) + it(`prefers float when multiple number types`, async () => { let result = await queryResult( [{ number: 1.1 }, { number: 1 }], @@ -199,6 +213,21 @@ describe(`GraphQL type inferance`, () => { expect(Object.keys(fields.foo.type.getFields())).toHaveLength(4) }) + it(`infers number types`, () => { + const fields = inferObjectStructureFromNodes({ + nodes: [ + { + int32: 42, + float: 2.5, + longint: 3000000000, + }, + ], + }) + expect(fields.int32.type.name).toEqual(`Int`) + expect(fields.float.type.name).toEqual(`Float`) + expect(fields.longint.type.name).toEqual(`Float`) + }) + it(`Handle invalid graphql field names`, async () => { let result = await queryResult( nodes, diff --git a/packages/gatsby/src/utils/__tests__/is-32-bit-integer.js b/packages/gatsby/src/utils/__tests__/is-32-bit-integer.js new file mode 100644 index 0000000000000..c040eda6f915e --- /dev/null +++ b/packages/gatsby/src/utils/__tests__/is-32-bit-integer.js @@ -0,0 +1,21 @@ +const is32BitInteger = require(`../is-32-bit-integer.js`) + +const MAX_INT = 2147483647 +const MIN_INT = -2147483648 + +describe(`is32BitInteger`, () => { + it(`works with all kind of values`, () => { + expect(is32BitInteger(MAX_INT)).toBe(true) + expect(is32BitInteger(MIN_INT)).toBe(true) + expect(is32BitInteger(MAX_INT + 1)).toBe(false) + expect(is32BitInteger(MIN_INT - 1)).toBe(false) + expect(is32BitInteger(2.4)).toBe(false) + expect(is32BitInteger(`42`)).toBe(false) + expect(is32BitInteger({})).toBe(false) + expect(is32BitInteger([1])).toBe(false) + expect(is32BitInteger(true)).toBe(false) + expect(is32BitInteger(false)).toBe(false) + expect(is32BitInteger(undefined)).toBe(false) + expect(is32BitInteger(null)).toBe(false) + }) +})