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

Gatsby 3: graphql query during createPages breaks allSitePage:filter: #30107

Closed
CodeAndWeb opened this issue Mar 8, 2021 · 7 comments · Fixed by #30132
Closed

Gatsby 3: graphql query during createPages breaks allSitePage:filter: #30107

CodeAndWeb opened this issue Mar 8, 2021 · 7 comments · Fixed by #30132
Labels
topic: GraphQL Related to Gatsby's GraphQL layer type: bug An issue or pull request relating to a bug in Gatsby

Comments

@CodeAndWeb
Copy link

CodeAndWeb commented Mar 8, 2021

Description

I am trying to filter my graphql pages using a filter on allSitePage:filter:context. Everything was ok in Gatsby2 with my migration to Gatsby3 the filter is now gone from the graphql.

The issue only happens when I run a query during createPages. I can restore the original data by commenting-out every await graphql().

Steps to reproduce

I've created a minimum project to demonstrate the issue. It's the starter project, the changed interesting file is the gatsby-node.js.

The stripped down content of createPages looks like this:

exports.createPages = async ({graphql, actions}) => {
    const {createPage} = actions
    createPage({
        path: `/test`,
        component: require.resolve(`./src/templates/dog-template.js`),
        context: {dog:{name:'bla', bread:'blub'}},
    })

    // This query breaks the allSitePage:filter:
    const result = await graphql(`query { allMdx { nodes { slug } } }`)    <---- remove this query and everything is ok
    console.log(JSON.stringify(result, null, 4))
}

git clone https://github.com/CodeAndWeb/gatsby-3-migration-issue.git
yarn install

Everything is ok from the start:

gatsby develop --verbose

Visit http://localhost:8000/___graphql

Edit gatsby-node.js and uncomment the following lines:

const result = await graphql(`
query {
      allMdx {
        nodes {
          slug
        }
      }
}
`)
console.log(JSON.stringify(result, null, 4))

Re-start gatsby and visit http://localhost:8000/___graphql

Expected result

allSitePage(filter: {context: {dog: {}}}) should be available in the graphql database.

Everything else seems to be ok in the graphql database... the nodes have the context fields. It's just the filter that is broken.

Actual result

allSitePage(filter: {context: {dog: {}}}) is gone as soon as I run a query during createPages()

Environment

System:
OS: macOS 11.2.2
CPU: (16) x64 Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
Shell: 5.8 - /bin/zsh
Binaries:
Node: 15.4.0 - /usr/local/bin/node
Yarn: 1.22.10 - /usr/local/bin/yarn
npm: 7.0.15 - /usr/local/bin/npm
Languages:
Python: 2.7.16 - /usr/bin/python
Browsers:
Chrome: 88.0.4324.192
Firefox: 86.0
Safari: 14.0.3
npmPackages:
gatsby: ^3.0.1 => 3.0.3
gatsby-plugin-image: ^1.0.0 => 1.0.0
gatsby-plugin-manifest: ^3.0.0 => 3.0.0
gatsby-plugin-mdx: ^2.0.0 => 2.0.0
gatsby-plugin-postcss: ^4.0.0 => 4.0.0
gatsby-plugin-react-helmet: ^4.0.0 => 4.0.0
gatsby-plugin-sharp: ^3.0.0 => 3.0.0
gatsby-plugin-sitemap: ^3.0.0 => 3.0.0
gatsby-source-filesystem: ^3.0.0 => 3.0.0
gatsby-transformer-sharp: ^3.0.0 => 3.0.0

@CodeAndWeb CodeAndWeb added the type: bug An issue or pull request relating to a bug in Gatsby label Mar 8, 2021
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Mar 8, 2021
@CodeAndWeb CodeAndWeb changed the title Gatsby 3: graphql query during createPages breaks allSitePage:filter:context Gatsby 3: graphql query during createPages breaks allSitePage:filter: Mar 8, 2021
@CodeAndWeb
Copy link
Author

I've made some more experiments to see if something with my async handling is wrong.

  1. I've added a simple promise instead of the query to see if I am running into async issues:

    console.log("BEFORE SLEEP");
    await new Promise(r => setTimeout(r, 2000));
    console.log("AFTER SLEEP")

The filters are present and the console output also looks good in my opinion:

success building schema - 0.252s
verbose Transition to "initializingData" > "creatingPages"
BEFORE SLEEP
AFTER SLEEP
info Total nodes: 45, SitePage nodes: 3 (use --verbose for breakdown)
verbose Number of node types: 7. Nodes per type: SitePage: 3, SitePlugin: 34, Site: 1, SiteBuildMetadata: 1, Directory: 2, File: 3,
ImageSharp: 1
success createPages - 2.016s
verbose Checking for deleted pages
verbose Deleted 0 pages

I've done a similar thing with the query:

console.log("BEFORE QUERY");
const result = await graphql(`
  query {
       allMdx {
        nodes {
          slug
        }
      }
  }
`)
console.log("QUERY RESULT:");
console.log(JSON.stringify(result, null, 4))

The log:

success Checking for changed pages - 0.003s
success source and transform nodes - 0.046s
verbose Transition to "initializingData" > "buildingSchema"
success building schema - 0.248s
verbose Transition to "initializingData" > "creatingPages"
BEFORE QUERY
QUERY RESULT:
{
    "data": {
        "allMdx": {
            "nodes": []
        }
    }
}
info Total nodes: 45, SitePage nodes: 3 (use --verbose for breakdown)
verbose Number of node types: 7. Nodes per type: SitePage: 3, SitePlugin: 34, Site: 1, SiteBuildMetadata: 1, Directory: 2, File: 3,
ImageSharp: 1
success createPages - 0.039s
verbose Checking for deleted pages
verbose Deleted 0 pages
verbose Found 2 changed pages

The result filter broken. :-(

@vladar vladar added topic: GraphQL Related to Gatsby's GraphQL layer and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Mar 9, 2021
@vladar
Copy link
Contributor

vladar commented Mar 9, 2021

Thank you so much for the repro 💜 It should be fixed via #30132. I'll post an update here when the fix is published.

@ahmadkhalaf1
Copy link

@vladar
Hi , i kinda have similar issue , when i do filter to context it say : context is not defined by type SitePageFilterInput . would this be also fixed with the new update?

@CodeAndWeb
Copy link
Author

@ahmadkhalaf1 That's what happens when you run a page query with the broken filters. I would assume that this is the same issue.

@CodeAndWeb
Copy link
Author

@valdar Thanks for the fast fix.

@vladar
Copy link
Contributor

vladar commented Mar 9, 2021

Published in [email protected]

@ahmadkhalaf1
Copy link

fixed my issue thanks ^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: GraphQL Related to Gatsby's GraphQL layer type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants