How can i mock in 8base?

i was wondering how can i can i mock on 8 base using cutom functions


Can you describe your problem in detail, please, preferably with some examples?
If you want to invoke function with some arguments you can use this command:
8base invoke resolverName -p stc/mocks/resolverMock.json

Lada Kokotova | Technical Support Engineer

Hey @jetixsolorzano

I’m going to provide examples assuming that you’re using the 8base CLI to generate the functions. Here’s an example of mocking a webhook function.

  1. Generate a serverless function:
$ 8base g webhook myExampleFunction -s=js` 

=> Updated file 8base.yml
=> Created file src/webhooks/myExampleFunction/handler.js
=> Created file src/webhooks/myExampleFunction/mocks/request.json
  1. Generate as many different mock files as you want:
$ 8base g mock myExampleFunction -m mock1Name

=> Created file src/webhooks/myExampleFunction/mocks/mock1Name.json

You can create as many mocks as you want. Each one allows you to test how your function responds to different arguments when invoking it locally. By default, a mock file looks like this:

  "data": {
    "foo": "bar"
  "headers": {
    "x-header-1": "header value"
  "body": "{\"foo\":\"bar\"}"

The object in your mock file is the EXACT object that will get passed to your serverless function when running the invoke-local command with the -m (mark flag). You’re able to add any data values, headers, or a stringified body argument that you plan on expecting as the arguments passed to your function in production.

  1. Test your function – by invoking it locally - and specify which mock you want to use in the test.
$ 8base invoke-local myExampleFunction -m mock1Name

=> Result: {
  "statusCode": 200,
  "body": "{\"result\":\"Webhook recieved: bar\"}"

Simply change the value of the -m flag with the mock filename that you want used in the test. If you’re not using the default directory structure, you can pass a relative path to the mock file you want to use as well (using -p flag).

  1. In Function Documentation:

Whenever you generate a function using the generate (g) command, it will generate documentation in the top of the handler.(js|ts) file that gives you help on using mocks.

 * This file was generated using 8base CLI.
 * To learn more about writing custom GraphQL resolver functions, visit
 * the 8base documentation at:
 * To update this functions invocation settings, update its configuration block
 * in the projects 8base.yml file:
 *  functions:
 *    myExampleFunction:
 *      ...
 * Data that is sent to the function can be accessed on the event argument at:
 * There are two ways to invoke this function locally:
 *  (1) Explicit file mock file path using '-p' flag:
 *    8base invoke-local myExampleFunction -p src/resolvers/myExampleFunction/mocks/request.json
 *  (2) Default mock file location using -m flag:
 *    8base invoke-local myExampleFunction -m request
 *  Add new mocks to this function to test different input arguments. Mocks can easily be generated
 *  the following generator command:
 *    8base generate mock myExampleFunction -m [MOCK_FILE_NAME]

Hope this helps.

1 Like

Is it possible to get a mock response when i send a mutation to my work space when there is no database?

We don’t have a built in capability for stubing API calls/responses in functions run locally that use the ctx.api.gqlRequest module. You’d have to overwrite that module in your local environment so that a custom module is replaced which returns a suitable response based on the query/mutation you’re running.

how can overwrite that module in your local environment ?

Fork the 8base SDK and refactor it to your needs.

Or, set an environment variable in your local environment like IS_LOCAL_ENV = true that you check in your functions and swap out the module for something else.

import MyCustomAPIStubber from '../relativePlace'

export default (event, ctx) => {
  const api = process.env.IS_LOCAL_ENV ? MyCustomAPIStubber : ctx.api

  /* Write your function code */