自动生成

这是 GraphQL Library 7 版本的文档。对于长期支持 (LTS) 版本 5,请参考 GraphQL Library 5 LTS 版本

本页描述用于自动生成的指令

@id

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

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

定义

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

使用

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

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])
}