Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoint to refresh external data #3054

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/gatsby-cli/src/create-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ function buildLocalCommands(cli, isLocalSite) {
handler: getCommandHandler(`develop`),
})

cli.command({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you assuming that you'd have gatsby develop running and then run this in another terminal tab? If so, this wouldn't work as Gatsby keeps the node data in memory — we need to add the ability to pass commands to the running gatsby develop process e.g. press "r" and a refresh would be triggered.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking! This does work (in my local tests, at least), as it makes a post request to the port the server is running on, hitting an endpoint that calls sourceNodes().

If I understand correctly, you envision that the process running gatsby develop is also listening to stdin and updating based on input there, (eg: pressing "r" in the terminal window). I'm not familiar with any programs that do that, but I can totally give that a shot.

Even without the refresh command, would you be open to the dev server accepting a webhook to reload the data? That is my use case and what some others commenting on the related issues are looking for.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, pressing r as in reload (kind of like how Jest CLI works?) could be nice for local development, but I agree with @PepperTeasdale that the main use case would be to use the webhook … since it's just a POST, building a trigger that fits the requirements on top of it would be trivial.

command: `refresh`,
desc: `Reload external data sources`,
builder: _ =>
_.option(`H`, {
alias: `host`,
type: `string`,
default: defaultHost,
describe: `Set host. Defaults to ${defaultHost}`,
})
.option(`p`, {
alias: `port`,
type: `string`,
default: `8000`,
describe: `Set port. Defaults to 8000`,
}),
handler: getCommandHandler(`refresh`),
})

cli.command({
command: `build`,
desc: `Build a Gatsby project.`,
Expand Down
14 changes: 14 additions & 0 deletions packages/gatsby/src/commands/develop.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const launchEditor = require(`react-dev-utils/launchEditor`)
const formatWebpackMessages = require(`react-dev-utils/formatWebpackMessages`)
const chalk = require(`chalk`)
const address = require(`address`)
const sourceNodes = require(`../utils/source-nodes`)

// const isInteractive = process.stdout.isTTY

Expand Down Expand Up @@ -91,6 +92,19 @@ async function startServer(program) {
graphiql: true,
})
)

/**
* Refresh external data sources.
* If no GATSBY_REFRESH_TOKEN env var is available, then no Authorization header is required
**/
app.post(`/__refresh`, (req, res) => {
if (req.headers.authorization === process.env.GATSBY_REFRESH_TOKEN) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick or misunderstanding perhaps, but if process.env.GATSBY_REFRESH_TOKEN is undefined, shouldn't refresh happen regardless of what is passed in res.headers.authorization?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for having a look! You point about the authorization headers makes sense. Also intend on adding appropriate documentation. Just wanted to handle any requested changes to the implementation details before writing it out (eg if the refresh token is defined as a command line argument when starting the server, rather than an env var).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd personally vote for it to be an env var

console.log(`Refreshing source data`)
sourceNodes()
}
res.end()
})

app.get(`/__open-stack-frame-in-editor`, (req, res) => {
launchEditor(req.query.fileName, req.query.lineNumber)
res.end()
Expand Down
26 changes: 26 additions & 0 deletions packages/gatsby/src/commands/refresh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* @flow */

const http = require(`http`)

module.exports = async (program: { host: string, port: string }) => {
const options = {
host: program.host,
port: program.port,
path: `/__refresh`,
method: `POST`,
}

const req = http.request(options, (res) => {
res.on(`end`, () => {
console.log()
console.info(`Successfully refreshed external data`)
})
})

req.on(`error`, (e) => {
console.log()
console.error(`Unable to refresh external data: ${e.message}`)
})

req.end()
}