Easily restore your project to a previous version with our new Instant One-click Backup Recovery

Hygraph
Docs

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));

#Installation

Use the following command to install the package:

npm install @hygraph/management-sdk

#Usage

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

#Creating the client

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.

#Running a migration

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 migration

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

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

#Supported operations

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