无法返回无效字段 - typegoose和typegraphql

发布于 2025-02-02 14:44:01 字数 1777 浏览 4 评论 0原文

我有使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城旧梦 2025-02-09 14:44:01

设置a fieldResolver 在专辑中解决这首歌

@Resolver(() => AlbumSong)
export class AlbumSongFieldResolver {
  @FieldResolver(() => Song)
  async song(@Root() parent: AlbumSong): Promise<Song> {
    return Song.findOne(parent.song);
  }
}

Setup a FieldResolver to resolve the song within an AlbumSong

@Resolver(() => AlbumSong)
export class AlbumSongFieldResolver {
  @FieldResolver(() => Song)
  async song(@Root() parent: AlbumSong): Promise<Song> {
    return Song.findOne(parent.song);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文