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 support for cache refresh via GraphiQL #25960

Merged
merged 3 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion packages/gatsby-graphiql-explorer/src/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ class App extends React.Component {
graphQLFetcher({
query: getIntrospectionQuery(),
}).then(result => {
const newState = { schema: buildClientSchema(result.data) }
const newState = {
schema: buildClientSchema(result.data),
enableRefresh: result.extensions.enableRefresh,
refreshToken: result.extensions.refreshToken,
}

if (this.state.query === null) {
try {
Expand Down Expand Up @@ -302,6 +306,16 @@ class App extends React.Component {
this.setState({ codeExporterIsOpen: newCodeExporterIsOpen })
}

_refreshExternalDataSources = () => {
const options = { method: `post` }
if (this.state.refreshToken) {
options.headers = {
Authorization: this.state.refreshToken,
}
}
return fetch(`/__refresh`, options)
}

render() {
const { query, variables, schema, codeExporterIsOpen } = this.state
const codeExporter = codeExporterIsOpen ? (
Expand Down Expand Up @@ -356,6 +370,13 @@ class App extends React.Component {
label="Code Exporter"
title="Toggle Code Exporter"
/>
{this.state.enableRefresh && (
<GraphiQL.Button
onClick={this._refreshExternalDataSources}
label="Refresh Data"
title="Refresh Data from External Sources"
/>
)}
</GraphiQL.Toolbar>
</GraphiQL>
{codeExporter}
Expand Down
6 changes: 6 additions & 0 deletions packages/gatsby/src/utils/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ export async function startServer(
return {
schema,
graphiql: false,
extensions(): { [key: string]: unknown } {
return {
enableRefresh: process.env.ENABLE_GATSBY_REFRESH_ENDPOINT,
refreshToken: process.env.GATSBY_REFRESH_TOKEN,
}
},
context: withResolverContext({
schema,
schemaComposer: schemaCustomization.composer,
Expand Down