Skip to main content

Custom functions

Custom functions can be used to run custom code at different points of a typegraph. These constructs fall under functions which are, concretly, operations associated to a specific runtime. For some common tasks, like simple operations on database tables for example, runtime implementations provide function generators to minimize boilerplate. For cases not expressible by generators, runtimes like the DenoRuntime allow us to write more powerful custom functions.

Custom functions are commonly used for:

  • Specialized business logic to respond directly to incoming requests
  • Authentication policy logic

The following example uses the DenoRuntime to respond to requests and define a policy.

Loading...

Note that for the fib root function, we're using a typescript module in an external file. Here's what scripts/fib.ts looks like:

Loading...

The following runtimes can be used to run custom functions:

Accessing function context

Beta

The following feature is currently only implemented for the DenoRuntime.

On some runtimes, custom functions are passed to the context object along with the function inputs. This object provides access to all kinds of information about the context in which the function is running. The following example illustrates availaible fields:

Loading...

Note, the typescript version of the sample uses a closure instead of a string snippet to define the function. This is a simple syntax sugar availaible when using DenoRuntime through the typescript sdk or the PythonRuntime the python one. Consult the reference for each runtime to look at what's availaible.

Accessing the typegraph

Beta

The following feature is currently only implemented for the DenoRuntime.

To do anything meaningful with custom functions, you'll want to access the rest of functionality implemented on your typegraph. The primary way of doing this is by sending GraphqQl queries from within your function. On the DenoRuntime, to make this easier, there's a gql object passed to all functions. The following exapmle illustrates how it functions:

Loading...

And scripts/createVote.ts looks like:

Loading...

Generating types

Beta

The following feature is not yet stable.

Custom functions can be made smoother to develop or more robust to changes by using the metagen to generate code for them. By using the generated types along with static analysis tools in your workflow, this allows you to avoid many cases of drift between your custom functions and your typegraph. Not to mention improving the development experience of writing custom functions. The code generated for Python and Rust targets also includes the serialization boilerplate and utilities that you'd need for those to even get started in those languages.

The metagen library is availaible within the meta CLI or the typegraph SDK. In the following example we'll use the CLI to generate typescript code for a simple typegraph.

Loading...

We'll need to configure metagen through the metatype.yaml file. We add a new target to the metagen section that does what we need.

metagen:
targets:
# named targets to configure a bunch of generators together
metagen_deno:
- generator: fdk_typescript # generator to use
# path to generate to
path: ./metagen/ts/
# point to the typegraph location
# or name of typegraph on a typegate somehwere
typegraph_path: ./metagen-deno.ts

We can configure multiple target in our config file. Each target can have mutiple generator configurations. Metagen currently implements for the Typescript, Python and Rust targets.

We can then invoke the target using the following command:

meta gen metagen_deno

This results in the following files.

❯ lsd --tree metagen
 metagen
└──  ts
└──  fdk.ts

Within fdk.ts and the types and helpers, all the types from your typegraph should be there. Something like:

Code generation sample. Collapsed for aesthetic reasons.
Loading...

Note, this also include typescript function types for specific typegraph functions. By default, the fdk_typescript generator will only include stub function types for those defined on the DenoRuntime but this is configurable.

We can then use these types in the following manner. Add the following snippet into metagen/ts/remix.ts.

Loading...

Ofcourse, we'll need to include both our implementation and the generated code when defining our typegraph functions.