-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick or misunderstanding perhaps, but if There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
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() | ||
} |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 tostdin
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.There was a problem hiding this comment.
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.