GraphQLError: ID 不能表示值: <Buffer...>"



我有一个基本的Nestjs-Mongoose-Graphqlapi,其中定义了两个模式:UserEvent

//USER Schema
@Schema()
export class User extends Document {
@Prop()
username: string;
@Prop({ required: true })
password: string;
@Prop({ required: true, unique: true })
email: string;
@Prop({ required: true, unique: true })
handle: string;
@Prop()
avatar: string;
}
export const UserSchema = SchemaFactory.createForClass(User);
//EVENT schema
@Schema()
export class Event extends Document {
@Prop({
type: MongooseSchema.Types.ObjectId,
ref: User.name,
required: true,
})
creator: GqlUser;
@Length(5, 30)
@Prop({ required: true })
title: string;
@Length(5, 200)
@Prop({ required: true })
description: string;
@Prop()
createdDate: string;
@Prop()
public: boolean;
@Prop()
active: boolean;
}
export const EventSchema = SchemaFactory.createForClass(Event);

EventSchema中,字段creator被键入为指向UserMongooseSchema.Types.ObjectId

我的events.resolvers.ts看起来像这样:


@Resolver(of => GqlEvent)
export class EventsResolvers {
constructor(private eventsService: EventsService) {}
@Query(returns => [GqlEvent])
async events() {
return this.eventsService.findAll();
}
@Mutation(returns => GqlEvent)
async createEvent(
@Args('createEventInput') createEventInput: CreateEventDto,
) {
return this.eventsService.create(createEventInput);
}
}

事件Dtos:

@ObjectType()
export class GqlEvent {
@Field(type => ID)
id: string;
@Field(type => GqlUser)
creator: GqlUser;
@Field()
title: string;
@Field()
description: string;
@Field()
createdDate: string;
@Field()
public: boolean;
@Field()
active: boolean;
}
@InputType()
export class CreateEventDto {
@Field(type => ID)
creator: GqlUser;
@Field()
@Length(5, 30)
title: string;
@Field()
@Length(5, 200)
description: string;
@Field()
@IsBoolean()
public: boolean;
}

这样,Nestjs生成以下gql模式(为了清楚起见,我跳过了与用户CRUD相关的部分(:

# ------------------------------------------------------
# THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
# ------------------------------------------------------
type GqlUser {
id: ID!
username: String!
handle: String!
avatar: String!
email: String!
}
type GqlEvent {
id: ID!
creator: GqlUser!
title: String!
description: String!
createdDate: String!
public: Boolean!
active: Boolean!
}
type Query {
events: [GqlEvent!]!
}
type Mutation {
createEvent(createEventInput: CreateEventDto!): GqlEvent!
}

input CreateEventDto {
creator: ID!
title: String!
description: String!
public: Boolean!
}

工作原理:createEvent突变在数据库中正确插入文档:

{
"_id":{"$oid":"5f27eacb0393199e3bab31f4"},
"creator":{"$oid":"5f272812107ea863e3d0537b"},
"title":"test event",
"description":"a test description",
"public":true,
"active":true,
"createdDate":"Mon Aug 03 2020",
"__v":{"$numberInt":"0"}
}

我的问题:当我尝试请求creator的子字段时,我有以下错误:

Gql查询:

query {
events {
id
creator {
id
}
createdDate
public
description
title
active
}
}

响应:

"errors": [
{
"message": "ID cannot represent value: <Buffer 5f 27 28 12 10 7e a8 63 e3 d0 53 7b>",
"locations": [
{
"line": 6,
"column": 7
}
],
"path": [
"createEvent",
"creator",
"id"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"message": "ID cannot represent value: <Buffer 5f 27 28 12 10 7e a8 63 e3 d0 53 7b>",
"stacktrace": [
"GraphQLError: ID cannot represent value: <Buffer 5f 27 28 12 10 7e a8 63 e3 d0 53 7b>",...

当我省略creator字段时,它工作得很好,我理解mongooseMongooseSchema.Types.ObjectId会导致gql模式的问题。。。但我找不到合适的方法来修复它。提前向寻求帮助

事实上,creator字段没有填充。

从更改

async findAll(): Promise<Event[]> {
return this.eventModel
.find()
.exec();
}

async findAll(): Promise<Event[]> {
return this.eventModel
.find()
.populate('creator')
.exec();
}

解决了我的问题。错误信息有点误导人。

确保填充包含要显示的id的字段。

最新更新