Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 获取错误{quot;errors";:[{quot;message";:“必须提供查询字符串。”_Node.js_Reactjs_Graphql_Graphql Js_Express Graphql - Fatal编程技术网

Node.js 获取错误{quot;errors";:[{quot;message";:“必须提供查询字符串。”

Node.js 获取错误{quot;errors";:[{quot;message";:“必须提供查询字符串。”,node.js,reactjs,graphql,graphql-js,express-graphql,Node.js,Reactjs,Graphql,Graphql Js,Express Graphql,我的app.js中有这个 const express=require('express'); const bodyParser=require('body-parser'); 常量graphqlHttp=require('express-graphql')。graphqlHttp; const{buildSchema}=require('graphql'); 常量app=express(); use(bodyParser.json()); 应用程序使用( “/myproject”, graphq

我的app.js中有这个

const express=require('express');
const bodyParser=require('body-parser');
常量graphqlHttp=require('express-graphql')。graphqlHttp;
const{buildSchema}=require('graphql');
常量app=express();
use(bodyParser.json());
应用程序使用(
“/myproject”,
graphqlHttp({
模式:构建模式(`
类型查询{
事件:[字符串!
}
类型突变{
createEvent(名称:String):String
}
模式{
查询:TypeQuery
突变:类型突变
}
`),
根值:{
事件:()=>{
返回[‘猫’、‘航行’];
},
createEvent:(参数)=>{
const eventName=args.name;
返回事件名;
},
},
graphql:对,
})
);
app.listen(3000);
当我输入浏览器的http://localhost:3000/myproject'我收到这个错误:

{"errors":[{"message":"Must provide query string."}]}
我错了什么? 在我的项目中,我所做的唯一更改是在app.js中。我没有前端。
谢谢

首先,我们应该将端口4000与graphql端点一起使用,并使用app.use(“/graphql”)。。。 您收到了.error,因为您没有正确加载graphiql!只需将
graphiql:true,
替换为
graphiql:true,

这对我有用,使用:

const express = require("express");
const bodyParser = require("body-parser");
const graphqlHttp = require("express-graphql").graphqlHTTP;
const { buildSchema } = require("graphql");

const app = express();

app.use(bodyParser.json());

app.use(
  "/graphql",
  graphqlHttp({
    schema: buildSchema(`
      type TypeQuery {
        events: [String!]!
      }

      type TypeMutation {
        createEvent(name: String): String
      }

      schema {
        query: TypeQuery
        mutation: TypeMutation
      }
    `),
    rootValue: {
      events: () => {
        return ["Cats", "Sailing"];
      },
      createEvent: (args) => {
        const eventName = args.name;
        return eventName;
      },
    },
    graphiql: true,
  })
);

app.listen(4000, () => {
  console.log("server started on port 4000");
});