Skip to content

Instantly share code, notes, and snippets.

@robrichard
Last active January 8, 2019 00:09
Show Gist options
  • Save robrichard/d5a6ae7ecb3c9efc7fad7b3236d255e7 to your computer and use it in GitHub Desktop.
Save robrichard/d5a6ae7ecb3c9efc7fad7b3236d255e7 to your computer and use it in GitHub Desktop.
express-graphql persisted query
const express = require('express');
const bodyParser = require('body-parser');
const graphqlHTTP = require('express-graphql');
const app = express();
app.use(bodyParser.json());
async function getPersistedQueryMiddleware(req, res, next) {
if (!req.body.id) {
next();
return;
}
// get query from database/file system/where ever it may be saved
// should also cache to avoid multiple lookups
const queryText = await loadQuery(req.body.id);
req.body.query = queryText;
next();
}
app.use('/graphql', getPersistedQueryMiddleware, graphqlHTTP({
schema: MyGraphQLSchema,
graphiql: true
}));
app.listen(4000);
@sibelius
Copy link

sibelius commented Jan 4, 2019

Very simple approach

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment