const express = require('express');
const {graphqlHTTP} = require('express-graphql');
const schema = require('./schema/schema');
const app = express();
app.get('/graphql', graphqlHTTP({
graphiql:true,
schema: schema
}))
app.listen(4000,()=>{
console.log("listining to port 4000..")
})
const graphql = require('graphql');
const{
GraphQLObjectType,
GraphQLID,
GraphQLString,
GraphQLInt,
GraphQLSchema
} = graphql
const UserType = new GraphQLObjectType({
name: 'user',
description: 'Documentation for User...',
fields: ()=>({
id: {type: GraphQLString},
name: {type: GraphQLString},
age: {type: GraphQLInt}
})
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
description: 'description',
fields: {
user: {
type: UserType,
args: {id: {type: GraphQLString}},
resolve(parent,args){
let user = {
id: '345',
age: 34,
name: 'Aman'
}
return user;
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery
})
以上是我的代码,我得到了这个错误:
";消息":"意外的标记<在位置0"的JSON中
"堆叠":"SyntaxError:意外的标记<在位置0"的JSON中
有人能告诉我我是如何解决这个错误的吗?提前谢谢。
您在客户端应用程序上似乎看到了这种错误?顺便说一句,你没有添加太多细节来说明你正在做什么来得到这个错误,你在哪里看到它,以及你认为是什么导致了它,所以我只能从你的设置角度来分析你提供的内容
这看起来像是从您期望JSON的服务器上得到了HTML响应。通常,对于GraphQL,这可能是因为您发送的是GET请求(可能会打开GraphiQL资源管理器(,而不是POST请求。
GraphQL客户端代码应该发出POST请求,并且应该确保它发送一个头来接受application/json
响应。