类型配置

这是 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

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

定义

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

用法

禁用查询 moviesmoviesConnection 的 edges 字段
type Movie @node @query(read: false, aggregate: true) {
    title: String
    length: Int
}
禁用 moviesConnection 的 aggregate 字段
type Movie @node @query(read: true, aggregate: 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

用法

仅启用 MoviemovieCreated 订阅
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
}

这样,类型就可以正确地写成 techs,而不是错误生成的 teches

{
  techs {
    title
  }
}

这同样适用于其他操作,例如 createTechs。但是,请记住,数据库标签不会因此指令而更改。

© . All rights reserved.