【问题标题】:Graphql: Must provide query stringGraphql:必须提供查询字符串
【发布时间】:2018-01-13 19:09:45
【问题描述】:

我有一个简单的 express graphql 服务器:

const schema = require('./schema');
const express = require('express');
const graphqlHTTP = require('express-graphql');
const cors = require('cors')
const bodyParser = require('body-parser');


const app = express();
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use('/graphql', graphqlHTTP(req => {
  return ({
  schema,
  pretty: true,
  })
}));

const server = app.listen(9000, err => {
  if (err) { return err; }
  console.log(`GraphQL server running on http://localhost:${9000}/graphql`);
});

我的请求看起来像:

有什么帮助吗?

(请不要将其关闭为重复,因为其他帖子没有提供有关用户如何解决它的足够信息)

【问题讨论】:

  • 尝试用schema: schema,替换schema,
  • @ChrisG 如果它不理解语法,它会抛出语法错误。
  • @ChrisG 在 ES6 中 { schema } 等价于 { schema: schema }。这是语法糖!不过眼光不错:这看起来像个错误。

标签: javascript node.js express graphql relay


【解决方案1】:

您需要在 Content-Type 标头中指定 application/json -- 您目前有 text/plain。您已在服务器上包含正文解析器中间件,但它依赖于您的请求中的该标头来了解何时需要将响应实际解析为 JSON。

【讨论】:

  • 你是怎么做到的?
猜你喜欢
  • 2018-12-30
  • 2019-06-02
  • 2019-06-09
  • 2016-08-10
  • 2022-01-18
  • 2020-01-26
  • 2017-10-26
  • 2019-05-23
  • 2020-01-27
相关资源
最近更新 更多