Express GraphQl

发布于 2025-02-10 13:40:23 字数 2021 浏览 1 评论 0原文

我正在尝试学习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个问题。

  1. 当我输入浏览器时:http://127.0.0.1:3000/graphql不加载。它告诉我加载...并坚持下去。
  2. 当我在失眠症中尝试它时,它告诉我
{
  "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.

  1. 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.
  2. when i try it in insomnia it's telling me
{
  "errors": [
    {
      "message": "Must provide query string."
    }
  ]
}

how can i fixed it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文