Create A Beam
Creating a beam involves mutating the underlying beam model using GraphQL API through the SDK's GQL service.
A beam consists of content blocks which could be formed in an editor. In order to create a beam, we need to specify a list containing the id(s) of the content block(s) in that beam. This means that before a beam is created, its individual content block(s) need to be created and stored in the content block model, first, then their respective ids will be referenced and saved to the beam model.
To better understand this tutorial, it is important that you go through creating a content block tutorial first
- Let's start by creating a new file
touch create-beam.ts
- Open this new file, import the SDK package and assign the graphQL client (from SDK services) to a variable
import getSDK from "@akashaorg/awf-sdk";
const gqlClient = getSDK().services.gql.client;
- 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 content blocks in the beam and their correspondingorder
. The second parameter is optional (defaults totrue
) and specifies whether the beam is active or has beem removed by its author
import getSDK from "@akashaorg/awf-sdk";
const gqlClient = getSDK().services.gql.client;
const createBeamHandler = async (
beamContent: { blockID: any; order: number }[],
isBeamActive = true
) => {
try {
const response = await gqlClient.CreateBeam({});
} catch (error) {
console.log(`An error occured: ${error.message}`);
}
};
- We need to pass some parameters to the
CreateBeam
method. This includes the required parameters likecontent
,active
createdAt
,appID
,appVersionID
. Additionally, we shall log the response from this method, so we can see the newly created beam's id
import getSDK from "@akashaorg/awf-sdk";
const gqlClient = getSDK().services.gql.client;
const createBeamHandler = async (
beamContent: { blockID: any; order: number }[],
isBeamActive = true
) => {
try {
const response = await gqlClient.CreateBeam({});
const response = await gqlClient.CreateBeam({
i: {
content: {
active: isBeamActive,
content: beamContent,
createdAt: new Date(),
appID: "application ID",
appVersionID: "application version ID",
},
},
});
// log the response document, and take note of the beam Id
console.log(response.data?.createAkashaBeam?.document);
} catch (error) {
console.log(`An error occured: ${error.message}`);
}
};
- Let's now run the function, passing to it the
content
of the beam we wish to create
import getSDK from "@akashaorg/awf-sdk";
const gqlClient = getSDK().services.gql.client;
const createBeamHandler = async (
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.data?.createAkashaBeam?.document);
} catch (error) {
console.log(`An error occured: ${error.message}`);
}
};
createBeamHandler([
{ blockID: "block1", order: 1 },
{ blockID: "block2", order: 2 },
]);
The id of the newly created beam can be got from the response document object using;
response.data?.createAkashaBeam?.document?.id
Congratulations! We have just created our first beam, let's proceed to fetch the beam