Skip to content

Commit a226769

Browse files
eojthebravesidharthachatterjee
authored andcommitted
feat(gatsby-source-drupal): Add ability to use JSON API filters when querying. (#12508)
1 parent a76f92b commit a226769

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

packages/gatsby-source-drupal/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,46 @@ module.exports = {
3434
}
3535
```
3636

37+
### Filters
38+
39+
You can use the `filters` option to limit the data that is retrieved from Drupal. Filters are applied per JSON API collection. You can use any [valid JSON API filter query](https://www.drupal.org/docs/8/modules/jsonapi/filtering). For large data sets this can reduce the build time of your application by allowing Gatsby to skip content you'll never use.
40+
41+
As an example, if your JSON API endpoint (https://live-contentacms.pantheonsite.io/api) returns the following collections list, then `articles` and `recipes` are both collections that can have a filters applied:
42+
43+
```json
44+
{
45+
...
46+
links: {
47+
articles: "https://live-contentacms.pantheonsite.io/api/articles",
48+
recipes: "https://live-contentacms.pantheonsite.io/api/recipes",
49+
...
50+
}
51+
}
52+
```
53+
54+
To retrieve only recipes with a specific tag you could do something like the following where the key (recipe) is the collection from above, and the value is the filter you want to apply.
55+
56+
```javascript
57+
// In your gatsby-config.js
58+
module.exports = {
59+
plugins: [
60+
{
61+
resolve: `gatsby-source-drupal`,
62+
options: {
63+
baseUrl: `https://live-contentacms.pantheonsite.io/`,
64+
apiBase: `api`,
65+
filters: {
66+
// collection : filter
67+
recipe: "filter[tags.name][value]=British",
68+
},
69+
},
70+
},
71+
],
72+
}
73+
```
74+
75+
Which would result in Gatsby using the filtered collection https://live-contentacms.pantheonsite.io/api/recipes?filter[tags.name][value]=British to retrieve data.
76+
3777
### Basic Auth
3878

3979
You can use `basicAuth` option if your site is protected by basicauth.

packages/gatsby-source-drupal/src/gatsby-node.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const createContentDigest = obj =>
1414

1515
exports.sourceNodes = async (
1616
{ actions, getNode, hasNodeChanged, store, cache, createNodeId },
17-
{ baseUrl, apiBase, basicAuth }
17+
{ baseUrl, apiBase, basicAuth, filters }
1818
) => {
1919
const { createNode } = actions
2020

@@ -50,6 +50,15 @@ exports.sourceNodes = async (
5050
if (typeof url === `object`) {
5151
// url can be string or object containing href field
5252
url = url.href
53+
54+
// Apply any filters configured in gatsby-config.js. Filters
55+
// can be any valid JSON API filter query string.
56+
// See https://www.drupal.org/docs/8/modules/jsonapi/filtering
57+
if (typeof filters === `object`) {
58+
if (filters.hasOwnProperty(type)) {
59+
url = url + `?${filters[type]}`
60+
}
61+
}
5362
}
5463

5564
let d

0 commit comments

Comments
 (0)