Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby): add useStaticQuery hook (#11588)
- Loading branch information
1 parent
a68769d
commit f149c4c
Showing
37 changed files
with
1,099 additions
and
24 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
e2e-tests/development-runtime/cypress/integration/functionality/static-queries.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
beforeEach(() => { | ||
cy.visit(`/static-query/`).waitForAPIorTimeout(`onRouteUpdate`) | ||
}) | ||
|
||
describe(`StaticQuery behavior`, () => { | ||
it(`works with inline query`, () => { | ||
cy.getTestElement(`inline`) | ||
.invoke(`text`) | ||
.should(`not.contain`, `Loading`) | ||
}) | ||
|
||
it(`works with variable query`, () => { | ||
cy.getTestElement(`variable`) | ||
.invoke(`text`) | ||
.should(`not.contain`, `Loading`) | ||
}) | ||
|
||
it(`works with exported variable query`, () => { | ||
cy.getTestElement(`exported`) | ||
.invoke(`text`) | ||
.should(`not.contain`, `Loading`) | ||
}) | ||
|
||
describe(`useStaticQuery`, () => { | ||
it(`works with inline query`, () => { | ||
cy.getTestElement(`use-static-query-inline`) | ||
.invoke(`text`) | ||
.should(`not.contain`, `Error`) | ||
}) | ||
|
||
it(`works with variable query`, () => { | ||
cy.getTestElement(`use-static-query-variable`) | ||
.invoke(`text`) | ||
.should(`not.contain`, `Error`) | ||
}) | ||
|
||
it(`works with exported variable query`, () => { | ||
cy.getTestElement(`use-static-query-exported`) | ||
.invoke(`text`) | ||
.should(`not.contain`, `Error`) | ||
}) | ||
|
||
it(`works with destructuring`, () => { | ||
cy.getTestElement(`use-static-query-destructuring`) | ||
.find(`li`) | ||
.should(`have.length.gte`, 1) | ||
}) | ||
}) | ||
}) |
41 changes: 41 additions & 0 deletions
41
e2e-tests/development-runtime/cypress/integration/hot-reloading/static-queries.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const author = `@gatsbyjs` | ||
|
||
beforeEach(() => { | ||
cy.visit(`/static-query/`).waitForAPIorTimeout(`onRouteUpdate`) | ||
}) | ||
|
||
describe(`hot-reloading static queries`, () => { | ||
it(`displays placeholder content on launch`, () => { | ||
cy.getTestElement(`hot`) | ||
.invoke(`text`) | ||
.should(`not.contain`, author) | ||
}) | ||
|
||
it(`can update a StaticQuery element`, () => { | ||
cy.exec( | ||
`npm run update -- --file src/components/static-query/hot.js --replacements "# %AUTHOR%:author" --exact` | ||
) | ||
|
||
cy.getTestElement(`hot`) | ||
.invoke(`text`) | ||
.should(`contain`, author) | ||
}) | ||
|
||
describe(`useStaticQuery`, () => { | ||
it(`displays placeholder content on launch`, () => { | ||
cy.getTestElement(`use-static-query-hot`) | ||
.invoke(`text`) | ||
.should(`not.contain`, author) | ||
}) | ||
|
||
it(`can update a useStaticQuery element`, () => { | ||
cy.exec( | ||
`npm run update -- --file src/components/static-query/use-static-query/hot.js --replacements "# %AUTHOR%:author" --exact` | ||
) | ||
|
||
cy.getTestElement(`use-static-query-hot`) | ||
.invoke(`text`) | ||
.should(`contain`, author) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
e2e-tests/development-runtime/src/components/static-query/exported-variable-query.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from "react" | ||
import { StaticQuery, graphql } from "gatsby" | ||
|
||
function ExportedVariable(props) { | ||
return ( | ||
<StaticQuery | ||
query={nameQuery} | ||
render={data => <p {...props}>{data.site.siteMetadata.author.name}</p>} | ||
/> | ||
) | ||
} | ||
|
||
export const nameQuery = graphql` | ||
{ | ||
site { | ||
siteMetadata { | ||
author | ||
} | ||
} | ||
} | ||
` | ||
|
||
export default ExportedVariable |
26 changes: 26 additions & 0 deletions
26
e2e-tests/development-runtime/src/components/static-query/hot.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from "react" | ||
import { StaticQuery, graphql } from "gatsby" | ||
|
||
function SupaHotFire(props) { | ||
return ( | ||
<StaticQuery | ||
query={graphql` | ||
query { | ||
site { | ||
siteMetadata { | ||
# %AUTHOR% | ||
title | ||
} | ||
} | ||
} | ||
`} | ||
render={data => ( | ||
<p {...props}> | ||
{data.site.siteMetadata.title}: {data.site.siteMetadata.author} | ||
</p> | ||
)} | ||
/> | ||
) | ||
} | ||
|
||
export default SupaHotFire |
4 changes: 4 additions & 0 deletions
4
e2e-tests/development-runtime/src/components/static-query/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { default as ExportedVariable } from "./exported-variable-query" | ||
export { default as Inline } from "./inline-query" | ||
export { default as Variable } from "./variable-query" | ||
export { default as Hot } from "./hot" |
19 changes: 19 additions & 0 deletions
19
e2e-tests/development-runtime/src/components/static-query/inline-query.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from "react" | ||
import { StaticQuery, graphql } from "gatsby" | ||
|
||
function InlineQuery(props) { | ||
return ( | ||
<StaticQuery | ||
query={graphql` | ||
query { | ||
sitePage(path: { eq: "/static-query/" }) { | ||
internalComponentName | ||
} | ||
} | ||
`} | ||
render={data => <p {...props}>{data.sitePage.internalComponentName}</p>} | ||
/> | ||
) | ||
} | ||
|
||
export default InlineQuery |
37 changes: 37 additions & 0 deletions
37
e2e-tests/development-runtime/src/components/static-query/use-static-query/destructuring.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from "react" | ||
import { useStaticQuery, graphql } from "gatsby" | ||
|
||
function DestructuringQuery(props) { | ||
let { allSitePlugin } = useStaticQuery(variableQuery) | ||
|
||
if (!allSitePlugin) return `Error` | ||
|
||
const plugins = allSitePlugin.edges | ||
.map(({ node }) => node) | ||
.filter(node => !node.pluginFilepath.includes(`gatsby/dist`)) | ||
return ( | ||
<ul {...props}> | ||
{plugins.map(plugin => ( | ||
<li key={plugin.name}> | ||
{plugin.name}: {plugin.version} | ||
</li> | ||
))} | ||
</ul> | ||
) | ||
} | ||
|
||
const variableQuery = graphql` | ||
{ | ||
allSitePlugin { | ||
edges { | ||
node { | ||
name | ||
version | ||
pluginFilepath | ||
} | ||
} | ||
} | ||
} | ||
` | ||
|
||
export default DestructuringQuery |
22 changes: 22 additions & 0 deletions
22
...velopment-runtime/src/components/static-query/use-static-query/exported-variable-query.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React from "react" | ||
import { useStaticQuery, graphql } from "gatsby" | ||
|
||
function ExportedVariableQuery(props) { | ||
const data = useStaticQuery(exportedVariableQuery) | ||
|
||
if (data) { | ||
return <p {...props}>{data.sitePage.path}</p> | ||
} | ||
|
||
return `Error` | ||
} | ||
|
||
export const exportedVariableQuery = graphql` | ||
query { | ||
sitePage(path: { eq: "/static-query/" }) { | ||
path | ||
} | ||
} | ||
` | ||
|
||
export default ExportedVariableQuery |
27 changes: 27 additions & 0 deletions
27
e2e-tests/development-runtime/src/components/static-query/use-static-query/hot.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from "react" | ||
import { useStaticQuery, graphql } from "gatsby" | ||
|
||
function SupaHotFire(props) { | ||
const data = useStaticQuery(graphql` | ||
{ | ||
site { | ||
siteMetadata { | ||
# %AUTHOR% | ||
title | ||
} | ||
} | ||
} | ||
`) | ||
|
||
if (data) { | ||
return ( | ||
<p {...props}> | ||
{data.site.siteMetadata.title}: {data.site.siteMetadata.author} | ||
</p> | ||
) | ||
} | ||
|
||
return `Error` | ||
} | ||
|
||
export default SupaHotFire |
5 changes: 5 additions & 0 deletions
5
e2e-tests/development-runtime/src/components/static-query/use-static-query/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export { default as Inline } from "./inline-query" | ||
export { default as Variable } from "./variable-query" | ||
export { default as ExportedVariable } from "./exported-variable-query" | ||
export { default as Destructuring } from "./destructuring" | ||
export { default as Hot } from "./hot" |
21 changes: 21 additions & 0 deletions
21
e2e-tests/development-runtime/src/components/static-query/use-static-query/inline-query.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React from "react" | ||
import { useStaticQuery, graphql } from "gatsby" | ||
|
||
function InlineQuery(props) { | ||
const data = useStaticQuery(graphql` | ||
query { | ||
sitePage(path: { eq: "/static-query/" }) { | ||
pluginCreator { | ||
name | ||
} | ||
} | ||
} | ||
`) | ||
if (data) { | ||
return <p {...props}>{data.sitePage.pluginCreator.name}</p> | ||
} | ||
|
||
return `Error` | ||
} | ||
|
||
export default InlineQuery |
24 changes: 24 additions & 0 deletions
24
e2e-tests/development-runtime/src/components/static-query/use-static-query/variable-query.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from "react" | ||
import { useStaticQuery, graphql } from "gatsby" | ||
|
||
function VariableQuery(props) { | ||
const data = useStaticQuery(variableQuery) | ||
|
||
if (data) { | ||
return <p {...props}>{data.sitePage.pluginCreator.version}</p> | ||
} | ||
|
||
return `Error` | ||
} | ||
|
||
const variableQuery = graphql` | ||
query { | ||
sitePage(path: { eq: "/static-query/" }) { | ||
pluginCreator { | ||
version | ||
} | ||
} | ||
} | ||
` | ||
|
||
export default VariableQuery |
28 changes: 28 additions & 0 deletions
28
e2e-tests/development-runtime/src/components/static-query/variable-query.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from "react" | ||
import { StaticQuery, graphql } from "gatsby" | ||
|
||
function Variable(props) { | ||
return ( | ||
<StaticQuery | ||
query={reactHelmetPluginQuery} | ||
render={data => ( | ||
<div> | ||
<p {...props}> | ||
{data.sitePlugin.name}: {data.sitePlugin.version} | ||
</p> | ||
</div> | ||
)} | ||
/> | ||
) | ||
} | ||
|
||
const reactHelmetPluginQuery = graphql` | ||
query ReactHelmetPluginQuery { | ||
sitePlugin(name: { eq: "gatsby-plugin-react-helmet" }) { | ||
name | ||
version | ||
} | ||
} | ||
` | ||
|
||
export default Variable |
Oops, something went wrong.