Skip to main content

Mock API Server

This package facilitates the creation of a mock server that can generate a variety of API responses for use in other services, web applications, or GraphQL SubGraphs. By doing so, it enables the construction of a fundamental data set that can be queried, deleted, updated, and modified at any time when used in conjunction with Service and Federated layers.

Usage​

To create a Mock API Server, you can follow the steps outlined below:

Pre-requisites​

  • Make sure you are on correct version of Nodejs 18.x and Nx Version 16.6
    node --version
    nx --version
  • To start with, create a new GenesisX workspace in an empty directory, run below command, of if you already have the workspace ready , please skip this step.
    npx @genesisx/create-workspace
    • Provide the requested parameters
    • Provide name to your workspace like test-workspace
    • Move to the created workspace - cd test-workspace
    • Install the packages
      yarn install
  • Install Mock API Server package
    yarn add @genesisx/mock-api-server
  • Now follow either of the two modes(mentioned below) to use this generator - either the Nx Console or CLI

Via Nx Console​

Select Generate tool from the Nx Console as shown in the image in your Visual Studio

Navigate to NX console > Generate > @genesisx/mock-api-server

And fill in the form as follows;

  • appName - GQL Api Mock Service Name that will be under apps folder of your project

  • portNumber - Port Number to be changed in file called .env

Serve your mock service created through Nx Console > Serve > <your_project_name>

Via Command Line​

You may also Generate the plugin via the terminal in absence of the Nx console using the below command

nx generate @genesisx/mock-api-server:templates <mockServerName> <portNumber> --no-interactive

Testing the app​

You can test run the newly created app using the Nx Serve command

nx serve <MOCK_API-APP-NAME>

Running build and lints​

To execute the lint checks on this package, run

nx lint mock-api-server

To build this package, run

nx build mock-api-server

To execute the unit tests via Jest, run

nx test mock-api-server

Using the mock server in coalition with Sub Graphs (Services and Federated layers)​

In order to connect the mock server to Sub Graphs, you will first have to generate the needed services, then some files will have to be updated.

Service​

Within a generated <graphql-service-app>, you will need to use the portNumber of the Mock-api-server you selected earlier

  • Open <workspace-name>/apps/<graphql-service-app>/.env
  • Change the attribute within the .env file titled SERVICE_HOST to the localhost address of the mock server (i.e if the mock-api-server portNumber is 2300 then this number will be the value of the needed attribute).err

Federation​

This time navigating to a <graphql-federation-app> you will need to use the portNumber of the Service app you selected earlier.

  • This time navigating to <workspace-name>/apps/<graphql-federation-app>/.env
  • Update the empty GQL_SERVICE_URL_CUSTOMERS attribute to equate to the portNumber belonging to the graphql-service-app (i.e if the app address is 2400 then this number will be the value of the needed attribute).err

Test launching the app including SubGraphs​

You can test the subgraph layers using the Nx console

Navigate to Nx Console > Serve > <SERVICE_NAME> or <FEDERATION_NAME>

You can test the service application using the following bash command

nx serve <SERVICE_NAME>

as well as the federated layer with

nx serve <FEDERATION_NAME>

Note - in order to serve each layer without error you must run each command in a separate terminal in the following order

  • MOCK SERVER > SERVICE > FEDERATION

Local development and testing​

In order to run development tests on your local branch, you will need to build this package and refresh node_modules with every change. This can be done using the bash command below:

yarn nx run mock-api-server:build && cp -R dist/packages/mock-api-server/ <new-wsp>/node_modules/@genesisx
  • More details & Troubleshooting​

    Visit the troubleshooting guide for common issues or refer to the readme.md file located within the package folder <workspace-folder>/node_modules/@genesisx/mock-api-server/README.md