Search code examples
nullgraphqlsequelize.jsexpress-graphql

express + express-graphql helloworld returns null


I am new to graphql and trying to implement a simple helloworld solution which when queried is returning null. The setup includes sequelize and pg and pg-hstore which I have however disabled to try and figure out where the problem is. Thanks in advance, been stuck for two days now.

Here is my resolver:

module.exports = {
  Query: {
    hello: (parent, { name }, context, info) => {
      return `Hello ${name}`;
    },
  },
};

Here is my schema:

const { buildSchema } = require("graphql");
module.exports = buildSchema(
  `type Query{
        hello(name:String!):String!
    }
    `
);

Here is the root of my app, app.js. I have left out the middleware that i have disabled since it seems inconsequential as i am having the error with or without them

const createError = require("http-errors");
const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser");
const logger = require("morgan");
const sassMiddleware = require("node-sass-middleware");
const graphqlHTTP = require("express-graphql");
const schema = require("./persistence/graphql/schema");
const persistence = require("./persistence/sequelize/models");
const rootValue = require("./persistence/sequelize/resolvers/index");

const indexRouter = require("./routes/index");
const usersRouter = require("./routes/users");

const app = express();

// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "pug");

app.use(
  "/api/graphql",
  graphqlHTTP({
    schema,
    rootValue,
    graphiql: true,
  })
);

module.exports = app;

when I query as follows:

{
   hello(name: "me")
}

I am getting this error:

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field Query.hello.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "hello"
      ]
    }
  ],
  "data": null
}

I know there are other servers out there, but I really need to figure this out with express-graphql. Thanks in advance.


Solution

  • This

    module.exports = {
      Query: {
        hello: (parent, { name }, context, info) => {
          return `Hello ${name}`;
        },
      },
    };
    

    is a resolver map like what graphql-tools or apollo-server expects to get. This is not a valid object to pass to rootValue.

    If you want to use rootValue to resolve your root-level fields, then the object would need to be just a map of field names with no type information. Additionally, if you use functions as the values, they would only take three arguments (args, context and info).

    module.exports = {
      hello: ({ name }, context, info) => {
        return `Hello ${name}`;
      },
    };
    

    That said, this is not a resolver function -- passing values like this through the root is not the same as actually providing resolvers for fields in your schema. Regardless of what HTTP library you're using (express-graphql or something else), you should pretty much never use buildSchema.