无法获取 graphql 服务器
cant fetch graphql server
你好,我遵循了 GraphQL 的教程,这是我到目前为止编写的代码:
const express = require('express');
const {graphqlHTTP} = require('express-graphql');
const graphql = require('graphql')
const {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLList,
} = graphql;
const users = require('./users.json')
const app = express();
const userType = new GraphQLObjectType({
name:'User',
fields: () => ({
email: { type: GraphQLString},
username: { type: GraphQLString},
name: { type: GraphQLString},
company: { type: GraphQLString},
address: { type: GraphQLString},
})
})
const RootQuery = new GraphQLObjectType({
name:'RootQueryType',
fields: () => ({
getAllUsers: {
type: new GraphQLList(userType),
resolve(parent,args){
return users
}
}
})
})
const Mutation = new GraphQLObjectType({
name:"mutation",
fields: () => ({
createUser: {
type: userType,
args:{
email: { type: GraphQLString},
username: { type: GraphQLString},
name: { type: GraphQLString},
company: { type: GraphQLString},
address: { type: GraphQLString},
},
resolve(parent,args){
users.push({ email: args.email,
username: args.usename,
name: args.name,
company: args.company,
adress: args,company,
})
return args
}
}
})
})
const schema = new GraphQLSchema({query: RootQuery, mutation: Mutation})
app.use('/graphql', graphqlHTTP({
schema:schema,
graphql:true
}))
app.listen(5000, ()=>{
console.log('server started')
})
但是当我在浏览器中访问 localhost:5000/graphql 时,我得到:
{"errors":[{"message":"必须提供查询字符串。"}]}
我尝试检查 graphqlstring 是否正确导入,一切正常
我搜索了很多,我希望答案在这里。
你能按照here用graphiql: true, ?
替换graphql: true,
吗
通常也尝试将端口设置为 4000
你好,我遵循了 GraphQL 的教程,这是我到目前为止编写的代码:
const express = require('express');
const {graphqlHTTP} = require('express-graphql');
const graphql = require('graphql')
const {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLList,
} = graphql;
const users = require('./users.json')
const app = express();
const userType = new GraphQLObjectType({
name:'User',
fields: () => ({
email: { type: GraphQLString},
username: { type: GraphQLString},
name: { type: GraphQLString},
company: { type: GraphQLString},
address: { type: GraphQLString},
})
})
const RootQuery = new GraphQLObjectType({
name:'RootQueryType',
fields: () => ({
getAllUsers: {
type: new GraphQLList(userType),
resolve(parent,args){
return users
}
}
})
})
const Mutation = new GraphQLObjectType({
name:"mutation",
fields: () => ({
createUser: {
type: userType,
args:{
email: { type: GraphQLString},
username: { type: GraphQLString},
name: { type: GraphQLString},
company: { type: GraphQLString},
address: { type: GraphQLString},
},
resolve(parent,args){
users.push({ email: args.email,
username: args.usename,
name: args.name,
company: args.company,
adress: args,company,
})
return args
}
}
})
})
const schema = new GraphQLSchema({query: RootQuery, mutation: Mutation})
app.use('/graphql', graphqlHTTP({
schema:schema,
graphql:true
}))
app.listen(5000, ()=>{
console.log('server started')
})
但是当我在浏览器中访问 localhost:5000/graphql 时,我得到: {"errors":[{"message":"必须提供查询字符串。"}]} 我尝试检查 graphqlstring 是否正确导入,一切正常 我搜索了很多,我希望答案在这里。
你能按照here用graphiql: true, ?
替换graphql: true,
吗
通常也尝试将端口设置为 4000