Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(gatsby-source-drupal): added preview feature (#14630)
* added preview feature to drupal source module

* Update README.md

* added a more robust check for relationship data, empty arrays were causing errors in preview updates

* Update README.md

* reverted backticks in comments

* refactor: don't spawn extra server, re-use node creation and relationship handling

* there is no more preview config option

* remove stray console.log

* fix tests

* mark as experimental

* move webhook update handling to separate function to make it easier to test

* add tests for updating function

* add more notes about experimental state of preview feature

* move utils to separate file - fixes exporting variable that is not gatsby API

* normalize newline in gitignore
  • Loading branch information
Grant Glidewell authored and GatsbyJS Bot committed Jul 8, 2019
1 parent 530e8da commit a045a32
Show file tree
Hide file tree
Showing 10 changed files with 523 additions and 164 deletions.
1 change: 1 addition & 0 deletions packages/gatsby-source-drupal/.gitignore
@@ -1,2 +1,3 @@
/gatsby-node.js
/normalize.js
/utils.js
11 changes: 11 additions & 0 deletions packages/gatsby-source-drupal/README.md
Expand Up @@ -162,6 +162,17 @@ module.exports = {
}
```

## Gatsby Preview (experimental)

You will need to have the Drupal module installed, more information on that here: https://www.drupal.org/project/gatsby

In your Drupal module configuration, set the update URL to your Gatsby Preview instance URL.

_NOTES_:

- This is experimental feature in active development. APIs used for this feature are not yet stable - it can break while we iterate on API design (particularly when versions of `gatsby-source-drupal` and `Gatsby Live Preview` drupal module are incompatible).
- It's not feature complete yet. There is no handling of deleting content yet.

## How to query

You can query nodes created from Drupal like the following:
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-source-drupal/package.json
Expand Up @@ -10,6 +10,7 @@
"@babel/runtime": "^7.0.0",
"axios": "^0.19.0",
"bluebird": "^3.5.0",
"body-parser": "^1.19.0",
"gatsby-source-filesystem": "^2.1.2",
"lodash": "^4.17.10",
"tiny-async-pool": "^1.0.4"
Expand Down
@@ -0,0 +1,23 @@
{
"data": {
"type": "node--article",
"id": "article-4",
"attributes": {
"id": 24,
"uuid": "article-4",
"title": "Article #4",
"body": "Praesent accumsan nisl nulla."
},
"relationships": {
"field_tags": {
"data": [
{
"type": "taxonomy_term--tags",
"id": "tag-1"
}
]
}
}
},
"links": {}
}
@@ -0,0 +1,23 @@
{
"data": {
"type": "node--article",
"id": "article-3",
"attributes": {
"id": 23,
"uuid": "article-3",
"title": "Article #3 - Updated",
"body": "Aliquam non varius libero, sit amet consequat ex. Aenean porta turpis quis vulputate blandit. Suspendisse in porta erat. Sed sit amet scelerisque turpis, at rutrum mauris. Sed tempor eleifend lobortis. Proin maximus, massa sed dignissim sollicitudin, quam risus mattis justo, sit amet aliquam odio ligula quis urna. Interdum et malesuada fames ac ante ipsum primis in faucibus. Ut mollis leo nisi, at interdum urna fermentum ut. Fusce id suscipit neque, eu fermentum lacus. Donec egestas laoreet felis ac luctus. Vestibulum molestie mattis ante, a vulputate nunc ullamcorper at. Ut hendrerit ipsum eget gravida ultricies."
},
"relationships": {
"field_tags": {
"data": [
{
"type": "taxonomy_term--tags",
"id": "tag-2"
}
]
}
}
},
"links": {}
}
121 changes: 109 additions & 12 deletions packages/gatsby-source-drupal/src/__tests__/index.js
Expand Up @@ -21,13 +21,17 @@ jest.mock(`gatsby-source-filesystem`, () => {
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)

const { sourceNodes } = require(`../gatsby-node`)
const { handleWebhookUpdate } = require(`../utils`)

describe(`gatsby-source-drupal`, () => {
const nodes = {}
const createNodeId = id => `generated-id-${id}`
const baseUrl = `http://fixture`
const createContentDigest = jest.fn().mockReturnValue(`contentDigest`)
const { objectContaining } = expect
const actions = {
createNode: jest.fn(node => (nodes[node.id] = node)),
}

const activity = {
start: jest.fn(),
Expand All @@ -36,18 +40,18 @@ describe(`gatsby-source-drupal`, () => {
const reporter = {
info: jest.fn(),
activityTimer: jest.fn(() => activity),
log: jest.fn(),
}

beforeAll(async () => {
const args = {
createNodeId,
createContentDigest,
actions: {
createNode: jest.fn(node => (nodes[node.id] = node)),
},
reporter,
}
const args = {
createNodeId,
createContentDigest,
actions,
reporter,
getNode: id => nodes[id],
}

beforeAll(async () => {
await sourceNodes(args, { baseUrl })
})

Expand Down Expand Up @@ -118,23 +122,23 @@ describe(`gatsby-source-drupal`, () => {

it(`Creates back references`, () => {
expect(
nodes[createNodeId(`file-1`)].relationships[`node--article___NODE`]
nodes[createNodeId(`file-1`)].relationships[`node__article___NODE`]
).toEqual(
expect.arrayContaining([
createNodeId(`article-2`),
createNodeId(`article-3`),
])
)
expect(
nodes[createNodeId(`tag-1`)].relationships[`node--article___NODE`]
nodes[createNodeId(`tag-1`)].relationships[`node__article___NODE`]
).toEqual(
expect.arrayContaining([
createNodeId(`article-1`),
createNodeId(`article-3`),
])
)
expect(
nodes[createNodeId(`tag-2`)].relationships[`node--article___NODE`]
nodes[createNodeId(`tag-2`)].relationships[`node__article___NODE`]
).toEqual(expect.arrayContaining([createNodeId(`article-1`)]))
})

Expand All @@ -152,4 +156,97 @@ describe(`gatsby-source-drupal`, () => {
)
})
})

describe(`Update webhook`, () => {
describe(`Update content`, () => {
describe(`Before update`, () => {
it(`Attributes`, () => {
expect(nodes[createNodeId(`article-3`)].title).toBe(`Article #3`)
})
it(`Relationships`, () => {
expect(nodes[createNodeId(`article-3`)].relationships).toEqual({
field_main_image___NODE: createNodeId(`file-1`),
field_tags___NODE: [createNodeId(`tag-1`)],
})
})
it(`Back references`, () => {
expect(
nodes[createNodeId(`file-1`)].relationships[`node__article___NODE`]
).toContain(createNodeId(`article-3`))
expect(
nodes[createNodeId(`tag-1`)].relationships[`node__article___NODE`]
).toContain(createNodeId(`article-3`))
expect(
nodes[createNodeId(`tag-2`)].relationships[`node__article___NODE`]
).not.toContain(createNodeId(`article-3`))
})
})

describe(`After update`, () => {
beforeAll(async () => {
const nodeToUpdate = require(`./fixtures/webhook-update.json`).data

await handleWebhookUpdate({
nodeToUpdate,
...args,
})
})
it(`Attributes`, () => {
expect(nodes[createNodeId(`article-3`)].title).toBe(
`Article #3 - Updated`
)
})
it(`Relationships`, () => {
// removed `field_main_image`, changed `field_tags`
expect(nodes[createNodeId(`article-3`)].relationships).toEqual({
field_tags___NODE: [createNodeId(`tag-2`)],
})
})
it(`Back references`, () => {
// removed `field_main_image`, `file-1` no longer has back reference to `article-3`
expect(
nodes[createNodeId(`file-1`)].relationships[`node__article___NODE`]
).not.toContain(createNodeId(`article-3`))
// changed `field_tags`, `tag-1` no longer has back reference to `article-3`
expect(
nodes[createNodeId(`tag-1`)].relationships[`node__article___NODE`]
).not.toContain(createNodeId(`article-3`))
// changed `field_tags`, `tag-2` now has back reference to `article-3`
expect(
nodes[createNodeId(`tag-2`)].relationships[`node__article___NODE`]
).toContain(createNodeId(`article-3`))
})
})
})

describe(`Insert content`, () => {
it(`Node doesn't exist before webhook`, () => {
expect(nodes[createNodeId(`article-4`)]).not.toBeDefined()
expect(
nodes[createNodeId(`tag-1`)].relationships[`node__article___NODE`]
).not.toContain(createNodeId(`article-4`))
})

describe(`After insert`, () => {
beforeAll(async () => {
const nodeToUpdate = require(`./fixtures/webhook-insert.json`).data

await handleWebhookUpdate({
nodeToUpdate,
...args,
})
})
it(`Creates node`, () => {
expect(nodes[createNodeId(`article-4`)]).toBeDefined()
expect(nodes[createNodeId(`article-4`)].title).toBe(`Article #4`)
})

it(`Adds back references to referenced nodes`, () => {
expect(
nodes[createNodeId(`tag-1`)].relationships[`node__article___NODE`]
).toContain(createNodeId(`article-4`))
})
})
})
})
})

0 comments on commit a045a32

Please sign in to comment.