自动生成

这是 GraphQL 库版本 6 的文档。对于长期支持 (LTS) 版本 5,请参阅 GraphQL 库版本 5 LTS

此页面描述了用于自动生成的指令

@id

此指令将字段标记为对象类型的标识符。这使得能够为字段自动生成 ID。

每个生成的 ID 的格式是由 randomUUID() 函数 生成的 UUID。该字段将不会出现在变异的输入类型中。

建议与 @unique 结合使用以添加唯一的节点属性约束。

定义

"""Indicates that the field is an identifier for the object type."""
directive @id on FIELD_DEFINITION

用法

以下类型定义将 id 字段指定为自动生成的 value

type User @node {
    id: ID! @id
    username: String!
}

@timestamp

此指令将字段标记为时间戳字段,可用于存储通过 GraphQL API 发生特定事件的时间戳。

这些事件在 GraphQL API 层触发并存储。通过其他路由在数据库中发生的事件不会触发这些时间戳的更新。

定义

enum TimestampOperation {
    CREATE
    UPDATE
}

"""Instructs @neo4j/graphql to generate timestamps on particular events, which will be available as the value of the specified field."""
directive @timestamp(
    """Which events to generate timestamps on. Defaults to both create and update."""
    operations: [TimestampOperation!]! = [CREATE, UPDATE]
) on FIELD_DEFINITION

用法

以下类型定义具有两个独立的字段来存储创建和更新事件的时间戳

type User @node {
    createdAt: DateTime! @timestamp(operations: [CREATE])
    updatedAt: DateTime! @timestamp(operations: [UPDATE])
}

以下两个等效类型定义具有一个字段,该字段存储最后一次 createupdate 的事件时间戳

type User @node {
    lastModified: DateTime! @timestamp
}
type User @node {
    lastModified: DateTime! @timestamp(operations: [CREATE, UPDATE])
}