Express GraphQl
我正在尝试学习GraphQl
。我没有发现任何好的课程女巫会帮助我学习它。因此,我几乎没有例子开始构建它。在这一刻,当我尝试打开http://127.0.0.0.1:3000/graphql
时,我会遇到错误。它告诉我“消息”:“必须提供查询字符串。”
我认为我的用户查询做错了什么? 这是我的完整代码。有人可以帮忙...
// user type
const UserType = new GraphQLObjectType({
name: "User",
description: "User Type",
fields: () => ({
id: { type: GraphQLInt },
firstname: { type: GraphQLString },
lastname: { type: GraphQLString },
email: { type: GraphQLString },
password: { type: GraphQLString },
}),
});
// register (mutation)
const register = {
type: UserType,
args: {
firstname: { type: GraphQLString },
lastname: { type: GraphQLString },
email: { type: GraphQLString },
password: { type: GraphQLString },
},
//@ts-ignore
async resolve(parent, args) {
const { firstname, lastname, email, password } = args;
const user = new User();
user.firstname = firstname;
user.lastname = lastname;
user.email = email;
user.password = password;
const result = await user.save();
console.log(result);
return result;
},
};
// users (query)
const users = {
// type: new GraphQLList(UserType),
type: UserType,
args: { id: { type: GraphQLInt } },
//@ts-ignore
async resolve(parent, args) {
const users = await User.find();
return users;
},
};
const MutationType = new GraphQLObjectType({
name: "MutationType",
description: "Mutations",
fields: { register },
});
const QueryType = new GraphQLObjectType({
name: "QueryType",
description: "Queries",
fields: { users },
});
const schema = new GraphQLSchema({ query: QueryType, mutation: MutationType });
app.use(
"/graphql",
graphqlHTTP({
schema,
graphiql: true,
})
);
我有2个问题。
- 当我输入浏览器时:
http://127.0.0.1:3000/graphql
不加载。它告诉我加载...并坚持下去。 - 当我在失眠症中尝试它时,它告诉我
{
"errors": [
{
"message": "Must provide query string."
}
]
}
如何修复它?
I'm trying to learn graphql
. I didn't find any good course witch will help me to learn it. So i started building it with little examples. In this moment i'm getting error when i'm trying to open http://127.0.0.1:3000/graphql
. it's telling me "message": "Must provide query string."
I thinks i did something wrong with my users query?
This is my complete code of it. Can someone please help...
// user type
const UserType = new GraphQLObjectType({
name: "User",
description: "User Type",
fields: () => ({
id: { type: GraphQLInt },
firstname: { type: GraphQLString },
lastname: { type: GraphQLString },
email: { type: GraphQLString },
password: { type: GraphQLString },
}),
});
// register (mutation)
const register = {
type: UserType,
args: {
firstname: { type: GraphQLString },
lastname: { type: GraphQLString },
email: { type: GraphQLString },
password: { type: GraphQLString },
},
//@ts-ignore
async resolve(parent, args) {
const { firstname, lastname, email, password } = args;
const user = new User();
user.firstname = firstname;
user.lastname = lastname;
user.email = email;
user.password = password;
const result = await user.save();
console.log(result);
return result;
},
};
// users (query)
const users = {
// type: new GraphQLList(UserType),
type: UserType,
args: { id: { type: GraphQLInt } },
//@ts-ignore
async resolve(parent, args) {
const users = await User.find();
return users;
},
};
const MutationType = new GraphQLObjectType({
name: "MutationType",
description: "Mutations",
fields: { register },
});
const QueryType = new GraphQLObjectType({
name: "QueryType",
description: "Queries",
fields: { users },
});
const schema = new GraphQLSchema({ query: QueryType, mutation: MutationType });
app.use(
"/graphql",
graphqlHTTP({
schema,
graphiql: true,
})
);
I have 2 problems.
- when i type in browser:
http://127.0.0.1:3000/graphql
it's don't loading. it's telling me Loading... and stuck on it. - when i try it in insomnia it's telling me
{
"errors": [
{
"message": "Must provide query string."
}
]
}
how can i fixed it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论