You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: remove TODOs from Static vs Normal Queries to focus more on internals (#19219)
* remove TODOs and focus the doc more on internals
* Apply suggestions from code review
Co-Authored-By: Michael <[email protected]>
* Apply suggestions from code review
Co-Authored-By: Marcy Sutton <[email protected]>
* updates to remove implementation details and clarify more concepts
* add one more example to show differences
* Apply suggestions from code review
Co-Authored-By: LB <[email protected]>
* Update docs/docs/static-vs-normal-queries.md
Co-Authored-By: LB <[email protected]>
Copy file name to clipboardExpand all lines: docs/docs/query-execution.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -95,7 +95,7 @@ In `develop` mode, every time a node is created, or is updated (e.g. via editing
95
95
96
96
#### Queue queries for execution
97
97
98
-
There is now a list of all pages that need to be executed (linked to their Query information). Gatsby will queue them for execution (for realz this time). A call to [runQueriesForPathnames](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js#L127) kicks off this step. For each page or static query, Gatsby creates a Query Job that looks something like:
98
+
There is now a list of all pages that need to be executed (linked to their Query information). Gatsby will queue them for execution (for real this time). A call to [runQueriesForPathnames](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/internal-plugins/query-runner/page-query-runner.js#L127) kicks off this step. For each page or static query, Gatsby creates a Query Job that looks something like:
Copy file name to clipboardExpand all lines: docs/docs/static-vs-normal-queries.md
+96-31Lines changed: 96 additions & 31 deletions
Original file line number
Diff line number
Diff line change
@@ -2,35 +2,21 @@
2
2
title: Static vs Normal Queries
3
3
---
4
4
5
-
> This documentation isn't up to date with the latest version of Gatsby.
6
-
>
7
-
> Outdated areas are:
8
-
>
9
-
> - describe how queries are stripped and JSON imports are rewritten
10
-
>
11
-
> You can help by making a PR to [update this documentation](https://github.com/gatsbyjs/gatsby/issues/14228).
5
+
Gatsby handles three varieties of GraphQL queries: Page queries (sometimes for simplicity referred to as "normal queries"), static queries using the `<StaticQuery />` component, and static queries using the `useStaticQuery` hook.
12
6
13
-
## How StaticQuery differs from page query
7
+
## Differences between varieties of GraphQL queries
14
8
15
-
StaticQuery can do most of the things that page query can, including fragments. The main differences are:
9
+
Static queries differ from Gatsby page queries in a number of ways. For pages, Gatsby is capable of handling queries with variables because of its awareness of page context. However, **page queries can only be made in top-level page components**.
16
10
17
-
- page queries can accept variables (via `pageContext`) but can only be added to _page_ components
18
-
- StaticQuery does not accept variables (hence the name "static"), but can be used in _any_ component, including pages
19
-
- StaticQuery does not work with raw React.createElement calls; please use JSX, e.g. `<StaticQuery />`
20
-
-_NOTE: you can also use the new `useStaticQuery` hook; more information below_
21
-
- Static Queries don't need to get run for each page.(ie:Just once)
11
+
In contrast, static queries do not take variables. This is because static queries are used inside specific components, and can appear lower in the component tree. Data fetched with a static query won't be dynamic (i.e. **they can't use variables**, hence the name "static" query), but they can be called at any level in the component tree.
22
12
23
-
## useStaticQuery hook
13
+
_For more information on the practical differences in usage between static and normal queries, refer to the guide on [static query](/docs/static-query/#how-staticquery-differs-from-page-query). This guide discusses the differences in how they are handled internally by Gatsby_
24
14
25
-
- Gatsby v2.1.0 introduces `useStaticQuery`, a Gatsby feature that allows you to use a React hook to query GraphQL
26
-
-`useStaticQuery` is a hook, contrary to `<StaticQuery />` which is a component
27
-
- Check out [how to query data at build time using `useStaticQuery`](https://www.gatsbyjs.org/docs/use-static-query/)
15
+
## Keeping track of site queries during build in Redux stores
28
16
29
-
### staticQueryComponents
17
+
Gatsby stores the queries from your site in Redux stores called `components` and `staticQueryComponents`. This process and a flowchart illustrating it are explained in the [query extraction](/docs/query-extraction/#store-queries-in-redux) guide.
30
18
31
-
Started here because they're referenced in page-query-runner:findIdsWithDataDependencies.
32
-
33
-
The redux `staticQueryComponents` is a map from component jsonName to StaticQueryObject. E.g
19
+
In Redux, `staticQueryComponents` is a `Map` from component `jsonName` to `StaticQueryObject`. An example entry in that data structure looks like this:
34
20
35
21
```javascript
36
22
{
@@ -45,18 +31,97 @@ The redux `staticQueryComponents` is a map from component jsonName to StaticQuer
45
31
}
46
32
```
47
33
48
-
The `staticQueryComponents` redux namespace is owned by the `static-query-components.js` reducer with reacts to `REPLACE_STATIC_QUERY` actions.
34
+
In the example above, `blog-2018-07-17-announcing-gatsby-preview-995` is the key, with the object as its value in the `Map`.
35
+
36
+
The `staticQueryComponents` Redux namespace watches for updates to queries while you are developing, and adds new data to your cache when queries change.
37
+
38
+
## Replacing queries with JSON imports
39
+
40
+
With the final build, there isn't a GraphQL server running to query for data. Gatsby has already [extracted](/docs/query-extraction/) and [run](/docs/query-execution/) the queries, [storing](/docs/query-execution/#save-query-results-to-redux-and-disk) them in files based on hashes in `/public/static/d/<hash>.json`. It can now remove code for GraphQL queries, because the data is already available.
49
41
50
-
It is created in query-watcher. TODO: Check other usages
42
+
### Distinguishing between static and normal queries
51
43
52
-
TODO: in query-watcher.js/handleQuery, we remove jsonName from dataDependencies. How did it get there? Why is jsonName used here, but for other dependencies, it's a path?
44
+
Babel traverses all of your source code looking for queries during query extraction. In order for Gatsby to handle static and normal queries differently, it looks for 3 specific cases in [`babel-plugin-remove-graphql-queries`](https://github.com/gatsbyjs/gatsby/blob/master/packages/babel-plugin-remove-graphql-queries/src/index.js):
53
45
54
-
### Usages
46
+
1. JSX nodes with the name `StaticQuery`
47
+
2. Calls to functions with the name `useStaticQuery`
48
+
3. Tagged template expressions using the `gql` tag
55
49
56
-
-[websocket-manager](#TODO). TODO
57
-
-[query-watcher](#TODO).
50
+
### Adding imports for page data
58
51
59
-
-`getQueriesSnapshot` returns map with snapshot of `state.staticQueryComponents`
60
-
- handleComponentsWithRemovedQueries. For each staticQueryComponent, if passed in queries doesn't include `staticQueryComponent.componentPath`. TODO: Where is StaticQueryComponent created? TODO: Where is queries passed into `handleComponentsWithRemovedQueries`?
52
+
Code that is specific for querying the GraphQL server set up during build time is no longer relevant, and can be swapped out in exchange for the JSON data that has been extracted for each query.
53
+
54
+
The imports related to GraphQL and query declarations are changed to imports for the JSON that correspond to the query result that Gatsby found when it ran the query. Consider the following component with with a static query written using the `useStaticQuery` hook:
55
+
56
+
```jsx
57
+
import { useStaticQuery, graphql } from"gatsby"
58
+
59
+
export () => {
60
+
constdata=useStaticQuery(graphql`
61
+
siteMetadata {
62
+
site {
63
+
title
64
+
}
65
+
}
66
+
`)
67
+
return (
68
+
<h1>
69
+
{data.siteMetadata.site.title}
70
+
</h1>
71
+
)
72
+
}
73
+
```
74
+
75
+
This component will have the query string removed and replaced with an import for the JSON file created and named with its specific hash. The Redux stores tracking the queries link the correct data to the page they correspond to.
76
+
77
+
The above component is rewritten like this:
78
+
79
+
```diff
80
+
- import { useStaticQuery, graphql } from "gatsby"
81
+
+ import dataJson from `/d/<hash>.json`
82
+
83
+
export () => {
84
+
- const data = useStaticQuery(graphql`
85
+
- siteMetadata {
86
+
- site {
87
+
- title
88
+
- }
89
+
- }
90
+
- `)
91
+
+ const data = dataJson
92
+
93
+
return (
94
+
<h1>
95
+
{data.siteMetadata.site.title}
96
+
</h1>
97
+
)
98
+
}
99
+
```
100
+
101
+
A page query would be updated in a similar fashion. Alhough the exact specifics of what has to change are different, the idea is the same:
102
+
103
+
```diff
104
+
- import { graphql } from "gatsby"
105
+
+ import dataJson from `/d/<hash>.json`
106
+
107
+
- export const query = graphql`
108
+
- query HomePageQuery {
109
+
- site {
110
+
- siteMetadata {
111
+
- description
112
+
- }
113
+
- }
114
+
- }
115
+
- `
116
+
117
+
- export ({ data }) => {
118
+
+ export ({ data = dataJson }) => {
119
+
return (
120
+
<h1>
121
+
{data.siteMetadata.site.title}
122
+
</h1>
123
+
)
124
+
}
125
+
```
61
126
62
-
TODO: Finish above
127
+
Gatsby will remove unnecessary imports like `useStaticQuery` and `graphql` from your pages along with the strings containing queries as well.
0 commit comments