我试着学习GraphQL。我创建了一个项目,我做了此页说的。
安装
npm init
npm install graphql --saveServer.js
var { graphql, buildSchema } = require('graphql');
// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
type Query {
hello: String
}
`);
// The root provides a resolver function for each API endpoint
var root = {
hello: () => {
return 'Hello world!';
},
};
// Run the GraphQL query '{ hello }' and print out the response
graphql(schema, '{ hello }', root).then((response) => {
console.log(response);
});Run
node server.js这会给我一个错误。
throw new Error(
^
Error: Expected undefined to be a GraphQL schema.
at assertSchema (C:\Users\BK\Projects\Test\graphql-test\node_modules\graphql\type\schema.js:35:11)
at validateSchema (C:\Users\BK\Projects\Test\graphql-test\node_modules\graphql\type\validate.js:34:28)
at graphqlImpl (C:\Users\BK\Projects\Test\graphql-test\node_modules\graphql\graphql.js:52:64)
at C:\Users\BK\Projects\Test\graphql-test\node_modules\graphql\graphql.js:21:43
at new Promise (<anonymous>)
at graphql (C:\Users\BK\Projects\Test\graphql-test\node_modules\graphql\graphql.js:21:10)
at Object.<anonymous> (C:\Users\BK\Projects\Test\graphql-test\server.js:18:1)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)版本
发布于 2021-11-19 17:29:52
我遵循同样的教程,我也遇到了同样的问题。按照U Rogel的建议,进入下一页后,我遇到了以下问题:
Could not resolve dependency:
npm ERR! peer graphql@"^14.7.0 || ^15.3.0" from express-graphql@0.12.0
npm ERR! node_modules/express-graphql因此,我决定修改package.json中的graphql版本
"dependencies": {
"express": "^4.17.1",
"express-graphql": "^0.12.0",
"graphql": "^14.7.0"
}然后我运行npm install,回到第一页,它成功了!
我认为问题在于16+版本与基本示例不兼容。
https://stackoverflow.com/questions/69923984
复制相似问题