Skip to content

Commit

Permalink
chore(gatsby): Remove unnecessary checks (#17289)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprobst authored and GatsbyJS Bot committed Sep 1, 2019
1 parent 31c9cfa commit a88cb2c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 51 deletions.
25 changes: 0 additions & 25 deletions packages/gatsby/src/schema/__tests__/node-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,6 @@ describe(`NodeModel`, () => {
nodeModel.getNodeById({ id: `person4` }, { path: `/` })
expect(createPageDependency).not.toHaveBeenCalled()
})

it(`handles already resolved id`, () => {
const result = nodeModel.getNodeById({
id: { id: `person1`, name: `Person1` },
})
expect(result.id).toBe(`person1`)
expect(result.name).toBe(`Person1`)
})
})

describe(`getNodesByIds`, () => {
Expand Down Expand Up @@ -203,23 +195,6 @@ describe(`NodeModel`, () => {
nodeModel.getNodesByIds({ ids: [`person4`, `post4`] }, { path: `/` })
expect(createPageDependency).not.toHaveBeenCalled()
})

it(`handles already resolved ids`, () => {
const result = nodeModel.getNodesByIds({
ids: [
{ id: `person1`, name: `Person1` },
`person2`,
{ id: `post1`, frontmatter: { published: false } },
],
})
expect(result.length).toBe(3)
expect(result[0].id).toBe(`person1`)
expect(result[0].name).toBe(`Person1`)
expect(result[1].id).toBe(`person2`)
expect(result[1].name).toBe(`Person2`)
expect(result[2].id).toBe(`post1`)
expect(result[2].frontmatter.published).toBe(false)
})
})

describe(`getAllNodes`, () => {
Expand Down
16 changes: 4 additions & 12 deletions packages/gatsby/src/schema/node-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const {
const invariant = require(`invariant`)
const reporter = require(`gatsby-cli/lib/reporter`)

type IDOrNode = string | { id: string }
type TypeOrTypeName = string | GraphQLOutputType

/**
Expand All @@ -36,11 +35,11 @@ interface QueryArguments {

export interface NodeModel {
getNodeById(
{ id: IDOrNode, type?: TypeOrTypeName },
{ id: string, type?: TypeOrTypeName },
pageDependencies?: PageDependencies
): any | null;
getNodesByIds(
{ ids: Array<IDOrNode>, type?: TypeOrTypeName },
{ ids: Array<string>, type?: TypeOrTypeName },
pageDependencies?: PageDependencies
): Array<any>;
getAllNodes(
Expand Down Expand Up @@ -458,15 +457,8 @@ class ContextualNodeModel {
}
}

const getNodeById = (nodeStore, id) => {
// This is for cases when the `id` has already been resolved
// to a full Node for the input filter, and is also in the selection
// set. E.g. `{ foo(parent: { id: { eq: 1 } } ) { parent { id } } }`.
if (_.isPlainObject(id) && id.id) {
return id
}
return id != null ? nodeStore.getNode(id) : null
}
const getNodeById = (nodeStore, id) =>
id != null ? nodeStore.getNode(id) : null

const toNodeTypeNames = (schema, gqlTypeName) => {
const gqlType =
Expand Down
16 changes: 2 additions & 14 deletions packages/gatsby/src/schema/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,7 @@ const link = (options = {}, fieldConfig) => async (
fromNode: options.from ? options.fromNode : info.fromNode,
})

if (fieldValue == null || _.isPlainObject(fieldValue)) return fieldValue
if (
Array.isArray(fieldValue) &&
(fieldValue[0] == null || _.isPlainObject(fieldValue[0]))
) {
return fieldValue
}
if (fieldValue == null) return null

const returnType = getNullableType(options.type || info.returnType)
const type = getNamedType(returnType)
Expand Down Expand Up @@ -194,13 +188,7 @@ const fileByPath = (options = {}, fieldConfig) => async (
fromNode: options.from ? options.fromNode : info.fromNode,
})

if (fieldValue == null || _.isPlainObject(fieldValue)) return fieldValue
if (
Array.isArray(fieldValue) &&
(fieldValue[0] == null || _.isPlainObject(fieldValue[0]))
) {
return fieldValue
}
if (fieldValue == null) return null

const findLinkedFileNode = relativePath => {
// Use the parent File node to create the absolute path to
Expand Down

0 comments on commit a88cb2c

Please sign in to comment.