@hygraph/management-sdk
v1.2.5
Published
```js import { Client } from "@hygraph/management-sdk";
Downloads
8,912
Maintainers
Keywords
Readme
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))
Install
npm install @hygraph/management-sdk
Usage
To use the management sdk you need to instantiate a client which allows you to interact with the Hygraph Management API.
Creating the client
To create the management sdk client you need to pass the following parameters:
Authentication Token
authToken
.Can be retrieved from
Settings > API Access > Permanent Auth Tokens
on https://app.hygraph.com. 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
Settings > Environments > Endpoints
on https://app.hygraph.comMigration Name
name
[optional].Every migration has a unique name. If unspecified, a name would be generated and will be part of the response of a successful migration.
Subsequent migrations with same name will fail.
const { Client } = require("@hygraph/management-sdk");
const client = new Client({
authToken,
endpoint,
name, // optional
});
Running a Migration
The run
method runs the migration.
By default, migrations run in the foreground, meaning that the SDK client will return the results of all actions that were executed in the Management API. Passing an optional boolean argument as false configures the migration to run in the background. A successful result here does only mean that the actions were successfully scheduled, but not executed.
const result = await client.run(foreground);
if (result.errors) {
console.log(result.errors);
} else {
console.log(result.name);
}
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
).