gpt4 book ai didi

sql - GraphQL & Sequelize : Users and followers/following

转载 作者:行者123 更新时间:2023-12-03 22:34:46 29 4
gpt4 key购买 nike

我正在尝试设置我的 User GraphQL 模型以具有要查询的 followersfollowing 属性。但是,我无法在 Sequelize 中建立关系。我正在尝试使用 Follower 模型作为连接表并设置 BelongsToMany 关联,但无法使其正常工作。任何人都可以建议该怎么做或指出我做错了什么?

我通过手动查询提出了一个临时解决方案,您可以在我的 User.model.ts 中看到,但我相信使用正确的配置有更好的方法来做到这一点。

我分别在 GraphQL 和 Sequelize、TypeGraphQLsequelize-typescript 以及 PostgreSQL 周围使用 typescript 包装器。

User.model.ts

// VENDOR
import { ObjectType, Field, ID } from 'type-graphql';
import { Model, Table, Column, PrimaryKey, Unique, IsUUID, HasMany, DefaultScope, AllowNull, DataType, BelongsToMany } from 'sequelize-typescript';
// APP
import Post from '../post/post.types';
import Follower from '../follower/follower.types';

/** User model for GraphQL & Database */
@Table({ timestamps: false, tableName: 'users' }) // tell sequelize to treat class as table model
@DefaultScope(() => ({ include: [{ model: Post.scope(), as: 'posts' }] })) // tell sequelize to include posts in its default queries
@ObjectType() // tell GraphQL to treat class as GraphQL model
export default class User extends Model<User>{

@PrimaryKey
@Unique
@AllowNull(false)
@IsUUID(4)
@Column(DataType.UUID)
@Field(() => ID)
id: string;

@Unique
@AllowNull(false)
@Column
@Field()
ci_username: string;

@AllowNull(false)
@Column
@Field()
username: string;

@AllowNull(false)
@Column
@Field()
first_name: string;

@Column
@Field()
last_name: string;

@Column
@Field({ nullable: true })
profile_picture?: string;

// @BelongsToMany(() => User, { otherKey: 'user_id', as: 'followers', through: () => Follower })
// @Field(() => [User])
// followers: User[];

// MY TEMPORARY SOLUTION USING MANUAL QUERYING
@Field(() => [User])
get followers(): Promise<User[]> {
return Follower.findAll({ where: { user_id: this.id } })
.then(records => records.map(record => record.follower_id))
.then((follower_ids: string[]) => {
return User.findAll({ where: { id: follower_ids }});
})
}

// DOES NOT WORK, BUT I BELIEVE COULD POTENTIALLY LEAD TO BETTER SOLUTION
@BelongsToMany(() => User, { otherKey: 'follower_id', as: 'following', through: () => Follower })
@Field(() => [User])
following: User[];

@HasMany(() => Post)
@Field(() => [Post])
posts: Post[];
}

Follower.model.ts

// VENDOR
import { Model, Table, Column, PrimaryKey, Unique, IsUUID, AllowNull, DataType, Index, ForeignKey, AutoIncrement } from 'sequelize-typescript';

// APP
import User from '../user/user.types';

/** Follower model for Database */
@Table({ timestamps: false, tableName: 'followers' }) // tell sequelize to treat class as table model
export default class Follower extends Model<Follower>{

@PrimaryKey
@AutoIncrement
@Unique
@AllowNull(false)
@Column
id: number;

@AllowNull(false)
@IsUUID(4)
@Index
@ForeignKey(() => User)
@Column(DataType.UUID)
user_id: string;

@AllowNull(false)
@IsUUID(4)
@Index
@ForeignKey(() => User)
@Column(DataType.UUID)
follower_id: string;
}

GraphQL 查询
{
users: allUsers {
id
username
first_name
last_name
following {
username
id
}
}
}

GraphQL 响应/错误
{
"errors": [
{
"message": "Cannot return null for non-nullable field User.following.",
"locations": [
{
"line": 7,
"column": 5
}
],
"path": [
"users",
0,
"following"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"Error: Cannot return null for non-nullable field User.following.",
" at completeValue (/Users/jsainz237/Projects/trueview/trueview-api/node_modules/graphql/execution/execute.js:560:13)",
" at /Users/jsainz237/Projects/trueview/trueview-api/node_modules/graphql/execution/execute.js:492:16"
]
}
}
}
],
"data": null
}

任何帮助表示赞赏。

最佳答案

您需要手动编写一个 @FieldResolver 来解析关系并返回正确的数据。
另一种解决方案是依赖 ORM 功能和惰性关系——当返回的基本实体包含一个作为字段的 promise 时,因此当 .then() 被调用时,它会自动获取数据库的关系。

关于sql - GraphQL & Sequelize : Users and followers/following,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62395300/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com