Runtime Environment
The 8base runtime environment is a secure and isolated environment where 8base invokes your custom functions.
Function Arguments
Each function handler receives two arguments: event
and context
. The structure of the event
object depends on funcion type. The context
object contains useful properties and methods that are described below.
Context Object
Functions have a second context
argument passed to them that has the following properties:
workspaceId
: Property holds the ID of the current workspace.context.invokeFunction(taskName, args, options)
: Allows you to invoke other functions. To learn more, see [Custom Functions] (/backend/custom-functions).context.api.gqlRequest(query, variables, options)
: Provides a convenient way to interact with the 8base GraphQL API. When using theapi.gqlRequest
you don't have to worry about setting the URL or authentication header - it is done automatically. The query is executed under the authentication context of the user that made the request.
Using gqlRequest
to Call 8base API
The context.api.gqlRequest
method has the following signature: gqlRequest(query, variables, options)
. In addition to a GraphQL query and variables, you can pass the optional options
argument, which has two properties:
authorization
: Allows you to override the authorization token. For example, if you want to use an API token instead of the original user authorization token.checkPermissions
: Setting this tofalse
lets you execute a GraphQL operation with elevated permissions. This is useful when the logic inside of your function requires more permissions than the user making the request. You can think of it as equivalent tosudo
in Linux.
import gql from 'graphql-tag';
const query = gql`
query customer($id: ID!) {
customer(id: $id) {
id
name
}
}
`;
module.exports = async (event, context) => {
const { id } = event.data;
const { customer } = await context.api.gqlRequest(query, { id });
};
Environment Variables
You can set environment variables in 8base settings and they automatically get exposed in all functions through process.env.VARIABLE_NAME
.
Timeouts
Functions have maximum 20 seconds execution time by default. After the execution time limit is reached the execution times out and returns an error. If you need more than 20 seconds of continuous execution you can increase your timeout up to 900 seconds by specifying it in 8base.yml
. Check out the example in 8base Config
section of documentation.
What Happens after a Function Returns?
After your function returns, its execution is immediately frozen. This means that if you have any asynchronous processes still running, they will not be able to finish. There are several ways to deal with this:
- Use
await
to get a result from the asynchronous operation before returning from the function:
module.exports = async (event, context) => {
const response = await context.api.gqlRequest(QUERY);
return {
data: JSON.stringify(response),
};
};
- Return a promise:
module.exports = (event: any, context: any) => {
const promisedResult = new Promise((resolve, reject) => {
context.api.gqlRequest(QUERY).then((response) => {
resolve({
data: JSON.stringify(response),
});
});
});
return promisedResult;
};
- Spawn a background task that keeps running after your function returns. You can use the
context.invokeFunction
API on the Tasks page.
Managing Dependencies
You can add any dependencies using npm
or yarn
. When you run '8base deploy', 8base uploads your code to the cloud, runs npm install
and deploys the bundle to AWS Lambda. The ZIP file archive includes your project.
The total size of your application code and its dependencies cannot exceed 50 MB zipped or 250 MB unzipped.
We recommend that you have a package-lock.json
file to dramatically accelerate deployment. 8base checks whether package-lock.json
has changed since the last deploy and only installs dependencies when necessary.
Supported Languages and Runtime
8base invokes your Custom Functions in an secure and isolated runtime environment.
Custom Functions can be written in either JavaScript or TypeScript.
8base runtime environment currently supports:
- TypeScript: v4.3.4
- Node.js: v18.x and v20.x The 8base CLI was recently updated to support Node v18 and v20. For information about updating, see Command Line Interface (CLI).