类型配置
|
这是 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 参数的值。
使用
禁用查询 movies 和 moviesConnection 边缘字段
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
@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