跟着官方文档代码跑
let { graphql, buildSchema } = require('graphql');
let schema = buildSchema(`
type Query {
hello: String
}
`);
let root = { hello: () => 'Hello world!' };
graphql({
schema,
source: '{ hello }',
root
}).then((response) => {
console.log(response);
});
产生如下报错:Expected undefined to be a GraphQL schema.
以为是依赖没安装,又跑了一次 npm i graphql 依旧如此
再往下翻文档发现,graphql使用的root参数名不对,改正后即可
graphql({
schema,
source: '{ hello }',
rootValue: root // 就是这里!!!
}).then((response) => {
console.log(response);
});