对 Type-graphql 和 Typeorm 实体中的外键字段使用 ID 标量类型在语义上是否正确?

Is it semantically correct to use ID scalar type for a foreign key field in a Type-graphql and Typeorm entity?

这是来自 GraphQL 的 documentationStringID 的定义:

GraphQL comes with a set of default scalar types out of the box:

String: A UTF‐8 character sequence.

ID: The ID scalar type represents a unique identifier, often used to refetch an object or as the key for a cache. The ID type is serialized in the same way as a String; however, defining it as an ID signifies that it is not intended to be human‐readable.

您会为 Article 中的 userId 字段使用 ID 类型吗?它是用户实体的外键?或者您更喜欢 userIdString 类型,因为 id 字段是 Article 的唯一标识符,而不是 userId?

@ObjectType()
@Entity()
export class Article {
  @Field((type) => ID)
  @PrimaryGeneratedColumn("uuid")
  id: string;

  @Field()
  @Column({ type: "text" })
  title: string;

  @Field(type => ID) // <<= ID or String?
  @Column({type: "uuid"})
  userId: string

  @ManyToOne((type) => User, (user) => user.id)
  @JoinColumn({ name: "userId" })
  user: User;

  @Field()
  @CreateDateColumn()
  created: Date;

  @Field()
  @UpdateDateColumn()
  updated: Date;

}

@ObjectType()
@Entity()
export class User {
  @Field((type) => ID)
  @PrimaryGeneratedColumn("uuid")
  id: string;

  @Field()
  @Column({ type: "text" })
  name: string;

}

从语义上讲,标量 ID 比标量 String 更有意义,因为根据定义它 represents a unique identifier, often used to refetch an object or as the key for a cache。它也不意味着供人类消费,标量的另一部分定义 ID.

从 GraphQL 的角度来看,它的唯一性并不重要,在同一类型中看到多个 IDs 是很常见的。使用它实际上是鼓励它向您的消费者表明它可以(很可能)用于获取完整的对象。

现在您还必须挑战公开 userId 的需要,因为这是一种非常“REST”的思维方式,它会泄露所使用的数据库关系模型,使您的数据更难发展在不对架构进行重大更改的情况下建立模型。

通常您只会公开 user: User 字段,因为它已经包含 id 字段。有人可能会争辩说它会进行不必要的连接,但这可以通过前瞻轻松地进行优化。如果消费者需要用户数据,他们可能需要的不仅仅是 id,所以他们将不得不从数据库中获取整个对象。