Skip to content

Commit

Permalink
fix(gatsby): Local file lookups in eq: $id queries (#34699) (#34717)
Browse files Browse the repository at this point in the history
Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
Co-authored-by: Lennart <lekoarts@gmail.com>
  • Loading branch information
3 people committed Feb 4, 2022
1 parent f9478d9 commit 7c2f404
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
12 changes: 12 additions & 0 deletions packages/gatsby/src/schema/__tests__/fixtures/queries.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
const os = require(`os`)
const path = require(`path`)

const dir = os.platform() === "win32" ? "C:/Users/test/site" : "/home/test/site"

const nodes = [
{
id: `file1`,
Expand All @@ -8,6 +13,8 @@ const nodes = [
contentDigest: `file1`,
},
name: `1.md`,
dir,
absolutePath: path.posix.join(dir, `1.md`)
},
{
id: `file2`,
Expand All @@ -18,6 +25,8 @@ const nodes = [
contentDigest: `file2`,
},
name: `2.md`,
dir,
absolutePath: path.posix.join(dir, `2.md`)
},
{
id: `file3`,
Expand All @@ -28,6 +37,8 @@ const nodes = [
contentDigest: `file3`,
},
name: `authors.yaml`,
dir,
absolutePath: path.posix.join(dir, `authors.yaml`)
},
{
id: `md1`,
Expand All @@ -46,6 +57,7 @@ const nodes = [
authors: [`author2@example.com`, `author1@example.com`],
reviewer___NODE: `author2`,
reviewerByEmail: `author2@example.com`,
fileRef: `2.md`
},
},
{
Expand Down
34 changes: 33 additions & 1 deletion packages/gatsby/src/schema/__tests__/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ describe(`Query schema`, () => {

const typeDefs = [
`type Markdown implements Node { frontmatter: Frontmatter! }`,
`type Frontmatter { authors: [Author] }`,
`type Frontmatter { authors: [Author], fileRef: File @fileByRelativePath }`,
`type Author implements Node { posts: [Markdown] }`,
]
typeDefs.forEach(def =>
Expand Down Expand Up @@ -2108,6 +2108,38 @@ describe(`Query schema`, () => {
}
})

it(`@fileByRelativePath works`, async () => {
const query = `
{
markdown(id: { eq: "md1"}) {
frontmatter {
title
fileRef {
childMarkdown {
frontmatter {
title
}
}
}
}
}
}
`
const results = await runQuery(query)

expect(results?.errors).toBeUndefined()
expect(results?.data?.markdown?.frontmatter?.title).toEqual(
`Markdown File 1`
)

// main assertion - markdown.frontmatter.fileRef is a file referenced by local path
// we want to make sure it finds node correctly (and doesn't crash)
expect(
results?.data?.markdown?.frontmatter?.fileRef?.childMarkdown
?.frontmatter?.title
).toEqual(`Markdown File 2`)
})

describe(`doesn't try to use fast path if there are more or different filters than just id.eq`, () => {
it(`using single filter different than id.eq`, async () => {
const results = await runQuery(
Expand Down
13 changes: 4 additions & 9 deletions packages/gatsby/src/schema/node-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,10 @@ class LocalNodeModel {
})
runQueryActivity.start()
}
let nodeFoundById = getDataStore().getNode(query.filter.id.eq)

// make sure our node is of compatible type
if (
nodeFoundById &&
!nodeTypeNames.includes(nodeFoundById.internal.type)
) {
nodeFoundById = null
}
const nodeFoundById = this.getNodeById({
id: query.filter.id.eq,
type: gqlType,
})

if (runQueryActivity) {
runQueryActivity.end()
Expand Down

0 comments on commit 7c2f404

Please sign in to comment.