Graphql SyntaxError:意外标记<;在Graphiql中测试时,在JSON的位置0处
我正在学习GraphQL,并且是这项技术的新手。我无法找出这个语法错误的原因。当我在graphiql上测试它时,它会抛出一个意外的令牌语法错误 这是我的server.js:Graphql SyntaxError:意外标记<;在Graphiql中测试时,在JSON的位置0处,graphql,express-graphql,Graphql,Express Graphql,我正在学习GraphQL,并且是这项技术的新手。我无法找出这个语法错误的原因。当我在graphiql上测试它时,它会抛出一个意外的令牌语法错误 这是我的server.js: const express = require("express"); const graphqlHTTP = require("express-graphql"); const schema = require("./schema"); const app = express(); app.get( "/graphq
const express = require("express");
const graphqlHTTP = require("express-graphql");
const schema = require("./schema");
const app = express();
app.get(
"/graphql",
graphqlHTTP({
schema: schema,
graphiql: true
})
);
app.listen(4000, () => {
console.log("Server listening to port 4000...");
});
这是我的模式:
const {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLSchema,
GraphQLList,
GraphQLNotNull
} = require("graphql");
// HARD CODED DATA
const customers = [
{ id: "1", name: "John Doe", email: "jdoe@gmail.com", age: 35 },
{ id: "2", name: "Kelly James", email: "kellyjames@gmail.com", age: 28 },
{ id: "3", name: "Skinny Pete", email: "skinnypete@gmail.com", age: 31 }
];
// CUSTOMER TYPE
const CustomerType = new GraphQLObjectType({
name: "Customer",
fields: () => ({
id: { type: GraphQLString },
name: { type: GraphQLString },
email: { type: GraphQLString },
age: { type: GraphQLInt }
})
});
// ROOT QUERY
const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: {
customer: {
type: CustomerType,
args: {
id: { type: GraphQLString }
},
resolve(parentValue, args) {
for (let i = 0; i < customers.length; i++) {
if (customers[i].id == args.id) {
return customers[i];
}
}
}
},
customers: {
type: new GraphQLList(CustomerType),
resolve(parentValue, args) {
return customers;
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery
});
const{
GraphQLObjectType,
图形字符串,
GraphQLInt,
图形模式,
图表列表,
GraphQLNotNull
}=需要(“graphql”);
//硬编码数据
常数客户=[
{id:“1”,姓名:“John Doe”,电子邮件:jdoe@gmail.com“,年龄:35},
{id:“2”,姓名:“Kelly James”,电子邮件:kellyjames@gmail.com“,年龄:28},
{id:“3”,姓名:“瘦皮特”,电子邮件:skinnypete@gmail.com“,年龄:31}
];
//客户类型
const CustomerType=新的GraphQLObjectType({
名称:“客户”,
字段:()=>({
id:{type:GraphQLString},
名称:{type:GraphQLString},
电子邮件:{type:GraphQLString},
年龄:{type:GraphQLInt}
})
});
//根查询
const RootQuery=新的GraphQLObjectType({
名称:“RootQueryType”,
字段:{
客户:{
类型:CustomerType,
args:{
id:{type:GraphQLString}
},
解析(parentValue,args){
for(设i=0;i
有人能给我指出正确的方向吗?我无法在这里找出问题所在?根据
快速中间件
的文档,您应该使用app.use
安装中间件,而不是app.get
:
app.use('/graphql', graphqlHTTP({schema, graphiql: true}))
这样做将使GraphiQL在浏览器中可用,但也允许您向
/graphql
端点发出POST
请求。通过使用app.get
,您可以访问GraphiQL界面,但实际上无法发出POST
请求。当您在GraphiQL中发出请求时,它会尝试向您的端点发出POST
请求,但由于您的应用程序未配置为接收请求,因此请求失败。您看到的错误是尝试将丢失路由的通用错误express抛出解析为JSON的结果。在我的情况下,此错误消息是由愚蠢的错误引起的,因此我的故事可能对某些人有用:
我没有将JSON值设置为{“query”:“graphqlQueryHere…”
我只是发布了普通的graphQL查询而不是JSON。请看一看,你没有这样做