Last active
January 8, 2019 00:09
-
-
Save robrichard/d5a6ae7ecb3c9efc7fad7b3236d255e7 to your computer and use it in GitHub Desktop.
express-graphql persisted query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very simple approach