类型配置
这是 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: * aggregate
* edges
* totalCount
* pageInfo
本页介绍如何使用 @query
、@mutation
和 @subscription
指令来减少生成的操作字段。
@query
@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