question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can we use middlewares after graphqlHTTP?

See original GitHub issue

Is there anyway to use a middleware after graphqlHTTP middleware? As a use case, suppose we want to use response.redirect() after using graphqlHTTP middleware. How can we do that?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

11reactions
amokrushincommented, Apr 30, 2017
const { PassThrough } = require('stream');

function graphqlMiddlewareWrapper(graphqlMiddleware) {
    return (req, res, next) => {
        const resProxy = new PassThrough();
        resProxy.headers = new Map();
        resProxy.statusCode = 200;
        resProxy.setHeader = (name, value) => {
            resProxy.headers.set(name, value);
        };
        res.graphqlResponse = (cb) => {
            res.statusCode = resProxy.statusCode;
            resProxy.headers.forEach((value, name) => {
                res.setHeader(name, value);
            });
            resProxy.pipe(res).on('finish', cb);
        };
        graphqlMiddleware(req, resProxy).then(() => next(), next);
    };
}

Usage

app.use('/graphql',
    graphqlMiddlewareWrapper(graphqlHttp({
        schema,
        graphiql: true,
    })),
    (req, res, next) => {
        console.log('SUCCESS');
        if (something) {
            // res.redirect(), etc
            res.status(200).end('something');
        } else {
            // pass through graphql response
            res.graphqlResponse(next);
        }
    },
    (err, req, res, next) => {
        console.log('CATCH ERROR');
        res.status(500).end(err.message);
        next();
    }
);
7reactions
scottc-netflixcommented, Jul 14, 2018

Just FYI for anyone using apollo-server-express you can do this:

graphqlExpress({
  schema,
  formatResponse(response) {
    // do something with response
    return response;
  },
  formatError(err) {
    // same
    return err;
  }
})

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mount middleware after graphqlHTTP.Middleware
I'm trying to get a middleware run after the GraphQL resolver. These were my first two attempts: app.use('/api', graphqlHTTP({ schema: ...
Read more >
Authentication and Express Middleware - GraphQL
To use middleware with a GraphQL resolver, just use the middleware like you would with a normal Express app. The request object is...
Read more >
Authentication and Express Middleware with GraphQL
In this article, we'll look at how to use middleware with Express GraphQL. Express Middleware. We can use Express middlewares as usual if...
Read more >
graphql-middleware - npm
Start using graphql-middleware in your project by running `npm i ... There are 188 other projects in the npm registry using graphql-middleware.
Read more >
Create your first GraphQL app with nodejs and set an auth ...
After we create our project the first thing that we should do is to create our server: const express = require('express') const graphqlHTTP...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found