Skip to content

Instantly share code, notes, and snippets.

@Friss
Last active September 2, 2020 13:40
Show Gist options
  • Save Friss/90604b5f53ad4bb38146a8f49c730c13 to your computer and use it in GitHub Desktop.
Save Friss/90604b5f53ad4bb38146a8f49c730c13 to your computer and use it in GitHub Desktop.
Apollo error test
var express = require('express');
var bodyParser = require('body-parser');
var apolloServerExpress = require('apollo-server-express');
var {graphqlExpress, graphiqlExpress} = apolloServerExpress;
var makeExecutableSchema = require('graphql-tools').makeExecutableSchema;
var typeDefs = `
type Query {
testString: String
}
`;
var resolvers = {
Query: {
testString(_, args) {
return 'Error test';
},
},
};
var app = express();
app.use(
'/graphql',
bodyParser.json(),
graphqlExpress({
schema: makeExecutableSchema({typeDefs, resolvers}),
})
);
app.use('/graphiql', graphiqlExpress({endpointURL: '/graphql'}));
// catch 404 and forward to error handler
app.use(function(req, res, next) {
console.log('In 404');
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
console.log('In error handler');
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.json({err});
});
app.listen(8888, () =>
console.log(`GraphiQL is now running on http://localhost:${8888}/graphiql`)
);
zfriss@BOSMM2TX2V7HTD8 graphql-test $ node error-test.js
GraphiQL is now running on http://localhost:8888/graphiql
In 404
In error handler
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:504:11)
at ServerResponse.setHeader (_http_outgoing.js:511:3)
at ServerResponse.header (/Users/zfriss/Desktop/Dev/ion/ionstats-server/node_modules/express/lib/response.js:730:10)
at ServerResponse.send (/Users/zfriss/Desktop/Dev/ion/ionstats-server/node_modules/express/lib/response.js:170:12)
at ServerResponse.json (/Users/zfriss/Desktop/Dev/ion/ionstats-server/node_modules/express/lib/response.js:256:15)
at /Users/zfriss/Desktop/Dev/ion/ionstats-server/error-test.js:50:7
at Layer.handle_error (/Users/zfriss/Desktop/Dev/ion/ionstats-server/node_modules/express/lib/router/layer.js:71:5)
at trim_prefix (/Users/zfriss/Desktop/Dev/ion/ionstats-server/node_modules/express/lib/router/index.js:315:13)
at /Users/zfriss/Desktop/Dev/ion/ionstats-server/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/zfriss/Desktop/Dev/ion/ionstats-server/node_modules/express/lib/router/index.js:335:12)
@arvi
Copy link

arvi commented Oct 10, 2019

Hello,

I am also experiencing same Cannot set headers after they are sent to the client when trying to clear session cookies upon logout:

logout: requiresAuth.createResolver(
      async (parent, args, { req, res }) => {
      req.session.destroy((err) => {
          if (err) reject(err);
            res.clearCookie(SESS_NAME);
            resolve(true);
      });
});

@gaurav-bothra
Copy link

gaurav-bothra commented Sep 2, 2020

#4527 same error with express-session and nest/graphql

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