MongoDB,Mongoose和Apollo GraphQL在文档中定义_id类型

问题描述 投票:1回答:1

我在这个项目中使用TypeScript和NestJS:

https://github.com/EricKit/nest-user-auth

我正在尝试将_id属性添加到GraphQL架构:

type User {
  username: String!
  email: String!
  permissions: [String!]!
  createdAt: Date!
  updatedAt: Date!
  enabled: Boolean!
  _id: String!
}

现在,NestJS从此模式为用户生成类型文件

export abstract class User {
    username: string;
    email: string;
    permissions: string[];
    createdAt: Date;
    updatedAt: Date;
    enabled: boolean;
    _id: string;
}

现在的问题是我想为UserDocument创建一个接口,该接口添加mongoDB特定字段并定义文档

export interface UserDocument extends User, Document {
  // Declaring everything that is not in the GraphQL Schema for a User
  _id: string; // TODO: This should actually be Types.ObjectId
  password: string;
  lowercaseUsername: string;
  lowercaseEmail: string;
  passwordReset?: {
    token: string;
    expiration: Date;
  };

  checkPassword(password: string): Promise<boolean>;
}

我无法将_id定义为mongoose.Type.ObjectID,因为它会产生错误:

Interface 'UserDocument' incorrectly extends interface 'User'.
  Types of property '_id' are incompatible.
    Type 'ObjectId' is not assignable to type 'string'.

这是有道理的,我想找到一种方法将它作为ObjectId保存在我的UserDocument架构上,在Schema中保持为String,并且仍然能够扩展GraphQL架构类型。这可能吗?

node.js mongodb mongoose graphql nestjs
1个回答
2
投票

GraphQL方式是自定义标量类型,用于序列化/反序列化ObjectId

GraphQL架构

scalar Date
scalar MongoObjectId
...
type User {
  username: String!
  email: String!
  permissions: [String!]!
  createdAt: Date!
  updatedAt: Date!
  enabled: Boolean!
  _id: MongoObjectId!
}

MongoObjectId标量类,受NestJs Date scalarTypeGraphQL ObjectId scalar的启发

import { Scalar } from '@nestjs/graphql';
import { Kind, ASTNode } from 'graphql';
import { ObjectId } from "mongodb";

@Scalar('MongoObjectId')
export class ObjectIdScalar {
  description = 'Mongo object id scalar type';

  parseValue(value: string) {
    return new ObjectId(value); // value from the client
  }

  serialize(value: ObjectId) {
    return value.toHexString(); // value sent to the client
  }

  parseLiteral(ast: ASTNode) {
    if (ast.kind === Kind.STRING) {
      return new ObjectId(ast.value); // value from the client query
    }
    return null;
  }
}

之后,我们需要注册ObjectIdScalar作为提供者(就像date scalar一样)并将_idstring更改为Type.ObjectID

© www.soinside.com 2019 - 2024. All rights reserved.