无法返回无效字段 - typegoose和typegraphql
我有使用TypeGoose和typeGraphQl设置此专辑模型,其中包含来自song
使用COMEALSONG
Objecttype的多首歌曲:
import {
prop as Property,
getModelForClass,
modelOptions,
Ref,
} from "@typegoose/typegoose";
import { Field, ObjectType, ID } from "type-graphql";
import { AlbumCategory, } from "./albumCategory.model";
import { Song } from "./song.model";
@ObjectType()
export class AlbumSong {
@Field(() => ID)
@Property({ required: true })
id!: string;
@Field(() => Song)
@Property({ type: () => Song, ref: () => Song, required: true })
song!: Song;
}
@ObjectType({ description: "The Album Model" })
@modelOptions({ schemaOptions: { collection: "albums", timestamps: true } })
export class Album {
@Field(() => ID)
id: string;
@Field()
@Property({ type: () => String })
title: string;
@Field(() => [AlbumSong])
@Property({ type: AlbumSong })
albumSongs!: Partial<AlbumSong>[];
@Field()
@Property({ required: true, default: Date.now })
createdAt: Date;
@Field()
@Property({ required: true, default: Date.now })
updatedAt: Date;
}
export const AlbumModel = getModelForClass(Album);
尝试使用
@Query((_returns) => Album, { nullable: false, name: "album" })
async getAlbumById(@Arg("id") id: string) {
return await AlbumModel.findById({ _id: id });
}
以下方式 查询专辑时GraphQl:
query Album($albumId: String!) {
album(id: $albumId) {
id
albumSongs {
id
song {
id
}
}
}
}
我获取:“无法返回null for nullable field Compulsong.song.song。”
给我看来,参考似乎不起作用,当我仅查询专辑杂志的id 它返回很好...
I have this Album model set up using Typegoose and TypeGraphQL, which contains multiple songs from the Song
model using the AlbumSong
ObjectType:
import {
prop as Property,
getModelForClass,
modelOptions,
Ref,
} from "@typegoose/typegoose";
import { Field, ObjectType, ID } from "type-graphql";
import { AlbumCategory, } from "./albumCategory.model";
import { Song } from "./song.model";
@ObjectType()
export class AlbumSong {
@Field(() => ID)
@Property({ required: true })
id!: string;
@Field(() => Song)
@Property({ type: () => Song, ref: () => Song, required: true })
song!: Song;
}
@ObjectType({ description: "The Album Model" })
@modelOptions({ schemaOptions: { collection: "albums", timestamps: true } })
export class Album {
@Field(() => ID)
id: string;
@Field()
@Property({ type: () => String })
title: string;
@Field(() => [AlbumSong])
@Property({ type: AlbumSong })
albumSongs!: Partial<AlbumSong>[];
@Field()
@Property({ required: true, default: Date.now })
createdAt: Date;
@Field()
@Property({ required: true, default: Date.now })
updatedAt: Date;
}
export const AlbumModel = getModelForClass(Album);
When trying to query the album using:
@Query((_returns) => Album, { nullable: false, name: "album" })
async getAlbumById(@Arg("id") id: string) {
return await AlbumModel.findById({ _id: id });
}
With the following GraphQL:
query Album($albumId: String!) {
album(id: $albumId) {
id
albumSongs {
id
song {
id
}
}
}
}
I get: "Cannot return null for non-nullable field AlbumSong.song."
To me it seems like the reference is not working, when i only query the albumSong's id
it returns just fine...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置a fieldResolver 在专辑中解决这首歌
Setup a FieldResolver to resolve the song within an AlbumSong