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:
Parameter | Description |
---|---|
define | Object that contains a number of methods to define models and types on your data model. |
options | Object that contains the configuration values set for options defined using defineOptions in addConnector . |
state | Object that contains data stored in initState when addConnector runs. |
Learn more about defining your data model.
define
Contains methods to define models and types on your data model.
Method | Description |
---|---|
crossReference() | Define a cross reference type. |
document() | Define a document model. |
enum() | Define an enum type. |
inlineEnum() | Define an enum type without an explicit name set in the definition. |
inlineObject() | Define an object type without an explicit name set in the definition. |
inlineUnion() | Define a union type without an explicit name set in the definition. |
locales() | Define all available locales in your data source. |
object() | Define an object type. |
union() | Define a union type. |
crossReference
define.crossReference()
Used to define a cross-reference type. Cross-reference fields allow you to specify reference fields across different connectors or across different instances of the same connector. These are cross-references that your data source can resolve.
Cross-references defined by users
Netlify also offers the ability for Connect users to define cross-references across different types of data sources. For example, from Drupal to Contentstack. These cross-references are resolved by Netlify, instead of the individual data source. Connect users can define cross-references in the Netlify UI.
Pass in an object with the following property:
Property | Description |
---|---|
to | Object or array of objects containing the modelName , connectorName , and instanceID to cross-reference to. |
Usage example
// Reference a single modelconnector.model(async ({ define }) => { const myCrossReference = define.crossReference({ to: { modelName: `ExampleDocument`, connectorName: `example-connector`, instanceID: `1234`, }, });});
// Reference multiple modelsconnector.model(async ({ define }) => { const myCrossReference = define.crossReference({ to: [ { modelName: `ExampleDocument`, connectorName: `example-connector`, instanceID: `1234`, }, { modelName: `AnotherDocument`, connectorName: `another-connector`, instanceID: `5678`, }, ], });});
// Define the cross-reference inlineconnector.model(({ define })=>{ define.document({ name: `ExampleDocument`, fields: { exampleFieldName: { type: define.crossReference({ to: { modelName: `AnotherDocument`, connectorName: `another-connector`, instanceID: `5678`, }, }), }, }, })})
document
define.document()
Used to define a document model. Pass in an object with the following properties:
Property | Description |
---|---|
name | String representing the name of the document model. For example, Post or User . |
cacheFieldName | (optional) String representing the field to use for caching each document. |
fields | Object 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:
Property | Description |
---|---|
name | String representing the name of the enum. |
values | Array 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:
Property | Description |
---|---|
values | Array 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:
Property | Description |
---|---|
fields | Object containing each of the object’s fields. |
name | (optional) String representing the name of the object. It will only be used to prefix the generated name. |
Usage example
connector.model(async ({ define }) => { define.document({ name: "Post", fields: { content: { type: define.inlineObject({ fields: { title: { type: "String", }, } }) }, bonusContent: { type: define.inlineObject({ name: `example_name`, // generated name: inline_example_name_j2kjl4 fields: { heading: { 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:
Property | Description |
---|---|
types | Array 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:
Property | Description |
---|---|
code | String 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:
Property | Description |
---|---|
name | String representing the name of the object. |
fields | Object containing each of the object’s fields. |
For unnamed object types, you can also define the models inline using inlineObject()
.
union
define.union()
Used to define a union type. Pass in an object with the following properties:
Property | Description |
---|---|
name | String representing the name of the union. |
types | Array 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:
Property | Description |
---|---|
{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. |
type | String 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
:
Type | Description |
---|---|
Built-in scalars | String , Int , Float , Boolean , JSON , and Date . |
Mapped built-in scalars | string (mapped to String ), integer (Int ), number (Float ), boolean (Boolean ), json (JSON ), date (Date ). |
Cross-reference | Cross-reference type that you’ve defined. |
Documents | Another document model you’ve defined. Setting a document model as the type of a field automatically makes that field a relationship field. |
Enums | Enum type that you’ve defined. |
Objects | Object type that you’ve defined. |
Unions | Union 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, }, }, });});