Type configuration

When representing a Neo4j node, a GraphQL Object Type produces multiple operation fields in the query, mutation, and subscription types. For example:

type Movie {
    title: String
    length: Int
}

From these type definitions, the library generates the following operation fields:

Query:

  • movies

  • moviesAggregate

  • moviesConnection

Mutation:

  • createMovies

  • deleteMovies

  • updateMovies

Subscription:

  • movieCreated

  • movieUpdated

  • movieDeleted .

This page describes how to reduce the operation fields produced using the directives @query, @mutation, and @subscription.

@query

This directive is used to limit the availability of query operations in the library.

Definition

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

Usage

Disable movies and moviesConnection operations
type Movie @query(read: false, aggregate: true) {
    title: String
    length: Int
}
Disable moviesAggregate operations
type Movie @query(read: true, aggregate: false)  {
    title: String
    length: Int
}

@mutation

This directive is used to limit the availability of mutation operations in the library.

Definition

enum MutationFields {
    CREATE
    UPDATE
    DELETE
}

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

Usage

Disable Create, Delete, and Update operations for Movie
type Movie @mutation(operations: []) {
    title: String
    length: Int
}
Enable only Create operations for Movie
type Movie @mutation(operations: [CREATE]) {
    title: String
    length: Int
}

@subscription

This directive is used to limit subscription operations in the library.

Definition

enum SubscriptionFields {
    CREATED
    UPDATED
    DELETED
    RELATIONSHIP_CREATED
    RELATIONSHIP_DELETED
}

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

Usage

Disable subscriptions for Movie
type Movie @subscription(events: [])  {
    title: String
    length: Int
}
Enable only movieCreated subscription for Movie
type Movie @subscription(events: [CREATED]) {
    title: String
    length: Int
}

@default

When generating the input type for the create mutation, the value specified in this directive is used as the default for the field.

Definition

"""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

Usage

@default may be used with enums. When setting the default value for an enum field, it must be one of the enumerated enum values:

enum Location {
    HERE
    THERE
    EVERYWHERE
}

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

@plural

This directive redefines how to compose the plural of the type for the generated operations. This is particularly useful for types that are not correctly pluralized or are non-English words. Take this type definition as an example:

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

This way, instead of the wrongly generated teches, the type is properly written as techs:

{
  techs {
    title
  }
}

The same is applied to other operations such as createTechs. However, keep in mind that database labels are not changed with this directive.