graphql - 使用 TypORM 返回一个类型数组
问题描述
我是第一次使用 typeorm。我正在创建用户 - 配置文件一对一的关系。当我尝试查询时profiles
,我得到了错误Cannot return null for non-nullable field Profile.firstName
。
我的用户实体
@ObjectType()
@Entity("users")
export class User extends BaseEntity{
@Field()
@PrimaryGeneratedColumn("uuid")
id: string;
@Field()
@OneToOne(() => Profile, profile => profile.user)
@JoinColumn()
profile: Profile;
@Field()
@Column({ nullable: true })
profileId: string;
}
我的个人资料实体:
@ObjectType()
@Entity("profile")
export class Profile extends BaseEntity{
@Field()
@PrimaryGeneratedColumn("uuid")
id: string;
@OneToOne(() => User, user => user.profile)
user: User;
}
我的个人资料解析器查询
@Resolver()
export class ProfileResolver {
@Query(() => Profile)
async profiles(
) {
const profiles = await Profile.find()
console.log("Profiles", profiles) // This contains the right data
return profiles
}
}
当我第一次注册用户时,我确保首先创建一个配置文件,然后将其附加到用户。
创建用户时我的注册突变
@Mutation(() => Boolean)
async register(
@Arg('email') email: string,
@Arg('password') password: string
) {
const hashedPassword = await hash(password, 12)
const profile: Profile = Profile.create()
await profile.save()
const user: User = User.create({
email,
password: hashedPassword,
profileId: profile.id,
profile: profile
})
console.log(user) // It contains the user with a relative profile
我还尝试在此处添加该字段:
const profile: Profile = Profile.create({firstName: "test})
但我总是得到错误:
Cannot return null for non-nullable field Profile.firstName
当我运行查询时:
query{
profiles{
firstName
}
}
我检查了其他类似的问题,但我没有设法解决它
解决方案
推荐阅读
- javascript - 如何根据用户选择隐藏表单字段?
- vba - 读写文件导致“该进程无法访问该文件,因为它正在被另一个进程使用”错误
- python - 如何读取 plist 中的响应字符串?
- python - 如何从列表列表中提取特定的键、值对并将其添加到数据框中?
- python - python循环期间的正则表达式错误
- sbt - How to create new SBT configuration with its own Main?
- node.js - 如何在 Loopback 4 上将文件作为下载流式传输?
- java - 如何在连接四游戏中确定获胜者?
- kubernetes - Kubernetes Traefik Ingress 收到错误的网关错误
- python - Flask程序没有在python中启动webbrowser