Directives

@col

Arguments

NameTypeDefault ValueDescription
nameString!noneName of column to associate with this field

Targets

  • FIELD_DEFINITION

The @col directive is used to explicitly set the name of the corresponding column for a field. If the directive is not used, Sqlmancer will use the name of the field instead, applying whatever transformation to the field's name was configured through the transformFieldNames argument of the @sqlmancer directive.

@depend

Arguments

NameTypeDefault ValueDescription
on[String!]!noneList of columns to select when requesting this field

Targets

  • FIELD_DEFINITION

The @depend directive is used to indicate a field is not associated with a particular column, but still depends on one or more columns. If the field is requested, the specified columns will be included in the request and available to the field's resolver.

@hasDefault

Arguments

none

Targets

  • FIELD_DEFINITION

The @hasDefault directive is used to indicate the associated column has a default value. This means the field can have a non-nullable type without being required when creating a new instance of the model.

@input

Arguments

NameTypeDefault ValueDescription
actionCREATE or UPDATEnoneWhether to generate an input type for creating or updating a model record
modelStringField's typeThe model being created or updated
listBooleanfalseWhether the added input argument should be a list of objects instead of an object

Targets

  • FIELD_DEFINITION

The @input directive will add an input argument to the field with a generated input type based on the provided model. The generated type will be different depending on the action argument. For UPDATE, all model fields are always nullable (optional). For CREATE, fields are only nullable (optional) if they are also nullable on the model type or if they have a @hasDefault directive.

@ignore

Arguments

none

Targets

  • FIELD_DEFINITION

The @ignore directive is used to indicate the field has no associated column. Note: if a @depend or @join directive is used on a field, an @ignore directive is not necessary.

@limit

Arguments

none

Targets

  • FIELD_DEFINITION

The @limit directive adds a limit argument to the field with a type of Int.

@many

Arguments

NameTypeDefault ValueDescription
modelStringField's typeThe model for which to generate the where and orderBy arguments

Targets

  • FIELD_DEFINITION

The @many directive can be used instead of separately applying the @limit, @offset, @where and @orderBy directives. The model argument may be omitted if the type of the field the directive is applied to is the target model.

@model

Arguments

NameTypeDefault ValueDescription
tableStringnoneThe name of the table (or view) in the database this model represents
pkString!noneThe primary key column
cteStringnoneCommon table expression used to create this model if it is an inline view
readOnlyBooleanfalseWhether the model should not include create, update or delete methods
include[String!][]Any columns (not necessarily fields) to always include when querying this model

Targets

  • INTERFACE
  • OBJECT
  • UNION

The @model directive indicates a data model should be generated for the specified type. Either the table argument or the cte argument are required. If the @model directive is used on a Union or Interface, then the fields of all possible types for that union or interface will be merged to determine the possible columns and associations for the model.

@offset

Arguments

none

Targets

  • FIELD_DEFINITION

The @offset directive adds an offset argument to the field with a type of Int.

@orderBy

Arguments

NameTypeDefault ValueDescription
modelStringField's typeThe model for which to generate the orderBy argument

Targets

  • FIELD_DEFINITION

The @orderBy directive adds an orderBy argument to the field with a generated input type based on the field's type.

@paginate

Arguments

none

Targets

  • FIELD_DEFINITION

The @paginate directive is an optional directive used to modify a field's type. The directive will generate a new output type representing a page of results based on the field's original type, which should be a model. For example, given these type definitions

type Film @model(table: "film", pk: "id") {
id: ID!
title: String!
rentalCost: Float!
}
type Query {
films: Film @paginate
}

the directive will generate these types

type FilmPage {
results: [Film!]!
hasMore: Boolean!
aggregate: FilmAggregate!
}
type FilmAggregate {
count: Int!
min: FilmAggregateMin!
max: FilmAggregateMax!
sum: FilmAggregateSum!
avg: FilmAggregateAvg!
}
type FilmAggregateMin {
id: ID
title: String
rentalCost: Float
}
type FilmAggregateMax {
id: ID
title: String
rentalCost: Float
}
type FilmAggregateSum {
rentalCost: Float
}
type FilmAggregateAvg {
rentalCost: Float
}

and change the type of the field:

type Query {
films: FilmPage!
}

The directive can be used in conjunction with the paginate client method, or for creating paginated relations using the @relate directive. However, use of the directive is completely optional -- you can always create your own types to limit which fields are exposed in your API.

@private

Arguments

none

Targets

  • FIELD_DEFINITION
  • INTERFACE
  • OBJECT
  • UNION

The @private directive indicates a field or type should be removed from the schema. Sqlmancer will still parse fields and types removed this way when generating its data models. This allows you to include sensitive fields like passwords in the data model without exposing them through the API.

@relate

Arguments

NameTypeDefault ValueDescription
on[{ from: String!, to: String! }]noneThe fields used to join the two tables
throughStringnoneThe name of the junction table
paginationOFFSETnoneIndicates the type of pagination used by the field, if any

Targets

  • FIELD_DEFINITION

The @relate directive is used to a relation with another model. The on argument consists of one or two objects, each having a from and to field. If no junction table is used, on should receive a single object with the from indicating a column on this model's table and the to indicating a column on the related model's table.

If a junction table is used, on should receive two objects. The first object's from would be a column on this model's table and its to would be a column on the junction table. The second object's from would be a column on the junction table and its to would be a column on the related model's table. If a function table is used, the through argument should be provided as well to indicate the name of the junction table.

@sqlmancer

Arguments

NameTypeDefault ValueDescription
dialectPOSTGRES or MYSQL or MARIADB or SQLITEnoneThe SQL dialect being used
transformFieldNamesCAMEL_CASE or PASCAL_CASE or SNAKE_CASEnoneThe transformation to apply to a field name when determining the column name it matches
customScalars{ string: [String!], number: [String!], boolean: [String!], JSON: [String!], Date: [String!] }noneUsed to map any custom scalars to their respective types. Not including a custom scalar means it will be omitted from a model's fields entirely. The type you map it to effects the TypeScript typings as well as any generated types (like those added with the @where and @aggregate directives).

Targets

  • OBJECT

The @sqlmancer directive is required and should be applied to your Query type.

@value

Arguments

NameTypeDefault ValueDescription
isString!noneThe internal value to use instead of the enum value's name

Targets

  • ENUM_VALUE

The @value directive sets the internal value for an enum value. The directive should be utilized whenever the schema enum value doesn't match the enum value in the database (for example, if the database value includes a dash or other character prohibited by GraphQL). The directive is used in instead of utilizing the resolver map for the same purpose.

@where

Arguments

NameTypeDefault ValueDescription
modelStringField's typeThe model for which to generate the where argument

Targets

  • FIELD_DEFINITION

The @where directive adds an where argument to the field with a generated input type based on the field's type.