Skip to content

Listing Materials

This example demonstrates how to query and paginate through available materials in the Helio Additive platform.

Create the following GraphQL query file, then run your language’s codegen tool to generate the types.

queries/Materials.graphql
query Materials($page: Int, $pageSize: Int) {
materials(page: $page, pageSize: $pageSize) {
pages
pageInfo {
hasPreviousPage
hasNextPage
}
objects {
... on Material {
id
name
brand {
id
name
}
feedstock
density
minExtrusionTemp
maxExtrusionTemp
}
}
}
}

API Reference:

  1. Setup the client and configuration

    index.ts
    import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client";
    import { MaterialsDocument } from "./queries/__generated/Materials.generated";
    // Load PAT from environment
    const PAT = process.env.PAT;
    if (!PAT) {
    throw new Error("PAT environment variable is required");
    }
    // Create Apollo Client
    const client = new ApolloClient({
    cache: new InMemoryCache(),
    link: new HttpLink({
    uri: "https://api.helioadditive.com/graphql",
    headers: {
    "Content-Type": "application/json",
    Authorization: `Bearer ${PAT}`,
    },
    }),
    });
  2. Query and paginate through materials

    Fetch materials page by page until all materials have been retrieved.

    async function listMaterials() {
    let page = 1;
    const pageSize = 20;
    const allMaterials: Array<{ id: string; name: string }> = [];
    // Paginate through all materials
    while (true) {
    const { data } = await client.query({
    query: MaterialsDocument,
    variables: { page, pageSize },
    fetchPolicy: "no-cache",
    });
    // Collect materials from this page
    for (const material of data?.materials?.objects ?? []) {
    if (material.__typename === "Material") {
    allMaterials.push({
    id: material.id,
    name: material.name,
    });
    console.log(`Material: ${material.name} (${material.brand?.name})`);
    }
    }
    // Check if there are more pages
    if (!data?.materials?.pageInfo?.hasNextPage) {
    break;
    }
    page++;
    }
    return allMaterials;
    }
  3. Put it all together

    Run the function and display the results.

    // Example usage
    listMaterials()
    .then((materials) => {
    console.log(`\nTotal materials found: ${materials.length}`);
    })
    .catch(console.error);
  1. Create a .env.local file with your PAT:

    Terminal window
    PAT="your-personal-access-token"
  2. Run the example:

    Terminal window
    bun run index.ts