Skip to main content

Fetch A Beam By Id

Fetching a beam by its id involves querying the underlying beam model using GraphQL API through the SDK's GQL service.

We can fetch the beam we just created in the previous tutorial using its id

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

const gqlClient = getSDK().services.gql.client;
  1. Again, let's define a function to handle and return the response from the SDK service. The function will have just one parameter which is the id of the beam we are about to fetch
fetch-beam-by-id.ts
import getSDK from "@akashaorg/awf-sdk";

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

const fetchBeamByIdHandler = async (beamId: string) => {
try {
const response = await gqlClient.GetBeamByDid({
id: beamId,
});

// the beam data is contained in a node
console.log(response.data?.node);
} catch (error) {
console.log(`An error occured: ${error.message}`);
}
};
  1. Let's run the function, passing to it the beamId variable
fetch-beam-by-id.ts
import getSDK from "@akashaorg/awf-sdk";

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

const fetchBeamByIdHandler = async (beamId: string) => {
try {
const response = await gqlClient.GetBeamByDid({
id: beamId,
});

// the beam data is contained in a node
console.log(response.data?.node);
} catch (error) {
console.log(`An error occured: ${error.message}`);
}
};

fetchBeamByIdHandler(
"kjzl6kcym7w8y84k406k7e4bo0u5jmglj4y7qj8nz60fqvvcqxkmr49kxsijpc1"
);

Congratulations, we have now fetched a beam using its id

How about we explore how to fetch a list of beams?