我是graphql的新手,正在尝试实现一个简单的helloworld解决方案,该解决方案在被查询时返回null。设置包括sequelize、pg和pg-hstore,我已经禁用了它们,以尝试找出问题所在。提前谢谢,已经卡了两天了。
这是我的解析器:
module.exports = {
Query: {
hello: (parent, { name }, context, info) => {
return `Hello ${name}`;
},
},
};以下是我的方案:
const { buildSchema } = require("graphql");
module.exports = buildSchema(
`type Query{
hello(name:String!):String!
}
`
);这是我的应用程序app.js的根目录。我遗漏了我禁用的中间件,因为它看起来无关紧要,因为无论有没有它们,我都会出现错误
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;当我按如下方式查询时:
{
hello(name: "me")
}我得到了这个错误:
{
"errors": [
{
"message": "Cannot return null for non-nullable field Query.hello.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"hello"
]
}
],
"data": null
}我知道还有其他服务器,但我真的需要用express-graphql解决这个问题。提前谢谢。
发布于 2020-04-24 18:42:09
这
module.exports = {
Query: {
hello: (parent, { name }, context, info) => {
return `Hello ${name}`;
},
},
};是一个解析器映射,就像graphql-tools或apollo-server期望得到的那样。这不是可传递给rootValue的有效对象。
如果您希望使用rootValue来解析根级别的字段,那么该对象需要只是一个字段名称的映射,而不需要类型信息。此外,如果使用函数作为值,它们将只接受三个参数(args、context和info)。
module.exports = {
hello: ({ name }, context, info) => {
return `Hello ${name}`;
},
};也就是说,这不是一个解析器函数--通过根传递这样的值与为模式中的字段实际提供解析器是不同的。无论您使用的是什么HTTP库(express-graphql或其他什么),您都应该使用never use buildSchema。
https://stackoverflow.com/questions/61405980
复制相似问题