3

Had this working in version 1, but the whole server config has changed. This is what I have, after adding bodyparser() to the express app as was suggested by Daniel in the comments:

    const server = new ApolloServer({
    typeDefs,
    resolvers,
    playground: {
        settings: {
            'editor.theme': 'light',
        }
    },
})

// Initialize the app
const app = express();
app.use(cors())
app.use(bodyParser.json())

server.applyMiddleware({
    app
})

app.post('/calc', function(req, res){
    const {body} = req;

    console.log("HOWDYHOWDYHOWDY", body) // <== body is {}

    res.setHeader('content-type', 'application/json')

    calculate(body)
        .then(result => res.send(result))
        .catch(e => res.status(400).send({error: e.toString()}))    
})

The request body is never making it to the app.post handler, though the handler is called. I see it going out from the browser, though. Any ideas?

Update: Daniel had the correct answer, but I had another problem in the request headers I was using. Once I fixed that, then the post handler received the body.

Jeff Lowery
  • 2,492
  • 2
  • 32
  • 40

2 Answers2

6

Apollo's middleware applies the bodyparser middleware specifically to the GraphQL endpoint -- it won't affect any other routes your server exposes. In order to correctly populate req.body, you need to add the bodyparser middleware yourself, for example:

app.use(bodyParser.json())
app.post('/calc', routeHandler)

// or...
app.post('/calc', bodyParser.json(), routeHandler)
Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • I appreciate your response, Daniel. I did what you suggested, and will update my original post. Now I get an empty JSON object, instead of undefined. Better? So I am a little surprised that the req.body would be empty without bodyparser middleware attached to the app--I would expect a text body. Without bodyparser.json() = undefined; with = {} – Jeff Lowery Oct 11 '18 at 17:59
  • 2
    req.body is undefined by default (see express docs: https://expressjs.com/en/4x/api.html#req.body) You may need to select the appropriate parser to see the body populated correctly, depending on what you're sending. See this answer (https://stackoverflow.com/a/47486182/6024220) and the body-parser docs for more details. Also be aware that if you're using a multipart bodies, you'll need a different library like multer instead. – Daniel Rearden Oct 11 '18 at 18:46
  • Okay, learned something. I am calling the correct bodyparser, so that's not the problem. Other people are having similar issues it seems. Can't find a direct corollary with what I'm trying to do here, but when I find an answer I will post it. – Jeff Lowery Oct 11 '18 at 19:16
  • You could also just try moving your `/calc` route above your `applyMiddleware` call, so it's called and terminated before apollo does anything to your request. – Daniel Rearden Oct 11 '18 at 19:34
  • yes, I did try that, to no effect. BUT... turns out I had an fault with the request headers in my client code (a line I forgot to delete), so that was the second issue. Now, no matter where the post handler is placed, I see a body (finally!). Marking your response as the answer. Thanks again. – Jeff Lowery Oct 11 '18 at 19:39
3

I just ran into this as well. Fixed it by passing the following into the headers:

Content-Type: application/json
ha404
  • 1,989
  • 2
  • 13
  • 10