Importing External Modules
So far, the functinos we've looked at have been generated by helpers like the CRUD helpers from the Prisma runtime or the the Random runtime's generate
helper. The deno.policy
function we used for authoring policies was also based on function objects. All these helpers are shorthands for creating function objects and now we'll look at how to roll a custom function ourselves. We'll be using the Deno runtime to run our code.
Instead of including the code inline through a string, the Deno runtime allows us to import modules from disk. Our modules are allowed to use ESM imports to access libraries on different registries like npm
and deno.land
. We'll use these features to write a simple function that converts markdown to html.
import * as marked from "https://deno.land/x/marked/mod.ts";
export function parse({ raw }: { raw: string }): string {
return marked.parse(raw);
}
We'll expose our module using the deno runtime.
typegraph("roadmap", (g) => {
// ...
g.expose(
{
// ..
parse_markdown: deno.import(t.struct({ raw: t.string() }), t.string(), {
name: "parse",
// the path is parsed relative to the typegraph file
module: "md2html.ts",
}),
// ..
},
pub,
);
});
We can now access our func through the GraphQl api.