Extending @ehrencrona' answer. It's completely right that the main purpose to use a staticQuery rather than a standard common query is to fetch static data and because of this, they don't accept values plus other limitations/differences such as:
- Page queries can accept variables (via
pageContext) but can only be added to page components
StaticQuery does not accept variables (hence the name "static"), but can be used in any component, including pages
StaticQuery does not work with raw React.createElement calls; please use JSX, e.g. <StaticQuery />
Reference: https://www.gatsbyjs.org/docs/static-query/
However, they have a huge benefit rather than a page query due to the appearance of React Hooks (since v16.8) and it's the use of useStaticQuery hook. Despite having (almost) the same limitations as the staticQuery they have these benefits.
- Much more cleaner
- Much more readable
- Much more atomized
From:
<StaticQuery
query={graphql`
query {
site {
siteMetadata {
title
}
}
}
`}
render={data => (
<header>
<h1>{data.site.siteMetadata.title}</h1>
</header>
)}
/>
To:
const Header = () => {
const { title } = useSiteMetadata()
return (
<header>
<h1>{title}</h1>
</header>
)
}
Reference: https://www.gatsbyjs.org/blog/2019-02-20-introducing-use-static-query/
More information in Gatby's documentation about useStaticQuery hook.
For the answer, you should use a "hardcoded" value there:
const data = useStaticQuery(graphql`
query MyQuery {
mongodbItems(id: {eq: "5"}){
id
}
}
`)