Skip to content
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

[1.0] WIP fix infer graphql input type #908

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions packages/gatsby/lib/schema/__tests__/infer-graphql-input-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ describe(`GraphQL Input args`, () => {
title: `The world of dash and adventure`,
blue: 100,
},
anObjectArray: [
{ aString: `some string`, aNumber: 2, aBoolean: true },
{ aString: `some string`, aNumber: 2, anArray: [1, 2] },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the idea that these are the same type but each is missing a different field?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I do think properties that hold an array should be able to be empty or not, but for starters I think it would be ok if objects always have to have the same keys. And if the data doesn't some out of the CMS like that it's still easy to fix in client code.

],
},
{
name: `The Mad Wax`,
Expand Down Expand Up @@ -258,4 +262,31 @@ describe(`GraphQL Input args`, () => {
expect(result.data.allNode.anArray[0].totalCount).toEqual(2)
})
.catch(err => expect(err).not.toBeDefined()))

it(`can query object arrays`, () =>
graphql(
schema,
`
{
allNode {
anObjectArray {
aString
aNumber
aBoolean
}
}
}
`
)
.then(result => {
expect(result.errors).not.toBeDefined()

expect(result.data.allNode.anObjectArray).toHaveLength(2)
expect(result.data.allNode.anObjectArray[0].aString)
.toEqual(`some string`)
expect(result.data.allNode.anObjectArray[0].aNumber).toEqual(2)
expect(result.data.allNode.anObjectArray[0].aBoolean).toEqual(true)
expect(result.data.allNode.anObjectArray[1].anArray).toEqual([1, 2])
})
.catch(err => expect(err).not.toBeDefined()))
})
12 changes: 10 additions & 2 deletions packages/gatsby/lib/schema/infer-graphql-input-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
GraphQLList,
GraphQLEnumType,
GraphQLNonNull,
GraphQLObjectType,
} = require(`graphql`)
const _ = require(`lodash`)
const moment = require(`moment`)
Expand Down Expand Up @@ -54,10 +55,11 @@ const inferGraphQLInputFields = (exports.inferGraphQLInputFields = (
) => {
switch (typeOf(value)) {
case `array`:
let headType = typeOf(value[0])
const headValue = value[0]
let headType = typeOf(headValue)
// Check if headType is a number.
if (headType === `number`) {
if (value[0] % 1 === 0) {
if (headValue % 1 === 0) {
headType = `int`
} else {
headType = `float`
Expand All @@ -79,6 +81,12 @@ const inferGraphQLInputFields = (exports.inferGraphQLInputFields = (
case `boolean`:
inType = GraphQLBoolean
break
case `object`:
inType = inferGraphQLInputFields(headValue, key, nodes).type
break
case `array`:
inType = inferGraphQLInputFields(headValue, key, nodes).type
break
}

return {
Expand Down