【发布时间】:2019-05-23 17:25:00
【问题描述】:
我从 express 上的 graphql 开始。我收到错误“必须提供查询字符串”。当我访问它的路线时。
这是一个快速应用程序。这是我的 app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
let bodyParser = require('body-parser');
var cors = require('cors');
const graphqlHTTP = require('express-graphql');
const MySchema = require('./schema/schema');
app.use(cors());
app.use(logger('dev'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/graphql', graphqlHTTP({
schema: MySchema,
graphiql: true,
}));
我的shema是:
const graphql = require('graphql');
const {
GraphQLObjectType,
GraphQLString,
GraphQLSchema,
GraphQLList
} = graphql;
//MODELS FROM MONGOOSE
const Entidad = require('../models/entidad');
//TYPES GRAPHQL
const EntidadType = new GraphQLObjectType({
name: 'Entidad',
fields : () => ({
id : {type : GraphQLString},
nombre : {type : GraphQLString},
created_at : { type : GraphQLString},
updated_at : { type : GraphQLString},
})
});
//ROOT QUERY
const RootQuery = new GraphQLObjectType({
name : 'RootQueryType',
fields : {
entidad : {
type: EntidadType,
args: {id:{type: GraphQLString}},
resolve(parent, args){
//code to get data from db
return Entidad.findById(args.id);
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery
});
我不知道为什么打开 graphiql 时出现错误。
我测试了其他示例并给了我同样的错误,也许我错过了某些部分......
有什么想法吗?
【问题讨论】:
-
您是否在服务器上使用其他中间件?如果是这样,那么查看该代码也会有所帮助,因为像
body-parser这样的中间件可能会混淆express-graphql的中间件中的逻辑。 -
好的,我更新了帖子,现在你可以看到中间件了。我正在使用 body-parser
-
现在我注释行 app.use(bodyParser.urlencoded({extended: true}));我在文档资源管理器上看到了根查询,但现在我收到错误 { "errors": [ { "message": "Syntax Error: Unexpected
", "locations": [ { "line": 29, “列”:1 } ] } ] } -
您好@DanielRearden,当我尝试使用 AWS lambda 函数访问 graphql 时,我遇到了同样的错误。
标签: javascript html schema graphql express-graphql