@hygraph/management-sdk
TypeScript icon, indicating that this package has built-in type declarations

1.2.4 • Public • Published

@hygraph/management-sdk

Programmatically manage Hygraph project schema via migrations.

Version Downloads/week
Join us on SlackLogin to Hygraph@Hygraphcom

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.com

  • Migration 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).

Readme

Keywords

none

Package Sidebar

Install

npm i @hygraph/management-sdk

Weekly Downloads

1,454

Version

1.2.4

License

MIT

Unpacked Size

217 kB

Total Files

27

Last publish

Collaborators

  • ozgur.uysal
  • bikappa
  • rajatsharma
  • jpedroschmitz
  • mahaveergcms
  • danielhygraph
  • graphcms-owner