获取错误 {"errors":[{"message":"Must provide query string."}]}

Getting error {"errors":[{"message":"Must provide query string."}]}

我的 app.js

里有这个
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(
  '/myproject',
  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;
      },
    },
    graphql: true,
  })
);

app.listen(3000);

当我在浏览器中输入 'http://localhost:3000/myproject' 时出现此错误:

{"errors":[{"message":"Must provide query string."}]}

我哪里错了? 在我的项目中,我所做的唯一更改是 app.js。我没有前端。 谢谢

首先,我们应该将端口 4000 与 graphql 端点一起使用,并且 app.use("/graphql", ... 你得到了。错误,因为您没有正确加载 graphiql 游乐场!只需将 graphql: true, 替换为 graphiql: true, ?

这对我有用 http://127.0.0.1:4000/graphql :

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