Apollo-server: Minimal example throws "Cannot read property 'definitions' of undefined"

Created on 26 Aug 2016  路  12Comments  路  Source: apollographql/apollo-server

I'm trying to create a minimal GraphQL server example using Apollo Server. However, when I make a request to it, it responds with a 400 code and a body of:

{
  "errors": [
    {
      "message": "Cannot read property 'definitions' of undefined"
    }
  ]
}

As far as I can tell, this message is coming from inside Apollo Server itself and no stack trace is provided. Even if this is user error, I would very much appreciate a more helpful error message! This doesn't give me anything to work with.

Here's the sample code:

'use strict';
const express = require('express');
const apolloExpress = require('apollo-server').apolloExpress;
const bodyParser = require('body-parser');

const makeExecutableSchema = require('graphql-tools').makeExecutableSchema;

const typeDefs = [`
schema {
  query: RootQuery
}
`, `
type RootQuery {
  hello: String
}
`];

const resolvers = {
  RootQuery: {
    hello: () => 'Hello, world!'
  }
};

const myGraphQLSchema = makeExecutableSchema({
  typeDefs: typeDefs,
  resolvers,
});

const PORT = 3000;

var app = express();

app.use('/graphql', bodyParser.json(), apolloExpress({ schema: myGraphQLSchema }));

app.listen(PORT);

which, as far as I can tell, is correct according to the documentation. Here's my package versions in case there's a version mismatch:

  "apollo-server": "0.2.5",
  "body-parser": "1.15.2",
  "express": "4.14.0",
  "graphql": "0.6.1",
  "graphql-tools": "0.6.5"

I'm also running on Node v4.4.7 if that makes a difference.

Most helpful comment

I feel like GraphQL servers should have some kind of "debug mode" where they print the stack trace in the server logs rather than just returning it in the response.

All 12 comments

I feel like GraphQL servers should have some kind of "debug mode" where they print the stack trace in the server logs rather than just returning it in the response.

@dallonf try passing formatError: (err) => { console.log(err.stack); return err } in the apollo server options. That should print a stack trace and help you figure out where the error happened.

@stubailo Great idea, wanna make a PR? 馃榿

@helfer what do you think about checking process.env.NODE_ENV === 'production'? In dev mode, it can log errors by default, but not in prod?

Yeah, I think we should do that. See #111

@dallonf any luck debugging this?

I'll revisit on Monday, I'll let you know what I find then :)

Abhi just had a very similar problem (same error message) and I helped him solve it in the apollo slack channel. The error you're getting indicates that the execute function isn't getting an AST, which happens when the query either doesn't make it to the server in the right format, or it doesn't get parsed properly. You have to use something like body parser, and you have to make sure your request gets parsed. Abhi was using content type application/graphql, but bodyParser doesn't parse that unless you explicitly tell it to parse that as JSON. When we switched it to application/json, everything worked perfectly.

@helfer It's little confusing apollo-server won't accept simple -d "{ hello }", instead you need to either do server.use(bodyParser.json({ type: '*/*' })); or set application/json, and pass it as json like this: curl -X POST localhost:8000/query -d '{ "query": "{ version }" }'

Just spent an hour debugging this.. All for showing stack traces in development by default.

@sheerun yeah, I know, sorry about that. We opened an issue yesterday to provide a better error message. The format is documented though, right?

Ok, looks like actually sending a query solves the problem. I was sending blank requests just as a test. 馃憤 for #112, that was an absolutely dreadful first time experience. Thanks for your help!

I ran into a similar issue - it turned out I was passing typeDef instead of typeDefs

Was this page helpful?
0 / 5 - 0 ratings