GraphQLError:ID无法代表值:< Buffer ...>" [英] GraphQLError: ID cannot represent value: <Buffer...>"

查看:59
本文介绍了GraphQLError:ID无法代表值:< Buffer ...>"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的Nestjs-猫鼬-Graphql api,我定义了两个模式: User Event

 //USER架构@Schema()导出类用户扩展文档{@支柱()用户名:字符串;@Prop({必填:true})密码:字符串;@Prop({必填:true,唯一:true})电子邮件:字符串;@Prop({必填:true,唯一:true})处理:字符串;@支柱()头像:字符串;}export const UserSchema = SchemaFactory.createForClass(User); 

 //EVENT模式@Schema()导出类事件扩展了文档{@支柱({类型:MongooseSchema.Types.ObjectId,参考:User.name,必填:是,})创建者:GqlUser;@Length(5,30)@Prop({必填:true})title:字符串;@Length(5,200)@Prop({必填:true})描述:字符串;@支柱()createdDate:字符串;@支柱()public:布尔值@支柱()active:布尔值;}导出常量EventSchema = SchemaFactory.createForClass(Event); 

EventSchema 中,将 creator 字段键入为 MongooseSchema.Types.ObjectId 指向 User

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

 @Resolver(of => GqlEvent)导出类EventsResolvers {构造函数(私有eventsService:EventsService){}@Query(返回=> [GqlEvent])异步events(){返回this.eventsService.findAll();}@Mutation(返回=> GqlEvent)异步createEvent(@Args('createEventInput')createEventInput:CreateEventDto,){返回this.eventsService.create(createEventInput);}} 

事件Dto:

  @ObjectType()导出类GqlEvent {@Field(类型=> ID)id:字符串;@Field(类型=> GqlUser)创建者:GqlUser;@场地()title:字符串;@场地()描述:字符串;@场地()createdDate:字符串;@场地()public:布尔值@场地()active:布尔值;}@输入类型()导出类CreateEventDto {@Field(类型=> ID)创建者:GqlUser;@场地()@Length(5,30)title:字符串;@场地()@Length(5,200)描述:字符串;@场地()@IsBoolean()public:布尔值} 

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

 #------------------------------------------------------#此文件是自动生成的(请勿修改)#------------------------------------------------------输入GqlUser {id:ID!用户名:字符串!处理:字符串!头像:字符串!电子邮件:字符串!}输入GqlEvent {id:ID!创建者:GqlUser!标题:字符串!描述:字符串!createdDate:字符串!public:布尔值!active:布尔值!}输入查询{事件:[GqlEvent!]!}类型突变{createEvent(createEventInput:CreateEventDto!):GqlEvent!}输入CreateEventDto {创建者:ID!标题:字符串!描述:字符串!public:布尔值!} 

有效方法: createEvent 突变正确地在数据库中插入了一个文档:

  {"_id":{"$ oid":"5f27eacb0393199e3bab31f4"},创建者":{"$ oid":"5f272812107ea863e3d0537b"},"title":"test event",说明":测试说明","public":是的,有效":是的,"createdDate":"2020年8月3日星期一","__v":{"$ numberInt":"0"}} 

我的问题:尝试请求 creator 的子字段时出现以下错误:

Gql查询:

 查询{事件{ID创作者{ID}创建日期上市描述标题积极的}} 

响应:

 错误":[{"message":"ID无法代表值:<缓冲区5f 27 28 12 10 7e a8 63 e3 d0 53 7b>",位置":[{线":6列":7}],路径":["createEvent",创建者","id"],扩展名":{"代码":"INTERNAL_SERVER_ERROR",例外":{"message":"ID无法代表值:<缓冲区5f 27 28 12 10 7e a8 63 e3 d0 53 7b>","stacktrace":["GraphQLError:ID无法代表值:<缓冲区5f 27 28 12 10 7e a8 63 e3 d0 53 7b>",... 

因为当我省略 creator 字段时效果很好,所以我了解到猫鼬 MongooseSchema.Types.ObjectId 会导致gql模式出现问题...但是我找不到修复它的适当方法.提前寻求帮助

解决方案

实际上必须与未填充 creator 字段的事实有关.

更改自

  async findAll():承诺< Event []>{返回this.eventModel.找().exec();} 

  async findAll():承诺< Event []>{返回this.eventModel.找().populate('creator').exec();} 

解决了我的问题.该错误消息有点令人误解.

I have a basic Nestjs - Mongoose - Graphql api with I have two schemas defined: User and Event

//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);

In the EventSchema, field creator is typed as MongooseSchema.Types.ObjectId pointing at User

My events.resolvers.ts looks like this:


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

event 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;
}

With such, Nestjs generates following gql schema (I skip the parts related to user CRUD for clarity) :

# ------------------------------------------------------
# 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!
}

What works : the createEvent mutation correctly inserts a document in database :

{
"_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"}
}

My problem : I have the following error when I try to request subfields of creator :

Gql query :

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

Response :

"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>",...

As it works fine when I omit creator field, I understand the mongoose MongooseSchema.Types.ObjectId causes problem with gql schema... but I could not find the appropriate way to fix it. Thx in advance for help

解决方案

It actually had to to with the fact that creator field was not populated.

Changing from

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

to

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

Fixed my problem. The error message was kind of misleading.

这篇关于GraphQLError:ID无法代表值:&lt; Buffer ...&gt;&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆