【问题标题】:is it possible an implementation pagination over graphql/express/node server side?是否可以在 graphql/express/node 服务器端实现分页?
【发布时间】:2020-07-31 10:34:44
【问题描述】:

在客户端(react/apollo)没有收到任何分页查询,正在返回错误:

"message": "无法查询字段\"cursor\"

"message": "未知参数\"last\"

"message": "无法查询字段\"pageInfo\"

"message": "未知参数\"page\" on

这就是我在服务器端(node/express/mongoogse)执行模型、查询和突变模式的方式:

型号:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const userSchema = new Schema({
  identification: { type: String, trim: true, required: true, unique: true },
  name: { type: String, trim: true, required: true },
  lastName: { type: String, trim: true, required: true },
  status: { type: String, trim: true, required: true },
  username: { type: String, trim: true, required: true, unique: true },
  password: { type: String, required: true },
  roleid: { type: String, trim: true, required: true },
  description: { type: String, trim: true },
  email: { type: String, trim: true, unique: true }
});

module.exports = mongoose.model("User", userSchema);

舍玛:

const UserType = new GraphQLObjectType({
  name: "User",
  fields: () => ({
    id: { type: GraphQLID },
    identification: { type: GraphQLID },
    name: { type: GraphQLString },
    lastName: { type: GraphQLString },
    status: { type: GraphQLString },
    username: { type: GraphQLString },
    password: { type: GraphQLString },
    description: { type: GraphQLString },
    email: { type: GraphQLString },
    roleid: { type: GraphQLID },
    role: {
      type: RoleType,
      resolve(parent, args) {
        return Role.findById(parent.roleid);
      },
    },
  }),
});

const RootQuery = new GraphQLObjectType({
  name: "RootQueryType",
  fields: {
    users: {
      type: new GraphQLList(UserType),
      resolve(parent, args) {
        return User.find();
      },
    },
    user: {
      type: UserType,
      args: { id: { type: GraphQLID } },
      resolve(parent, args) {
        return User.findById(args.id);
      },
    },
  },
});


const Mutation = new GraphQLObjectType({
  name: "Mutation",
  fields: {
    addUser: {
      type: UserType,
      args: {
        identification: { type: GraphQLString },
        name: { type: GraphQLString },
        lastName: { type: GraphQLString },
        status: { type: GraphQLString },
        username: { type: GraphQLString },
        password: { type: GraphQLString },
        description: { type: GraphQLString },
        email: { type: GraphQLString },
        roleid: { type: GraphQLID },
      },
      resolve(parent, args) {
        return bcrypt
          .hash(args.password, 12)
          .then((hashedPassword) => {
            let user = new User({
              identification: args.identification,
              name: args.name,
              lastName: args.lastName,
              status: args.status,
              username: args.username,
              password: hashedPassword,
              description: args.description,
              email: args.email,
              roleid: args.roleid,
            });
            return user.save();
          })
          .then((result) => {
            return { ...result._doc, password: null, id: result.id };
          })
          .catch((err) => {
            throw err;
          });
      },
    },
    updateUser: {
      type: UserType,
      args: {
        id: { type: GraphQLID },
        identification: { type: GraphQLString },
        name: { type: GraphQLString },
        lastName: { type: GraphQLString },
        status: { type: GraphQLString },
        username: { type: GraphQLString },
        password: { type: GraphQLString },
        description: { type: GraphQLString },
        email: { type: GraphQLString },
        roleid: { type: GraphQLID },
      },
      resolve(parent, args) {
        return bcrypt
          .hash(args.password, 12)
          .then((hashedPassword) => {
            let user = User.findByIdAndUpdate(
              args.id,
              {
                identification: args.identification,
                name: args.name,
                lastName: args.lastName,
                status: args.status,
                username: args.username,
                password: hashedPassword,
                description: args.description,
                email: args.email,
                roleid: args.roleidupduu,
              },
              { new: true }
            );
            return user;
          })
          .then((result) => {
            return { ...result._doc, password: null, id: result.id };
          })
          .catch((err) => {
            throw err;
          });
      },
    },
    deleteUser: {
      type: UserType,
      args: { id: { type: GraphQLID } },
      resolve(parent, args) {
        return User.findByIdAndDelete(args.id);
      },
    },
  },
});

【问题讨论】:

    标签: node.js reactjs express graphql apollo-client


    【解决方案1】:

    必须先在架构中定义字段,然后才能在查询中请求它们。参数必须先在模式中定义,然后才能在查询中使用。 GraphQL 没有内置的分页方式——您必须自己添加适当的类型和参数。

    如果您正在尝试构建中继服务器,您可能会发现使用the official library 很有帮助。如果您构建中继服务器,请记住您不必实现基于光标的分页或连接类型。如果您刚开始使用 GraphQL,您可能希望先让简单的基于偏移量的分页工作,然后再处理更复杂的模式设计模式。

    【讨论】:

      猜你喜欢
      • 2011-05-10
      • 1970-01-01
      • 2020-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多