Create A Beam
Creating a beam involves mutating the underlying beam model using GraphQL API through the SDK's GQL service.
- 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
create-beam.ts
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
IDof the individual blocks in the beam and their correspondingorder. Learn more about content blocks here. The second parameter is optional (defaults totrue) 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}`);
}
};
``;
- We need to pass some parameters to the
CreateBeammethod. This includes the required parameters likecontent,activecreatedAt,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}`);
}
};
- Let's now run the function, passing to it the
contentof 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