Skip to content

model

connector.model(fn)

Used to define your data model. This model validates inserted data and Netlify uses it to create the GraphQL schema for your connector.

Takes a function with an object as a parameter. The object contains the following properties:

ParameterDescription
defineObject that contains a number of methods to define models and types on your data model.
optionsObject that contains the configuration values set for options defined using defineOptions().
stateObject that contains data stored during init().

Learn more about defining your data model.

define

Contains methods to define models and types on your data model.

MethodDescription
document()Used to define a document model.
enum()Used to define an enum type.
inlineEnum()Used to define an enum type without an explicit name set in the definition.
inlineObject()Used to define an object type without an explicit name set in the definition.
inlineUnion()Used to define a union type without an explicit name set in the definition.
locales()Used to define all available locales in your data source.
object()Used to define an object type.
union()Used to define a union type.

document

define.document()

Used to define a document model. Pass in an object with the following properties:

PropertyDescription
nameString representing the name of the document model. For example, Post or User.
cacheFieldName(optional) String representing the field to use for caching each document.
fieldsObject containing each of the document model’s fields.
localized(optional) Boolean. Set to true to specify that the model is document-localized.

Note that every document has an id that is defined by default. This is the unique ID within your document model type. You don’t need to define an id property manually in your model, but you will need to set the value when you sync documents.

Usage example
connector.model(async ({ define }) => {
define.document({
name: "Post",
cacheFieldName: "updatedAt",
fields: {
title: {
type: "String",
},
updatedAt: {
type: "String",
required: true,
},
},
});
});

enum

define.enum()

Used to define an enum type. Pass in an object with the following properties:

PropertyDescription
nameString representing the name of the enum.
valuesArray of objects containing label and value string pairs.

For unnamed enum types, you can also define the models inline using inlineEnum().

Usage example
connector.model(async ({ define }) => {
define.enum({
name: "ExampleEnumStoplight",
values: [
{
label: "Green light", // used as the GraphQL description
value: "GREEN" // the actual enum member value
},
{
label: "Yellow light",
value: "YELLOW"
},
{
label: "Red light",
value: "RED"
},
]
})
})

inlineEnum

define.inlineEnum()

Used to define an enum type without an explicit name set in the definition. Pass in an object with the following property:

PropertyDescription
valuesArray of objects containing label and value string pairs.
Usage example
connector.model(async ({ define }) => {
define.document({
name: "Post",
fields: {
content: {
type: define.inlineEnum({
values: [
{
label: "Green light", // used as the GraphQL description
value: "GREEN" // the actual enum member value
}
]
})
}
}
})
})

inlineObject

define.inlineObject()

Used to define an object type without an explicit name set in the definition. Pass in an object with the following property:

PropertyDescription
fieldsObject containing each of the object’s fields.
Usage example
connector.model(async ({ define }) => {
define.document({
name: "Post",
fields: {
content: {
type: define.inlineObject({
fields: {
title: {
type: "String",
},
}
})
}
}
})
})

inlineUnion

define.inlineUnion()

Used to define a union type without an explicit name set in the definition. Pass in an object with the following property:

PropertyDescription
typesArray of strings representing object types or document models.
Usage example
connector.model(async ({ define }) => {
define.document({
name: "Post",
fields: {
content: {
type: define.inlineUnion({
types: ["Post", "News"]
})
}
}
})
})

locales

define.locales()

Used to define all available locales in your data source. Pass in an array of objects containing the following properties:

PropertyDescription
codeString representing the locale code.
default(optional) Boolean used to designate a default locale. If not set, the first locale in the array becomes the default.
Usage example
connector.model(({ define }) => {
define.locales([
{
code: "en-US",
default: true,
},
{
code: "ca-FR",
},
]);
});

object

define.object()

Used to define an object type. Pass in an object with the following properties:

PropertyDescription
nameString representing the name of the object.
fieldsObject containing each of the object’s fields.

For unnamed object types, you can also define the models inline using inlineObject().

Usage example
// defines a Content object type
const Content = define.object({
name: "Content",
fields: {
title: {
type: "String",
},
}
});

union

define.union()

Used to define a union type. Pass in an object with the following properties:

PropertyDescription
nameString representing the name of the union.
typesArray of strings representing object types or document models.

For unnamed union types, you can also define the models inline using inlineUnion().

Usage example
const Content = define.union({
name: "ExampleContentUnion",
types: ["Post", "News"]
})
connector.model(async ({ define }) => {
const UserModel = define.document({
name: "User",
fields: {
posts: {
type: Content,
list: true
},
mostPopularPost: {
type: Content
}
}
})
define.document({
name: "News"
fields: {
title: {
type: "String"
}
}
})
define.document({
name: "Post",
fields: {
author: {
type: UserModel
}
}
})
})

fields

Object used to define fields for a document model, an object type, or an inline object type. Use an object with the following properties:

PropertyDescription
{field_name}Defined using the object property name for that field. You can use any field name except for internal, id, and fields. Review the usage example below for an example of how to set the name.
typeString or model definition that defines the type of the field. Learn more about field types.
required(optional) Boolean. Set to true to mark the field as required.
list(optional) Boolean. Set to true to indicate the field is a list. To make the list required, set this property to required instead of true. For example, list: required.
localized(optional) Boolean. Set to true to mark the field as localized.

Field types

The fields on your document model or object type can use the following as a type:

TypeDescription
Built-in scalarsString, Int, Float, Boolean, JSON, and Date.
Mapped built-in scalarsstring (mapped to String), integer (Int), number (Float), boolean (Boolean), json (JSON), date (Date).
DocumentsAnother document model you’ve defined. Setting a document model as the type of a field automatically makes that field a relationship field.
EnumsEnum type that you’ve defined.
ObjectsObject type that you’ve defined.
UnionsUnion type that you’ve defined.

If you have a type that is only used once, you can define it inline within another field definition. This can be convenient when automatically generating models and types.

Usage example

connector.model(async ({ define }) => {
// this defines an object type that we store in a variable called ContentType
const ContentType = define.object({
name: "Content",
fields: {
title: {
type: "String",
},
},
});
// this defines a document model `Post` with two fields: `title`, and `content`.
// `title` is a field with `String` type and is required.
// `content` is a field with the `ContentType` object type, defined above
define.document({
name: "Post",
fields: {
title: {
type: "String",
required: true,
},
content: {
type: ContentType,
},
},
});
});

Got it!

Your feedback helps us improve our docs.