Skip to content

NetlifyConnector

The NetlifyConnector provides different methods to help create a connector within your integration.

Usage example

import { NetlifyIntegration } from "@netlify/sdk";
// create an integration
const integration = new NetlifyIntegration();
// and a connector
const connector = integration.addConnector({
typePrefix: `Example`,
});
// use connector APIs on this object
connector.sync(async ({ models, isInitialSync }, configOptions) => {
models.Example.createNode({
id: 1,
});
});
export { integration };

Type parameters

NameType
ConnectorConfigConnectorConfig

Methods

init

connector.init()

Used to initialize your connector. Returns an object containing initialization state that you can use throughout your connector.

connector.init(({ options }) => {
const apiClient = new CustomCMSAPIClient({
url: options.apiURL,
token: options.apiToken
})
return {
apiClient // this will be available as state.apiClient in other connector APIs
}
})

Note that the returned state object is specific to each connector instance. If a data layer contains multiple instances of your connector, each will run its own connector.init(fn) lifecycle with the relevant configuration options for that instance and store its own state object.

event

Deprecated

connector.event() is deprecated. Use connector.sync() instead.

If you have already built an integration that uses this API, you can pass a single function to connector.sync() instead and use the isInitialSync variable to determine which logic to run.

Before:

connector.event(`createAllNodes`, customSyncAllDataFn)
connector.event(`updateNodes`, customSyncChangedDataFn)

After:

connector.sync(({ isInitialSync, models }) => {
if (isInitialSync) {
return customSyncAllDataFn(models)
} else {
return customSyncChangedDataFn(models)
}
})

If you previously set connector.event('updateNodes', false) to ensure every sync is a full data sync (with no caching), you need to configure your connector with the following setting:

integration.addConnector({
supports: {
deltaSync: false
}
})

model

connector.model()

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

Has access to the following:

  • define, which contains the following methods:
    • document: used to define a document model.
    • nodeModel: deprecated. Use document instead.
    • object: used to define an object type.
    • union: used to define a union type.
    • enum: used to define an enum type.
    • 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.
    • inlineEnum: used to define an enum type without an explicit name set in the definition.
  • configOptions object that contains the configuration values set for options defined using defineOptions.
  • state object that contains data stored during connector.init().

Learn more about defining your data model.

sync

connector.sync()

Used to define happens when data initially syncs from your data source into a Netlify Connect data layer.

Also used to define what happens when a sync occurs to update the data — either an automatic sync that a webhook triggers or a manual sync that a user triggers in the Netlify UI.

Has access to the following:

  • models: object that contains all defined document models.
  • cache: helper for storing and accessing non-node data (like CMS sync tokens).
  • isInitialSync: a boolean that will be true on initial data syncs and false on subsequent data syncs.
  • options: object that contains the configuration values set for options defined using defineOptions.
  • state: object that includes data stored during connector.init().

Learn more about specifying how to create and update data.

Example usage:

connector.sync(({ isInitialSync, models, cache, options, state }) => {
if (isInitialSync) {
return syncAllData(models)
} else {
return syncChangedData(models)
}
})

defineOptions

defineOptions

Used to define the configuration options your connector will expose in the Netlify UI.

Has access to zod, which you use to create and return a Zod object that contains your options.

Learn more about configuring options for the Netlify UI.