How to set gatsby preview field in migration file?

Hello,

I get an error when Iā€™m trying to set up an gatsby preview field by migration file. Unfortunately, Iā€™m getting several errors, no matter what parameters Iā€™m adding inside appearance object. Hereā€™s sample code:

const gatsbypreview = await client.fields.create(itemTypeId, {
	label: 'Gatsby Preview',
	fieldType: 'json',
	apiKey: 'gatsbypreview',
	validators: {
		required: {},
	},
	appearance: {
		editor: 'json',
		parameters: {},
		addons: [
			{
				"id": "MY_PLUGIN_ID",
				"parameters": {

					"developmentMode": true,
				},
			},
		],
	},
});

Iā€™m getting MY_PLUGIN_ID by sending request on https://site-api.datocms.com/plugins.

I tried to set in addons array attributes like:

  • ā€œtypeā€: ā€œpluginā€,
  • inside parameters object:
    • authToken
    • instanceUrl
    • developmentMode as above

, but Iā€™m still getting 422 Error - ā€œ422 INVALID_FIELD (details: {ā€œfieldā€:ā€œappearance.addons.0.idā€,ā€œcodeā€:ā€œVALIDATION_INVALIDā€,ā€œmessageā€:ā€œincompatible with field typeā€})ā€

Do you have any idea which attributes are valid for this field type? Itā€™s not mentioned in docs to be honest :frowning:

hello @m.sikora sorry for the delay here and welcome to Community! :slight_smile:

The issue in your case is that the Gatsby plugin is an editor plugin, not an addon plugin.

So you should write this:

const gatsbypreview = await client.fields.create(itemTypeId, {
	label: 'Gatsby Preview',
	fieldType: 'json',
	apiKey: 'gatsbypreview',
	validators: {
		required: {},
	},
	appearance: {
		editor: 'MY_PLUGIN_ID',
		parameters: {},
		addons: [],
	},
});

Let me know if that helps!

Thanks for your reply. Unfortunately, it didnā€™t help. Still we have the same problem with invalid field:

ApiException: 422 INVALID_FIELD (details: {ā€œfieldā€:ā€œappearance.editorā€,ā€œcodeā€:ā€œVALIDATION_INVALIDā€,ā€œmessageā€:ā€œincompatible with field typeā€})

We need to use this field with modular content in one migration file as below:

 'use strict';
module.exports = async (client) => {
  const previewModel = await client.itemTypes.create({
    name: 'Gatsby preview',
    apiKey: 'gatsby_preview',
  });
  const bannerWithSlidesBlock = await client.itemTypes.create({
    name: 'block | banner with slide',
    apiKey: 'block_banner_with_slide',
    modularBlock: true,
  });
  const itemTypeId = 'gatsby_preview';
  const field = await client.fields.create(itemTypeId, {
    label: 'module | banner with slide',
    fieldType: 'rich_text',
    apiKey: 'module_banner_with_slide',
    validators: {
      richTextBlocks: {
        itemTypes: [bannerWithSlidesBlock.id],
      },
    },
  });
  const gatsbypreview = await client.fields.create(itemTypeId, {
    label: 'Gatsby Preview',
    fieldType: 'json',
    apiKey: 'gatsbypreview',
    validators: {
        required: {},
    },
    appearance: {
        editor: 'MY_PLUGIN_ID',
        parameters: {},
        addons: [],
    },
});
}

Could you please advise?

Iā€™ve tried your code and that works for me perfectly.

Could you please double check what is your plugin ID?

Are we talking about this plugin: https://www.datocms.com/marketplace/plugins/i/datocms-plugin-gatsby-cloud ?

Yes, we are talking about this plugin. We double checked ID - it is in 5-digit format like ā€˜12345ā€™ - it is USER ID, not PLUGIN ID. We get it by sending request on https://site-api.datocms.com/plugins - is it a good way to an appropriate id?
We are running migration by using command:

dato migrate --token=ā€œMY_DATOCMS_FULL_ACCESS_TOKENā€ --destination=ā€œtestā€

We are still getting the same error, please look at information below:

Failed request:
POST https://site-api.datocms.com/item-types/gatsby_preview/fields
content-type: application/json
accept: application/json
authorization: Bearer [FULL_ACCESS_TOKEN]
user-agent: js-client v3.2.0
X-Api-Version: 3
X-Environment: test
{ā€œdataā€:{ā€œtypeā€:ā€œfieldā€,ā€œattributesā€:{ā€œlabelā€:ā€œGatsby Previewā€,ā€œfield_typeā€:ā€œjsonā€,ā€œapi_keyā€:ā€œgatsbypreviewā€,ā€œvalidatorsā€:{},ā€œappearanceā€:{ā€œaddonsā€:,ā€œeditorā€:ā€œUSER_IDā€,ā€œparametersā€:{}},ā€œdefault_valueā€:null}}}
HTTP Response:
422 Unprocessable Entity
{ā€œdataā€:[{ā€œidā€:ā€œf7f81eā€,ā€œtypeā€:ā€œapi_errorā€,ā€œattributesā€:{ā€œcodeā€:ā€œINVALID_FIELDā€,ā€œdetailsā€:{ā€œfieldā€:ā€œappearance.editorā€,ā€œcodeā€:ā€œVALIDATION_INVALIDā€,ā€œmessageā€:ā€œincompatible with field typeā€}}}]}
Exception details:
ApiException: 422 INVALID_FIELD (details: {ā€œfieldā€:ā€œappearance.editorā€,ā€œcodeā€:ā€œVALIDATION_INVALIDā€,ā€œmessageā€:ā€œincompatible with field typeā€})

It should be this id field that you can find in the responses here: https://www.datocms.com/docs/content-management-api/resources/plugin/instances

Could you maybe share the actual ID so that I can double check?

Thanks!

Our ID is - 21548

And hereā€™s response:

{ id: '21548',
  name: 'Gatsby Cloud',
  description:
   'Plugin that allows access to Gatsby Cloud Preview instances',
  packageName: 'datocms-plugin-gatsby-cloud',
  packageVersion: '0.1.7',
  pluginType: 'sidebar',
  fieldTypes: [ 'json' ],
  url:
   'https://unpkg.com/datocms-plugin-gatsby-cloud@0.1.7/dist/index.html',
  parameters:
   { authToken:
      'AUTH_TOKEN',
     instanceUrl: 'INSTANCE_URL',
     developmentMode: true },
  parameterDefinitions: { global: [ [Object], [Object], [Object] ], instance: [] } }

oh wait @m.sikora are you sure that you are getting the plugin ID from the plugins in the right environment?

Maybe you are referring the plugin ID from the primary env, but each env has its own plugins, so you should fetch the ID for each environment, are you doing that already? From the code youā€™ve shared before, looks like the ID is hard coded?

Youā€™re right, we were using id from primary environment. Now we create environment and get the id of plugin on this environment. Then we run migration with this id by command

dato migrate --token=ā€œMY_DATOCMS_FULL_ACCESS_TOKENā€ --source=ā€œtestā€ --inPlace

Now we get proper field in DatoCMS, but we also have an error:

ApiException: 422 MISSING_FIELDS (details: {ā€œrequired_fieldsā€:[ā€œgatsbypreviewā€,ā€œnameā€],ā€œmissing_fieldsā€:[ā€œgatsbypreviewā€]})

Are you getting this new error when creating the record? If so, you should pass a value for the gatsbypreview field, even if itā€™s empty.

On the create you should always pass all the fields, even if they are null.

Let me know if that solves your problem

Yes, we get this error when creating the record. We tried pass value of gatsbypreview field as null (the same error as above) and as empty JSON:

ApiException: 422 INVALID_FIELD (details: {ā€œfieldā€:ā€œgatsbypreviewā€,ā€œfield_idā€:ā€œ1978951ā€,ā€œfield_labelā€:ā€œGatsby Previewā€,ā€œerrorsā€:null,ā€œcodeā€:ā€œINVALID_FORMATā€,ā€œmessageā€:ā€œValue not acceptable for this field typeā€,ā€œfailing_valueā€:{}})

Could you please give us an example of properly set preview field? Here is our migration file:

module.exports = async client => {
	const articleModel = await client.itemTypes.create({
		name: 'Article',
		apiKey: 'article',
	});
	const titleField = await client.fields.create(articleModel.id, {
		label: 'Title',
		apiKey: 'title',
		fieldType: 'string',
		validators: {
			required: {},
		},
		appearance: {
			editor: 'single_line',
			parameters: {
				heading: true,
			},
			addons: [],
		},
	});

	const gatsbypre = await client.fields.create(articleModel.id, {
		label: 'Gatsby Preview',
		fieldType: 'json',
		apiKey: 'gatsbypreview',
		validators: {},
		appearance: {
			addons: [],
			editor: '23496',
			parameters: {},
		},
	});

	const article = await client.items.create({
		itemType: articleModel.id,
		title: 'My first article',
		gatsbypreview: null,
	});
};

Iā€™ve just tried this:

client.items.create({
	itemType: '236255',
	title: 'My first article',
	gatsbypreview: null,
});

and it worked for me. Maybe you have the gatsbypreview field marked as required?

Can you add records from the interface? Maybe that helps you?

Also remember that you can inspect all the calls that the browser does and replicate them yourself, maybe that helps you to troubleshoot your problem?