Skip to main content

Create A Beam

Creating a beam involves mutating the underlying beam model using GraphQL API through the SDK's GQL service.

  1. Let's start by creating a new file
touch create-beam.ts
  1. Open this new file, import the SDK package and assign the graphQL client (from SDK services) to a variable
create-beam.ts
import getSDK from "@akashaorg/awf-sdk";

const gqlClient = getSDK().services.gql.client;
  1. Let's define a function to handle and return the response from the SDK service. This function will have two params, first parameter being the content of the beam we wish to create. This is an array that holds the ID of the individual blocks in the beam and their corresponding order. Learn more about content blocks here. The second parameter is optional (defaults to true) and specifies whether the beam is active or has beem removed by its author
create-beam.ts
import getSDK from "@akashaorg/awf-sdk";

const gqlClient = getSDK().services.gql.client;

const createBeamHandler = (
content: { blockID: any; order: number }[],
isBeamActive = true
) => {
try {
const response = await gqlClient.CreateBeam({});
} catch (error) {
console.log(`An error occured: ${error.message}`);
}
};
``;
  1. We need to pass some parameters to the CreateBeam method. This includes the required parameters like content, active createdAt, appID, appVersionID. Additionally, we shall log the response from this method, so we can see the newly created beam's id
create-beam.ts
import getSDK from "@akashaorg/awf-sdk";

const gqlClient = getSDK().services.gql.client;

const createBeamHandler = (
content: { blockID: any; order: number }[],
isBeamActive = true
) => {
try {
const response = await gqlClient.CreateBeam({});
const response = await gqlClient.CreateBeam({
i: {
content: {
content: content,
active: isBeamActive,
createdAt: new Date(),
appID: "application ID",
appVersionID: "application version ID",
},
},
});

// log the response document, and take note of the beam Id
console.log(response.node?.createAkashaBeam?.document);
} catch (error) {
console.log(`An error occured: ${error.message}`);
}
};
  1. Let's now run the function, passing to it the content of the beam we wish to create
create-beam.ts
import getSDK from "@akashaorg/awf-sdk";

const gqlClient = getSDK().services.gql.client;

const createBeamHandler = (
content: { blockID: any; order: number }[],
isBeamActive = true
) => {
try {
const response = await gqlClient.CreateBeam({
i: {
content: {
content: content,
active: isBeamActive,
createdAt: new Date(),
appID: "application ID",
appVersionID: "application version ID",
},
},
});

// log the response document, and take note of the beam Id
console.log(response.node?.createAkashaBeam?.document);
} catch (error) {
console.log(`An error occured: ${error.message}`);
}
};

createBeamHandler([
{ blockID: "block1", order: 1 },
{ blockID: "block2", order: 2 },
]);
tip

The id of the newly created beam can be got from the response document object using; response.node?.createAkashaBeam?.document?.id

Now that we have successfully created a beam, let's proceed to fetch the beam