Skip to content

Commit 7609e9e

Browse files
jserraom-allanson
authored andcommitted
docs(gatsby-source-contentful): Rewrite of gatsby-source-contentful query section (#7533)
* Rewrite of gatsby-source-contentful query section * Re-apply PR changes with a round of edits on top * Remove duplicate word 'real' * Reduce some duplication
1 parent 2ca32e5 commit 7609e9e

File tree

1 file changed

+90
-15
lines changed

1 file changed

+90
-15
lines changed

packages/gatsby-source-contentful/README.md

+90-15
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,25 @@ You can pass in any other options available in the [contentful.js SDK](https://g
7878

7979
## Notes on Contentful Content Models
8080

81-
There are currently some things to keep in mind when building your content models at contentful.
81+
There are currently some things to keep in mind when building your content models at Contentful.
8282

83-
1. At the moment, Fields that do not have at least 1 populated instance will not be created in the graphql schema.
83+
1. At the moment, fields that do not have at least one populated instance will not be created in the GraphQL schema.
8484

8585
2. When using reference fields, be aware that this source plugin will automatically create the reverse reference. You do not need to create references on both content types. For simplicity, it is easier to put the reference field on the child in child/parent relationships.
8686

87-
## How to query
87+
## How to query for nodes
8888

89-
Two standard data types will be available from Contentful: `ContentType` and
90-
`Asset`.
89+
Two standard node types are available from Contentful: `Asset` and `ContentType`.
9190

92-
You can query Asset nodes created from Contentful like the following:
91+
`Asset` nodes will be created in your site's GraphQL schema under `contentfulAsset` and `allContentfulAsset`.
92+
93+
`ContentType` nodes are a little different - their exact name depends on what you called them in your Contentful data models. The nodes will be created in your site's GraphQL schema under `contentful${entryTypeName}` and `allContentful${entryTypeName}`.
94+
95+
In all cases querying for nodes like `contentfulX` will return a single node, and nodes like `allContentfulX` will return all nodes of that type.
96+
97+
### Query for all nodes
98+
99+
You might query for **all** of a type of node:
93100

94101
```graphql
95102
{
@@ -106,21 +113,84 @@ You can query Asset nodes created from Contentful like the following:
106113
}
107114
```
108115

109-
Non-standard data types, i.e. entry types you define in Contentful, will also be
110-
available in Gatsby. They'll be created in your site's GraphQL schema under
111-
`contentful${entryTypeName}` and `allContentful${entryTypeName}`. For example,
112-
if you have `Product` as one of your content types, you will be able to query it
113-
like the following:
116+
You might do this in your `gatsby-node.js` using Gatsby's [`createPages`](https://next.gatsbyjs.org/docs/node-apis/#createPages) Node API.
117+
118+
### Query for a single node
119+
120+
To query for a single `image` asset with the title 'foo' and a width of 1600px:
121+
122+
```
123+
export const assetQuery = graphql`
124+
{
125+
contentfulAsset(filter: { title: { eq: 'foo' } }) {
126+
image {
127+
resolutions(width: 1600) {
128+
width
129+
height
130+
src
131+
srcSet
132+
}
133+
}
134+
}
135+
}
136+
`
137+
```
138+
139+
To query for a single `CaseStudy` node with the short text properties `title` and `subtitle`:
140+
141+
```graphql
142+
{
143+
contentfulCaseStudy(filter: { title: { eq: 'bar' } }) {
144+
title
145+
subtitle
146+
}
147+
}
148+
```
149+
150+
> Note the use of [GraphQL arguments](https://graphql.org/learn/queries/#arguments) on the `contentfulAsset` and `resolutions` fields. See [Gatsby's GraphQL reference docs for more info](https://www.gatsbyjs.org/docs/graphql-reference/).
151+
152+
You might query for a **single** node inside a component in your `src/components` folder, using [Gatsby's `StaticQuery` component](https://www.gatsbyjs.org/docs/static-query/).
153+
154+
#### A note about LongText fields
155+
156+
If you include fields with a `LongText` type in your Contentful `ContentType`, their returned value will be **an object not a string**. This is because Contentful LongText fields are Markdown by default. In order to handle the Markdown content properly, this field type is created as a child node so Gatsby can transform it to HTML.
157+
158+
`ShortText` type fields will be returned as strings.
159+
160+
Querying a **single** `CaseStudy` node with the ShortText properties `title` and `subtitle` and LongText property `body` requires formatting the LongText fields as an object with the _child node containing the exact same field name as the parent_:
161+
162+
```graphql
163+
{
164+
contentfulCaseStudy {
165+
title
166+
subtitle
167+
body {
168+
body
169+
}
170+
}
171+
}
172+
```
173+
174+
### Query for Assets in ContentType nodes
175+
176+
More typically your `Asset` nodes will be mixed inside of your `ContentType` nodes, so you'll query them together. All the same formatting rules for `Asset` and `ContentType` nodes apply.
177+
178+
To get **all** the `CaseStudy` nodes with ShortText fields `id`, `slug`, `title`, `subtitle`, LongText field `body` and heroImage `Asset` field, we use `allContentful${entryTypeName}` to return all instances of that `ContentType`:
114179

115180
```graphql
116181
{
117-
allContentfulProduct {
182+
allContentfulCaseStudy {
118183
edges {
119184
node {
120185
id
121-
productName
122-
image {
123-
fixed(width: 100) {
186+
slug
187+
title
188+
subtitle
189+
body {
190+
body
191+
}
192+
heroImage {
193+
resolutions(width: 1600) {
124194
width
125195
height
126196
src
@@ -133,6 +203,11 @@ like the following:
133203
}
134204
```
135205

206+
## More on Queries with Contentful and Gatsby
207+
208+
It is strongly recommended that you take a look at how data flows in a real Contentful and Gatsby application to fully understand how the queries, Node.js functions and React components all come together. Check out the example site at
209+
[using-contentful.gatsbyjs.org](https://using-contentful.gatsbyjs.org/).
210+
136211
## **Beta** [Contentful Rich Text](https://www.contentful.com/developers/docs/concepts/rich-text/)
137212

138213
Rich text feature is supported in this source plugin, if you want to serialize the field content to html you can add the plugin `@contentful/gatsby-transformer-contentful-richtext`.

0 commit comments

Comments
 (0)