I have a simple express graphql server:
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`);
});
And my request looks like:
Any help?
(Please don't close it as duplicate because the other post does not provide enough info on how the user solved it)
I have a simple express graphql server:
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`);
});
And my request looks like:
Any help?
(Please don't close it as duplicate because the other post does not provide enough info on how the user solved it)
Share Improve this question asked Aug 6, 2017 at 9:34 Avraam MavridisAvraam Mavridis 8,94022 gold badges87 silver badges135 bronze badges 3-
Try replacing
schema,
withschema: schema,
– user5734311 Commented Aug 6, 2017 at 9:37 - @ChrisG it would have throw Syntax error, if it didnt understand the syntax. – Avraam Mavridis Commented Aug 6, 2017 at 9:40
-
@ChrisG In ES6
{ schema }
is equivalent to{ schema: schema }
. It's syntactic sugar! Good eye, though: this looks like a bug. – Ziggy Commented Oct 10, 2017 at 3:51
1 Answer
Reset to default 4You need to specify application/json
in your Content-Type header -- you currently have text/plain
. You've included the body parser middleware on the server, but it relies on that header in your request to know when it needs to actually parse the response into JSON.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745354687a4624045.html
评论列表(0条)