Skip to content

Commit

Permalink
Ignore empty arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleAMathews committed Apr 27, 2017
1 parent 349c60c commit d8cdf9b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
6 changes: 6 additions & 0 deletions packages/gatsby/lib/schema/__tests__/data-tree-utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe(`Gatsby data tree utils`, () => {
name: `The Mad Max`,
hair: 1,
date: `2006-07-22T22:39:53.000Z`,
emptyArray: [],
anArray: [1, 2, 3, 4],
frontmatter: {
date: `2006-07-22T22:39:53.000Z`,
Expand Down Expand Up @@ -68,6 +69,11 @@ describe(`Gatsby data tree utils`, () => {
expect(extractFieldExamples({ nodes }).iAmNull).not.toBeDefined()
})

it(`ignores empty arrays`, () => {
expect(extractFieldExamples({ nodes }).emptyArray).not.toBeDefined()
expect(extractFieldExamples({ nodes }).hair).toBeDefined()
})

it(`build enum values for fields from array on nodes`, () => {
expect(buildFieldEnumValues(nodes)).toMatchSnapshot()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,6 @@ test(`Infers graphql type from array of nodes`, () => {
},
],
},
}))
})
)
})
6 changes: 4 additions & 2 deletions packages/gatsby/lib/schema/data-tree-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ const extractFieldExamples = (exports.extractFieldExamples = ({
let examples = nodes.reduce((mem, node) => {
let subNode = selector ? _.get(node, selector) : node

// Ignore undefined/null subnodes
subNode = _.omitBy(flatten(subNode || {}, { safe: true }), _.isNil)
// Ignore undefined/null/empty array subnodes
subNode = _.omitBy(flatten(subNode || {}, { safe: true }), v => {
return _.isNil(v) || (_.isArray(v) && _.isEmpty(v))
})

return Object.assign({}, mem, subNode)
}, {})
Expand Down
19 changes: 3 additions & 16 deletions packages/gatsby/lib/schema/infer-graphql-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const mime = require("mime")
const isRelative = require("is-relative-url")
const { store, getNodes } = require("../redux")
const { addPageDependency } = require("../redux/actions/add-page-dependency")
const { extractFieldExamples } = require("./data-tree-utils")

const inferGraphQLType = ({ value, fieldName, ...otherArgs }) => {
if (Array.isArray(value)) {
Expand Down Expand Up @@ -111,24 +112,10 @@ const inferObjectStructureFromNodes = (exports.inferObjectStructureFromNodes = (
types,
allNodes,
}) => {
const type = nodes[0].type
const fieldExamples = {}
_.each(nodes, node => {
let subNode
if (selector) {
subNode = _.get(node, selector)
} else {
subNode = node
}
_.each(subNode, (v, k) => {
if (!fieldExamples[k]) {
fieldExamples[k] = v
}
})
})
const fieldExamples = extractFieldExamples({ nodes, selector })

// Remove fields common to the top-level of all nodes. We add these
// elsewhere so don't need to infer there type.
// elsewhere so don't need to infer their type.
if (!selector) {
delete fieldExamples.type
delete fieldExamples.id
Expand Down

0 comments on commit d8cdf9b

Please sign in to comment.