简体   繁体   English

GraphQL运行hello.js出现问题如何解决?

[英]How to solve the problem when running "hello.js" in GraphQL?

I am trying to use GraphQL for the first time today.我今天第一次尝试使用 GraphQL。
I refer to the tutorial at https://graphql.org/code/#javascript to run hello.js .我参考了https://graphql.org/code/#javascript的教程来运行hello.js
After following the tutorial, my program did not run successfully.按照教程操作后,我的程序没有成功运行。 Does anyone know a solution?有谁知道解决方案?
I create the environment as follow:我创建的环境如下:

npm init
npm install graphql

then create hello.js at root directory and copy the official website tutorial:然后在根目录下创建hello.js ,复制官网教程:

var { graphql, buildSchema } = require('graphql');

var schema = buildSchema(`
  type Query {
    hello: String
  }
`);

var root = { hello: () => 'Hello world!' };

graphql(schema, '{ hello }', root).then((response) => {
  console.log(response);
});

Then I input node hello.js , here is the output:然后我输入node hello.js ,这里是 output:

(node:10308) UnhandledPromiseRejectionWarning: Error: Expected undefined to be a GraphQL schema.
    at assertSchema (E:\Node\np\node_modules\graphql\type\schema.js:35:11)
    at validateSchema (E:\Node\np\node_modules\graphql\type\validate.js:34:28)
    at graphqlImpl (E:\Node\np\node_modules\graphql\graphql.js:52:64)
    at E:\Node\np\node_modules\graphql\graphql.js:21:43
    at new Promise (<anonymous>)
    at graphql (E:\Node\np\node_modules\graphql\graphql.js:21:10)
    at Object.<anonymous> (E:\Node\np\hello.js:11:1)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)    at Module.load (internal/modules/cjs/loader.js:937:32)(Use `node --trace-warnings ...` to show where the warning was created)(node:10308) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:10308) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I want to know the reason why the use of graphql fails, and how to fix it, thanks.想知道graphql使用失败的原因,以及如何解决,谢谢。

For me and anyone else looking, looks like they changed things up a bit and not updated their docs...对于我和其他任何人来说,看起来他们改变了一些东西并且没有更新他们的文档......

graphql want one prop passed in not 3, and the naming on them is specific too, root should be rootValue. graphql 是要传1个prop,不是3个,而且他们的命名也很明确,root应该是rootValue。

the hello js should look like this...你好 js 应该看起来像这样......

var { graphql, buildSchema } = require('graphql');

// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
  type Query {
    hello: String
  }
`);

// The rootValue provides a resolver function for each API endpoint
var rootValue = {
  hello: () => {
    return 'Hello world!';
  },
};

// Run the GraphQL query '{ hello }' and print out the response
graphql({
  schema,
  source: '{ hello }',
  rootValue
}).then((response) => {
  console.log(response);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM