2017-08-06 85 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。

相關問題