【问题标题】:How to solve the problem when running "hello.js" in GraphQL?在 GraphQL 中运行“hello.js”时如何解决问题?
【发布时间】:2022-02-20 19:06:53
【问题描述】:

我今天第一次尝试使用 GraphQL。
我参考https://graphql.org/code/#javascript 的教程来运行hello.js
按照教程完成后,我的程序没有成功运行。有谁知道解决方案?
我创建的环境如下:

npm init
npm install graphql

然后在根目录创建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);
});

然后我输入node hello.js,输出如下:

(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.

想知道graphql使用失败的原因,以及如何解决,谢谢。

【问题讨论】:

  • stackoverflow.com/questions/53192184/… 能解决您的问题吗?
  • 我看了这篇文章,用apollo server可以成功运行,但是我很想知道为什么最简单的方法失败了。困惑。

标签: javascript node.js graphql


【解决方案1】:

对于我和其他任何人来说,看起来他们做了一些改动并且没有更新他们的文档...

graphql 想要传入一个 prop 不是 3,而且它们的命名也是特定的,root 应该是 rootValue。

hello 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);
});

【讨论】:

    猜你喜欢
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2019-02-15
    • 1970-01-01
    相关资源
    最近更新 更多