24 |
25 | ## Installation
26 |
27 | ```shell
28 | yarn add gatsby-source-graphcms gatsby-plugin-image
29 | ```
30 |
31 | > Note: Gatsby v4 requires Node.js >= 14.15.
32 |
33 | ## Configuration
34 |
35 | > We recommend using environment variables with your Hygraph `token` and `endpoint` values. You can learn more about using environment variables with Gatsby [here](https://www.gatsbyjs.org/docs/environment-variables).
36 |
37 | ### Basic
38 |
39 | ```js
40 | // gatsby-config.js
41 | module.exports = {
42 | plugins: [
43 | {
44 | resolve: 'gatsby-source-graphcms',
45 | options: {
46 | endpoint: process.env.HYGRAPH_ENDPOINT,
47 | },
48 | },
49 | ],
50 | }
51 | ```
52 |
53 | ### Authorization
54 |
55 | You can also provide an auth token using the `token` configuration key. This is necessary if your Hygraph project is **not** publicly available, or you want to scope access to a specific content stage (i.e. draft content).
56 |
57 | ```js
58 | // gatsby-config.js
59 | module.exports = {
60 | plugins: [
61 | {
62 | resolve: 'gatsby-source-graphcms',
63 | options: {
64 | endpoint: process.env.HYGRAPH_ENDPOINT,
65 | token: process.env.HYGRAPH_TOKEN,
66 | },
67 | },
68 | ],
69 | }
70 | ```
71 |
72 | ### Options
73 |
74 | | Key | Type | Description |
75 | | --------------------- | ---------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
76 | | `endpoint` | String (**required**) | The endpoint URL for the Hygraph project. This can be found in the [project settings UI](https://hygraph.com/docs/guides/concepts/apis#working-with-apis). |
77 | | `token` | String | If your Hygraph project is **not** publicly accessible, you will need to provide a [Permanent Auth Token](https://hygraph.com/docs/reference/authorization) to correctly authorize with the API. You can learn more about creating and managing API tokens [here](https://hygraph.com/docs/guides/concepts/apis#working-with-apis). |
78 | | `typePrefix` | String _(Default: `GraphCMS_`)\_ | The string by which every generated type name is prefixed with. For example, a type of `Post` in Hygraph would become `GraphCMS_Post` by default. If using multiple instances of the source plugin, you **must** provide a value here to prevent type conflicts. |
79 | | `downloadLocalImages` | Boolean _(Default: `false`)_ | Download and cache Hygraph image assets in your Gatsby project. [Learn more](#downloading-local-image-assets). |
80 | | `buildMarkdownNodes` | Boolean _(Default: `false`)_ | Build markdown nodes for all [`RichText`](https://hygraph.com/docs/reference/fields/rich-text) fields in your Hygraph schema. [Learn more](#using-markdown-nodes). |
81 | | `fragmentsPath` | String _(Default: `graphcms-fragments`)_ | The local project path where generated query fragments are saved. This is relative to your current working directory. If using multiple instances of the source plugin, you **must** provide a value here to prevent type and/or fragment conflicts. |
82 | | `locales` | String _(Default: `['en']`)_ | An array of locale key strings from your Hygraph project. [Learn more](#querying-localised-nodes). You can read more about working with localisation in Hygraph [here](https://hygraph.com/docs/guides/concepts/i18n). |
83 | | `stages` | String _(Default: `['PUBLISHED']`)_ | An array of Content Stages from your Hygraph project. [Learn more](#querying-from-content-stages). You can read more about using Content Stages [here](https://hygraph.com/guides/working-with-content-stages). |
84 | | `queryConcurrency` | Integer _(Default: 10)_ | The number of promises ran at once when executing queries. |
85 |
86 | ## Features
87 |
88 | - [Installation](#installation)
89 | - [Configuration](#configuration)
90 | - [Basic](#basic)
91 | - [Authorization](#authorization)
92 | - [Options](#options)
93 | - [Features](#features)
94 | - [Querying localised nodes](#querying-localised-nodes)
95 | - [Querying from content stages](#querying-from-content-stages)
96 | - [Usage with `gatsby-plugin-image`](#usage-with-gatsby-plugin-image)
97 | - [`gatsbyImageData` resolver arguments](#gatsbyimagedata-resolver-arguments)
98 | - [Downloading local image assets](#downloading-local-image-assets)
99 | - [Using markdown nodes](#using-markdown-nodes)
100 | - [Usage with `gatsby-plugin-mdx`](#usage-with-gatsby-plugin-mdx)
101 | - [Working with query fragments](#working-with-query-fragments)
102 | - [Modifying query fragments](#modifying-query-fragments)
103 | - [Contributors](#contributors)
104 | - [FAQs](#faqs)
105 |
106 | ### Querying localised nodes
107 |
108 | If using Hygraph localisation, this plugin provides support to build nodes for all provided locales.
109 |
110 | Update your plugin configuration to include the `locales` key.
111 |
112 | ```js
113 | // gatsby-config.js
114 | module.exports = {
115 | plugins: [
116 | {
117 | resolve: 'gatsby-source-graphcms',
118 | options: {
119 | endpoint: process.env.HYGRAPH_ENDPOINT,
120 | locales: ['en', 'de'],
121 | },
122 | },
123 | ],
124 | }
125 | ```
126 |
127 | To query for nodes for a specific locale, use the `filter` query argument.
128 |
129 | ```gql
130 | {
131 | enProducts: allGraphCmsProduct(filter: { locale: { eq: en } }) {
132 | nodes {
133 | name
134 | }
135 | }
136 | }
137 | ```
138 |
139 | Check out the [demo source](https://github.com/hygraph/gatsby-source-graphcms/tree/main/demo) for an example of a localisation implementation.
140 |
141 | ### Querying from content stages
142 |
143 | This plugin provides support to build nodes for entries from multiple Content Stages.
144 |
145 | The provided Content Stages **must** be accessible according to the configuration of your project's [API access](https://hygraph.com/docs/authorization). If providing a `token`, then that [Permanent Auth Token](https://hygraph.com/docs/authorization#permanent-auth-tokens) must have permission to query data from all provided Content Stages.
146 |
147 | The example below assumes that both the `DRAFT` and `PUBLISHED` stages are publicly accessible.
148 |
149 | ```js
150 | // gatsby-config.js
151 | module.exports = {
152 | plugins: [
153 | {
154 | resolve: 'gatsby-source-graphcms',
155 | options: {
156 | endpoint: process.env.HYGRAPH_ENDPOINT,
157 | stages: ['DRAFT', 'PUBLISHED'],
158 | },
159 | },
160 | ],
161 | }
162 | ```
163 |
164 | To query for nodes from a specific Content Stage, use the `filter` query argument.
165 |
166 | ```gql
167 | {
168 | allGraphCmsProduct(filter: { stage: { eq: DRAFT } }) {
169 | nodes {
170 | name
171 | }
172 | }
173 | }
174 | ```
175 |
176 | ### Usage with `gatsby-plugin-image`
177 |
178 | > Requires [`gatsby-plugin-image`](https://www.gatsbyjs.com/plugins/gatsby-plugin-image) as a project dependency.
179 |
180 | This source plugin supports `gatsby-plugin-image` for responsive, high performance Hygraph images direct from our CDN.
181 |
182 | Use the `gatsbyImageData` resolver on your `GraphCMS_Asset` nodes.
183 |
184 | ```gql
185 | {
186 | allGraphCmsAsset {
187 | nodes {
188 | gatsbyImageData(layout: FULL_WIDTH)
189 | }
190 | }
191 | }
192 | ```
193 |
194 | #### `gatsbyImageData` resolver arguments
195 |
196 | | Key | Type | Description |
197 | | ---------------------- | ------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
198 | | `aspectRatio` | Float | Force a specific ratio between the image’s width and height. |
199 | | `backgroundColor` | String | Background color applied to the wrapper. |
200 | | `breakpoints` | [Int] | Output widths to generate for full width images. Default is to generate widths for common device resolutions. It will never generate an image larger than the source image. The browser will automatically choose the most appropriate. |
201 | | `height` | Int | Change the size of the image. |
202 | | `layout` | GatsbyImageLayout (`CONSTRAINED`/`FIXED`/`FULL_WIDTH`) | Determines the size of the image and its resizing behavior. |
203 | | `outputPixelDensities` | [Float] | A list of image pixel densities to generate. It will never generate images larger than the source, and will always include a 1x image. The value is multiplied by the image width, to give the generated sizes. For example, a `400` px wide constrained image would generate `100`, `200`, `400` and `800` px wide images by default. Ignored for full width layout images, which use `breakpoints` instead. |
204 | | `quality` | Int | The default image quality generated. This is overridden by any format-specific options. |
205 | | `sizes` | String | [The ` sizes` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attributes), passed to the img tag. This describes the display size of the image, and does not affect generated images. You are only likely to need to change this if your are using full width images that do not span the full width of the screen. |
206 | | `width` | Int | Change the size of the image. |
207 | | `placeholder` | `NONE`/`BLURRED`/`DOMINANT_COLOR`/`TRACED_SVG` | Choose the style of temporary image shown while the full image loads. |
208 |
209 | **NOTE**: `gatsby-plugin-sharp` needs to be listed as a dependency on your project if you plan to use placeholder `TRACED_SVG` or `DOMINANT_COLOR`.
210 |
211 | For more information on using `gatsby-plugin-image`, please see the [documentation](https://www.gatsbyjs.com/plugins/gatsby-plugin-image/).
212 |
213 | ### Downloading local image assets
214 |
215 | If you prefer, the source plugin also provides the option to download and cache Hygraph assets in your Gatsby project.
216 |
217 | To enable this, add `downloadLocalImages: true` to your plugin configuration.
218 |
219 | ```js
220 | // gatsby-config.js
221 | module.exports = {
222 | plugins: [
223 | {
224 | resolve: 'gatsby-source-graphcms',
225 | options: {
226 | endpoint: process.env.HYGRAPH_ENDPOINT,
227 | downloadLocalImages: true,
228 | },
229 | },
230 | ],
231 | }
232 | ```
233 |
234 | This adds a `localFile` field to the `GraphCMS_Asset` type which resolves to the file node generated at build by [`gatsby-source-filesystem`](https://www.gatsbyjs.org/packages/gatsby-source-filesystem).
235 |
236 | ```gql
237 | {
238 | allGraphCmsAsset {
239 | nodes {
240 | localFile {
241 | childImageSharp {
242 | gatsbyImageData(layout: FULL_WIDTH)
243 | }
244 | }
245 | }
246 | }
247 | }
248 | ```
249 |
250 | ### Using markdown nodes
251 |
252 | This source plugin provides the option to build markdown nodes for all `RichText` fields in your Hygraph schema, which in turn can be used with [MDX](https://mdxjs.com).
253 |
254 | To enable this, add `buildMarkdownNodes: true` to your plugin configuration.
255 |
256 | ```js
257 | // gatsby-config.js
258 | module.exports = {
259 | plugins: [
260 | {
261 | resolve: 'gatsby-source-graphcms',
262 | options: {
263 | endpoint: process.env.HYGRAPH_ENDPOINT,
264 | buildMarkdownNodes: true,
265 | },
266 | },
267 | ],
268 | }
269 | ```
270 |
271 | Enabling this option adds a `markdownNode` nested field to all `RichText` fields on the generated Gatsby schema.
272 |
273 | You will need to rebuild your `graphcms-fragments` if you enable embeds on a Rich Text field, or you add/remove additional fields to your Hygraph schema.
274 |
275 | #### Usage with `gatsby-plugin-mdx`
276 |
277 | These newly built nodes can be used with [`gatsby-plugin-mdx`](https://www.gatsbyjs.org/packages/gatsby-plugin-mdx) to render markdown from Hygraph.
278 |
279 | Once installed, you will be able to query for `MDX` fields using a query similar to the one below.
280 |
281 | ```gql
282 | {
283 | allGraphCmsPost {
284 | nodes {
285 | id
286 | content {
287 | markdownNode {
288 | childMdx {
289 | body
290 | }
291 | }
292 | }
293 | }
294 | }
295 | }
296 | ```
297 |
298 | Check out the [demo source](https://github.com/hygraph/gatsby-source-graphcms/tree/main/demo) for an example of a full MDX implementation.
299 |
300 | ### Working with query fragments
301 |
302 | The source plugin will generate and save GraphQL query fragments for every node type. By default, they will be saved in a `graphcms-fragments` directory at the root of your Gatsby project. This can be configured:
303 |
304 | > If using multiple instances of the source plugin, you **must** provide a value to prevent type and/or fragment conflicts.
305 |
306 | ```js
307 | // gatsby-config.js
308 | module.exports = {
309 | plugins: [
310 | {
311 | resolve: 'gatsby-source-graphcms',
312 | options: {
313 | endpoint: process.env.HYGRAPH_ENDPOINT,
314 | fragmentsPath: 'my-query-fragments',
315 | },
316 | },
317 | ],
318 | }
319 | ```
320 |
321 | The generated fragments are then read from the project for subsequent builds. It is recommended that they are checked in to version control for your project.
322 |
323 | Should you make any changes or additions to your Hygraph schema, you will need to update the query fragments accrdingly. Alternatively they will be regnerated on a subsequent build after removing the directory from your project.
324 |
325 | #### Modifying query fragments
326 |
327 | In some instances, you may need modify query fragments on a per type basis. This may involve:
328 |
329 | - Removing unrequired fields
330 | - Adding new fields with arguments as an aliased field
331 |
332 | For example, adding a `featuredCaseStudy` field:
333 |
334 | ```graphql
335 | fragment Industry on Industry {
336 | featuredCaseStudy: caseStudies(where: { featured: true }, first: 1)
337 | }
338 | ```
339 |
340 | Field arguments cannot be read by Gatsby from the Hygraph schema. Instead we must alias any required usages as aliased fields. In this example, the `featuredCaseStudy` field would then be available in our Gatsby queries:
341 |
342 | ```graphql
343 | {
344 | allGraphCmsIndustry {
345 | nodes {
346 | featuredCaseStudy {
347 | ...
348 | }
349 | }
350 | }
351 | }
352 | ```
353 |
354 | ## Contributors
355 |
356 | Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
357 |
358 |
359 |
360 |
361 |