我有一个基本的Nestjs-Mongoose-Graphql API,其中定义了两个模式:User
和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);
在EventSchema
中,字段creator
被键入为MongooseSchema.Types.ObjectId
,指向User
我的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);
}
}
事件日期:
@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
字段时,它工作正常,我理解mongoose MongooseSchema.Types.ObjectId
会导致gql模式出现问题...但我找不到合适的方法来修复它。
2条答案
按热度按时间ufj5ltwl1#
它实际上不得不与
creator
字段未填充的事实。变更自
到
修复了我的问题错误信息有点误导。
nr7wwzry2#
确保填充包含要显示的
id
的字段。