简体   繁体   English

对 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?

Here's the definition of String and ID from GraphQL's documentation :以下是 GraphQL文档StringID的定义:

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.

Would you use the ID type for the userId field in Article , which is the foreign key to the User entity?您会为Article中的userId字段使用 ID 类型,这是 User 实体的外键吗? Or would you prefer String type for userId because the id field is the unique identifier for Article , not userId ?或者您更喜欢String类型的userId因为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;

}

Semantically the scalar ID makes more sense than the scalar String since per the definition it represents a unique identifier, often used to refetch an object or as the key for a cache .从语义上讲,标量ID比标量String更有意义,因为根据定义它represents a unique identifier, often used to refetch an object or as the key for a cache It is also not meant for human consumption, another part of the definition of the scalar ID .它也不意味着供人类使用,这是标量ID定义的另一部分。

The uniqueness of it is not really important from a GraphQL perspective it is very common to see multiple IDs in the same type.从 GraphQL 的角度来看,它的唯一性并不重要,在同一类型中看到多个IDs是很常见的。 Using it is in fact encourage has it indicates to your consumer that it can (most likely) be used to fetch the full object.使用它实际上是鼓励它向您的消费者表明它可以(很可能)用于获取完整的 object。

Now you also have to challenge the need to expose the userId at all since this is a very "REST" way of thinking and it leaks the database relational model used, making it harder to evolve your data model without making breaking changes to the schema.现在您还必须挑战公开userId的需要,因为这是一种非常“REST”的思维方式,它会泄漏所使用的数据库关系 model,这使得在不对架构进行重大更改的情况下更难发展您的数据 model。

Generally you would only expose the user: User field as it already contains an id field.通常,您只会公开user: User字段,因为它已经包含一个id字段。 One might argue that it would do an unnecessary join, but this could be easily optimized with a lookahead.有人可能会争辩说它会进行不必要的连接,但这可以通过前瞻轻松地进行优化。 If a consumer needs user data, they will likely need more than just the id anyway so they will have to fetch the whole object from the database.如果消费者需要用户数据,他们可能需要的不仅仅是id ,因此他们将不得不从数据库中获取整个 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM