2017-08-06 53 views
1

我有一个简单的快递graphql服务器: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`); 
}); 

而且我的要求是这样的:

enter image description here

任何帮助吗?

(请不要关闭它的重复,因为其他职位并不提供关于用户如何解决它足够的信息)

+0

尝试用'schema:schema'替换'schema',' –

+0

@ChrisG如果它不理解语法,它会抛出语法错误。 –

+0

@ChrisG在ES6中'{schema}'相当于'{schema:schema}'。这是句法糖!好眼睛,但这看起来像一个bug。 – Ziggy

回答

0

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

相关问题