Skip to content

Commit 2b97f00

Browse files
authored
docs: add explanation for seed-query (#2113)
1 parent 8684b83 commit 2b97f00

2 files changed

Lines changed: 188 additions & 0 deletions

File tree

47.6 KB
Loading
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
---
2+
title: 'Seed Query'
3+
description: 'Learn how the seed query in faust.js helps determine the correct template to render in a headless WordPress app.'
4+
---
5+
6+
Seed query is an initial request sent to WordPress to determine the type and basic properties of the content. It's the first step in fetching the content from WordPress, but it does not fetch the content itself on its own.
7+
8+
Since faust.js tries to mimic the native [template hierarchy of WordPress](https://developer.wordpress.org/themes/basics/template-hierarchy/), seed query plays a crucial role in the data flow. It helps faust.js to have the required information to determine which template to render.
9+
10+
Here’s a simplified workflow of how faust.js uses the seed query:
11+
12+
![Image showing the graph visualizing the workflow from URI to seed query to template specific query to template.](./images/seed-query-1.png)
13+
14+
1. User requests a page with a URI (e.g. faustexample.wpengine.com **/sample-page/** )
15+
2. Faust.js sends seed Query
16+
3. Seed query returns the template type and properties
17+
4. Faust.js sends template specific queries and retrieves the whole content based on the query
18+
5. Template being rendered with the content
19+
20+
In faust.js seed query looks like this:
21+
22+
```js title="packages/faustwp-core/src/queries/seedQuery.ts"
23+
export const SEED_QUERY = gql`
24+
query GetSeedNode(
25+
$id: ID! = 0
26+
$uri: String! = ""
27+
$asPreview: Boolean = false
28+
) {
29+
... on RootQuery @skip(if: $asPreview) {
30+
nodeByUri(uri: $uri) {
31+
__typename
32+
...GetNode
33+
}
34+
}
35+
... on RootQuery @include(if: $asPreview) {
36+
contentNode(id: $id, idType: DATABASE_ID, asPreview: true) {
37+
__typename
38+
...GetNode
39+
}
40+
}
41+
}
42+
`;
43+
```
44+
45+
As you can see seed query relies on [nodeByUri](https://www.wpgraphql.com/2021/12/23/query-any-page-by-its-path-using-wpgraphql) and contentNode queries of WpGraphQL. In this case contentNode is being used for previews and nodeByUri for ordinary queries. Both of these queries will return the important information about the requested content with a minimal input, whether it is URI or ID.
46+
47+
Below you can see the fragments that are being used in seed query to get the content-specific data, based on the content type.
48+
49+
```js title="packages/faustwp-core/src/queries/seedQuery.ts"
50+
gql`
51+
fragment GetNode on UniformResourceIdentifiable {
52+
__typename
53+
uri
54+
id
55+
...DatabaseIdentifier
56+
...ContentType
57+
...User
58+
...TermNode
59+
...ContentNode
60+
...MediaItem
61+
...Page
62+
}
63+
64+
fragment DatabaseIdentifier on DatabaseIdentifier {
65+
databaseId
66+
}
67+
68+
fragment MediaItem on MediaItem {
69+
id
70+
mimeType
71+
}
72+
73+
fragment ContentType on ContentType {
74+
name
75+
isFrontPage
76+
isPostsPage
77+
}
78+
79+
fragment Page on Page {
80+
isFrontPage
81+
isPostsPage
82+
}
83+
84+
fragment TermNode on TermNode {
85+
isTermNode
86+
slug
87+
taxonomyName
88+
}
89+
90+
fragment ContentNode on ContentNode {
91+
isContentNode
92+
slug
93+
contentType {
94+
node {
95+
name
96+
}
97+
}
98+
template {
99+
templateName
100+
}
101+
}
102+
103+
fragment User on User {
104+
name
105+
userId
106+
databaseId
107+
}
108+
`;
109+
```
110+
111+
With this query WpGraphQL returns the unique data structure depending on the content type. Below you can see some of the examples.
112+
113+
Seed query response for a post:
114+
115+
```json
116+
{
117+
"data": {
118+
"nodeByUri": {
119+
"__typename": "Post",
120+
"uri": "/hello-world/",
121+
"id": "cG9zdDoxMA==",
122+
"databaseId": 10,
123+
"isContentNode": true,
124+
"slug": "hello-world",
125+
"contentType": {
126+
"node": {
127+
"name": "post"
128+
}
129+
},
130+
"template": {
131+
"templateName": "Default"
132+
}
133+
}
134+
}
135+
}
136+
```
137+
138+
Seed query response for a user:
139+
140+
```json
141+
{
142+
"data": {
143+
"nodeByUri": {
144+
"__typename": "User",
145+
"uri": "/author/admin/",
146+
"id": "dXNlcjox",
147+
"databaseId": 1,
148+
"name": "admin",
149+
"userId": 1
150+
}
151+
}
152+
}
153+
```
154+
155+
Seed query response for a page:
156+
157+
```json
158+
{
159+
"data": {
160+
"nodeByUri": {
161+
"__typename": "Page",
162+
"uri": "/sample-page/",
163+
"id": "cG9zdDoy",
164+
"databaseId": 2,
165+
"isContentNode": true,
166+
"slug": "sample-page",
167+
"contentType": {
168+
"node": {
169+
"name": "page"
170+
}
171+
},
172+
"template": {
173+
"templateName": "Default"
174+
},
175+
"isFrontPage": false,
176+
"isPostsPage": false
177+
}
178+
}
179+
}
180+
```
181+
182+
Since this query resolved to a node with a page type, faust.js will try to [match the following templates](https://github.com/wpengine/faustjs/blob/canary/packages/faustwp-core/src/getTemplate.ts) in order of appearance:
183+
184+
```js
185+
['page-example', 'page-1470', 'page', 'singular', 'index'];
186+
```
187+
188+
If you want to have a deep understanding of the templating system in faust.js, you can read [this article](https://wpengine.com/builders/understanding-the-templating-system-in-faust-js/).

0 commit comments

Comments
 (0)