Skip to main content

Mock Server

This package facilitates the creation of a mock server that generates various API responses. These responses can then be utilized in other services, web applications, or GraphQL subGraphs.

Usage

To create a Mock 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 Server package

    yarn add @genesisx/mock-server
  • Now follow either of the two modes(mentioned below) to use this generator - either the Nx Console or CLI

Via Nx Console

In your Visual Studio Code, navigate to Nx Console > Generate > @genesisx/mock-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

To integrate this package in your application, use below command:

nx generate @genesisx/mock-server:templates <mockAppName> <portNumber>

Testing the app

You can test the newly created app using the Nx serve command

nx serve <appName>

Running build and lints

To execute the lint checks on this package, run

nx lint mock-server

To build this package, run

nx build mock-server

To execute the unit tests via Jest, run

nx test mock-server

Using the mock server for Sub Graphs / Services

  • In the generated app (Graphql Service) use the mock server host url
  • Open workspace>/apps/<graphql-service-app>/.env
  • Change the attribute <graphql-service-app>_SERVICE_HOST value to : the mock server web address

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-server/README.md