类型配置

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

在表示 Neo4j 节点时,GraphQL 对象类型会在查询、变更和订阅类型中生成多个操作字段。例如

type Movie @node {
    title: String
    length: Int
}

根据这些类型定义,库会生成以下操作字段

查询:

  • movies

  • moviesConnection

变更:

  • createMovies

  • deleteMovies

  • updateMovies

订阅:

  • movieCreated

  • movieUpdated

  • movieDeleted

moviesConnection:

  • 聚合

  • 边缘

  • 总计数

  • 页面信息

本页描述如何使用指令 @query@mutation@subscription 来减少生成的操作字段。

@query

此指令用于限制库中查询操作的可用性。

定义

directive @query(read: Boolean! = true, connection: Boolean, aggregate: Boolean! = false) on OBJECT | SCHEMA | INTERFACE | UNION

其中 connection 参数的默认值为 read 参数的值。

使用

禁用查询 moviesmoviesConnection 边缘字段
type Movie @node @query(read: false, aggregate: true) {
    title: String
    length: Int
}
禁用 moviesConnection 聚合字段
type Movie @node @query(read: true, aggregate: false)  { # aggregate can be omitted as it defaults to false
    title: String
    length: Int
}
完全禁用 moviesConnection
type Movie @node @query(read: true, connection: false, aggregate: false)  { # aggregate can be omitted as it defaults to false
    title: String
    length: Int
}
禁用所有读取
type Movie @node @query(read: false, aggregate: false)  { # aggregate can be omitted as it defaults to false
    title: String
    length: Int
}

@mutation

此指令用于限制库中变更操作的可用性。

定义

enum MutationFields {
    CREATE
    UPDATE
    DELETE
}

directive @mutation(operations: [MutationFields!]! = [CREATE, UPDATE, DELETE]) on OBJECT | SCHEMA

用法

禁用 Movie 的创建、删除和更新操作
type Movie @node @mutation(operations: []) {
    title: String
    length: Int
}
仅启用 Movie 的创建操作
type Movie @node @mutation(operations: [CREATE]) {
    title: String
    length: Int
}

@subscription

此指令用于在库中启用订阅操作。订阅默认是选择加入的,这意味着除非明确启用,否则它们是禁用的。

定义

enum SubscriptionFields {
    CREATED
    UPDATED
    DELETED
}

directive @subscription(events: [SubscriptionFields!]! = [CREATED, UPDATED, DELETE]) on OBJECT | SCHEMA

用法

仅为 Movie 启用 movieCreated 订阅
type Movie @node @subscription(events: [CREATED]) {
    title: String
    length: Int
}
为除 Movie 之外的所有类型启用订阅
type Movie @node @subscription(events: [])  {
    title: String
    length: Int
}

type Actor @node  {
    name: String
    movies: [Movie!]! @relationship(type: "ACTED_IN", direction: OUT)
}

extend schema @subscription

@default

在为 create 变更生成输入类型时,此指令中指定的值将作为该字段的默认值。

定义

"""Int | Float | String | Boolean | ID | DateTime | Enum"""
scalar Scalar

"""Instructs @neo4j/graphql to set the specified value as the default value in the CreateInput type for the object type in which this directive is used."""
directive @default(
    """The default value to use. Must be a scalar type and must match the type of the field with which this directive decorates."""
    value: Scalar!,
) on FIELD_DEFINITION

用法

@default 可与枚举一起使用。为枚举字段设置默认值时,必须是枚举值之一。

enum Location {
    HERE
    THERE
    EVERYWHERE
}

type SomeType @node {
    firstLocation: Location! @default(value: HERE) # valid usage
    secondLocation: Location! @default(value: ELSEWHERE) # invalid usage, will throw an error
}

@plural

定义

"""
Instructs @neo4j/graphql to use the given value as the plural of the type name
"""
directive @plural(
  """The value to use as the plural of the type name."""
  value: String!
) on OBJECT | INTERFACE | UNION

用法

此指令重新定义了在生成的操作中如何构成类型的复数形式。这对那些复数形式不正确或非英文的类型特别有用。以下是一个类型定义示例

type Tech @plural(value: "Techs") @node {
  name: String
}

这样就不会错误生成 teches,而是正确地写成 techs

{
  techs {
    title
  }
}

同样适用于其他操作,如 createTechs。但请注意,数据库标签不会因为此指令而改变。