Estoy tratando de usar GraphQL por primera vez hoy.
Me refiero al tutorial en https://graphql.org/code/#javascript para ejecutar hello.js
.
Después de seguir el tutorial, mi programa no se ejecutó correctamente. ¿Alguien sabe alguna solución?
Creo el entorno de la siguiente manera:
npm init npm install graphql
luego cree hello.js
en el directorio raíz y copie el tutorial del sitio web oficial:
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); });
Luego ingreso node hello.js
, aquí está el resultado:
(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.
Quiero saber el motivo por el cual falla el uso de graphql
, y como solucionarlo, gracias.
Para mí y para cualquier otra persona que busque, parece que cambiaron un poco las cosas y no actualizaron sus documentos...
graphql quiere que se pase un accesorio en no 3, y el nombre en ellos también es específico, la raíz debe ser rootValue.
el hola js debería verse así...
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); });