Management SDK Quickstart

import { Client } from '@hygraph/management-sdk';
const client = new Client({
authToken: '...',
endpoint: '...',
});
const run = async () => {
client.createModel({
apiId: 'Post',
apiIdPlural: 'Posts',
displayName: 'Post',
});
const result = await client.run(true);
if (result.errors) {
throw new Error(result.errors);
}
return result;
};
run()
.then((result) => console.log(`Finished migration at: ${result.finishedAt}`))
.catch((err) => console.error('Error: ', err));

InstallationAnchor

Use the following command to install the package:

npm install @hygraph/management-sdk

UsageAnchor

To use the Management SDK you need to instantiate a client.

Creating the clientAnchor

To create the Management SDK client you need to pass the following parameters:

const { Client } = require('@hygraph/management-sdk');
const client = new Client({
authToken,
endpoint,
name, // optional
});
  • Authentication Token authToken: This can be retrieved from your Hygraph project in Settings > API Access > Permanent Auth Tokens. Make sure the token has proper management permissions depending on what you plan to execute via the SDK.
  • Hygraph Content API Endpoint endpoint: Endpoint of the Content API that belongs to the environment that you plan to interact with. The URL can be retrieved from your Hygraph project in Settings > Environments > Endpoints.
  • Migration Name name [optional]: Every migration has a unique name within an environment. If unspecified, a name will be generated and will be part of the response of a successful migration. Subsequent migrations with the same name in the same environment will fail.

At the moment, Hygraph only stores metadata for the last 30 migrations for each environment.

Every single time a new migration is applied to an environment, metadata regarding older migrations exceeding 30 - sorted by time of creation - are deleted. This means changes performed by older migrations do stay in place, however, the name and other information related to this migration will no longer be available.

Running a migrationAnchor

The run method runs the migration.

const result = await client.run(foreground);
if (result.errors) {
console.log(result.errors);
} else {
console.log(result.name);
}

By default, migrations run in the foreground, meaning that the SDK client will return the results of all actions that were executed. Passing an optional boolean argument as false configures the migration to run in the background. A successful result here only means that the actions were successfully scheduled, but not executed.

Dry run a migrationAnchor

A migration can be dry run to preview what changes would be applied.

const changes = client.dryRun();
console.log(changes);

Supported operationsAnchor

All operations that can be executed by the SDK can be found in the TypeScript Type Definitions (Client.d.ts).