I try to learn GraphQL. I created a project and I did what this page says.
Install
npm init
npm install graphql --save
Server.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
This returns me an error.
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)
Versions
I was following the same tutorial and I had the same problem. After going to the next page, as U Rogel suggested, I had the following problem:
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
So, I decided to modify my graphql version in the package.json
"dependencies": {
"express": "^4.17.1",
"express-graphql": "^0.12.0",
"graphql": "^14.7.0"
}
Then I ran npm install, went back to the first page, and I worked!
I think the problem is that version 16+ does not work with the basic examples.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.