簡體   English   中英

GraphQL 教程 - 預期未定義為 GraphQL 模式

[英]GraphQL Tutorial - Expected undefined to be a GraphQL schema

我嘗試學習 GraphQL。 我創建了一個項目,並按照此頁面的說明進行操作。

安裝

   npm init
   npm install graphql --save

服務器.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);
});

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)

版本

  • graphql:“^16.0.1”
  • 節點:v16.13.0

我正在學習相同的教程,但遇到了同樣的問題。 轉到下一頁后,正如 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+ 版本不適用於基本示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM