Elevate your development experience with an innovative mock server, seamlessly powered by filesystem integration, dynamic Handlebars templating, and realistic data generation with Faker.js
- No code changes. Just point your code to the mock server
- File system based mocks. Easily create, edit and version mocks alongside your code
- Dynamic and complex mocks. Use handlebars to create dynamic and complex mocks
- Realistic data. Easily generate sample data using fakerjs
- Zero configuration. Just run
faker-server graphql -s schema.graphql
Quickly start the server with a simple hello world example.
# Create a simple graphql schema
echo type Query { hello: String } > schema.graphql
# Create a simple handlebars template
echo "Hello, {{faker 'person.firstName'}}" > hello.hbs
npm exec @raysca/faker-server graphqlThe graphql server will be running on http://localhost:8080/api/graphql with a playground to test it out.
You can also use a remote schema by specifying the url to a graphql api. For example: Using the Shopify reference GraphQL api.
npm exec @raysca/faker-server graphql -s https://mock.shop/apiThe graphql server will be running on http://localhost:8080/api
Create a simple shop.hbs mock file
and query the mock server
query {
shop {
id
name
description
primaryDomain {
host
sslEnabled
url
}
}
}| Option | Description | Default |
|---|---|---|
-s, --schema |
Path to the graphql schema file. | current directory |
-m, --mocks |
Path to the mocks directory containing .hbs or .json files. |
current directory |
-p, --port |
Port to run the server on. | 8080 |
-e, --endpoint |
The graphql endpoint will be accessible from | /api/graphql |
-w, --watch |
Reload server if schema/mocks changes | false |
Usage: faker-server graphql --helpThere is a one-to-one mapping between graphql operations and mock files. This makes mocking intuitive and easy to understand. No complex configuration or code changes are required.
The server will look for a mock file in the mocks directory that matches the requested graphql operation. For example, if the graphql operation is getPerson then the server will look for a file called getPerson.hbs or getPerson.json in the mocks directory.
Here is an example graphql schema:
type Query {
getPerson: Person
people: [Person]
}
type Person {
name: String
phone: String
}Here is an example mocks/getPerson.hbs file:
{
"name": "{{faker 'person.firstName'}} {{faker 'person.lastName'}}",
"phone": "{{faker 'phone.phoneNumber'}}"
}
Here is an example mocks/people.hbs file:
[
{{#repeat 2}}
{
"name": "{{faker 'person.firstName'}} {{faker 'person.lastName'}}",
"phone": "{{faker 'phone.phoneNumber'}}"
}
{{/repeat}}
]
Whilst Fake GraphQL supports simple JSON file based mocks, the power of the
server shines when combined with handlebars templates and helpers.
The Fake GraphQL server has a number of built-in helpers to make it easy to generate random
data and more complex mocks. Here are the built-in helpers:
faker is used to generate random sample data in templates . It takes the fakerjs method as a parameter for example:
{
"firstName": "{{faker 'person.firstName'}}",
"lastName": "{{faker 'person.lastName'}}",
"phone": "{{faker 'phone.number'}}",
"email": "{{faker 'internet.email'}}",
"weight": "{{faker 'number.float' min=100 max=200}}kg"
}All the faker methods are supported. See the fakerjs
The random helper is used to select a random value from a list of values. For
example:
{{random 'a' 'b' 'c'}}
The repeat helper is used to repeat a comma-separated block of code a number
of times. This is good for generating arrays of values For example:
[
{{#repeat 2}}
{
"applicable": true,
"code": "CODE-{{faker "string.alphanumeric" 5}}"
}
{{/repeat}}
]
Also see the examples directory for more examples.
The faker-server rest command can be used to create a REST mock server. It

