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

时间:2019-03-09 15:57:56

标签: node.js mongodb mongoose graphql nestjs

我在此项目上将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'.

这很有意义,我想找到一种方法将其保留为UserDocument架构上的ObjectId,将其保留为架构中的String,并且仍然能够扩展GraphQL架构类型。这可能吗?

1 个答案:

答案 0 :(得分: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