是否有可能在graphql / express / node服务器端实现分页?

时间:2020-04-17 20:09:06

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

客户端(react / apollo)上的

没有得到任何分页查询,返回了错误:

“ message”:“无法查询字段\” cursor \“

“ message”:“未知参数\” last \“

“消息”:“无法查询字段\” 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);

Shema:

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);
      },
    },
  },
});

1 个答案:

答案 0 :(得分:1)

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

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

相关问题