GraphQL Fake Data Generator is a powerful and flexible tool designed to generate mock data for GraphQL schemas. It provides an easy and efficient way to create realistic and customizable mock data based on your GraphQL schema, which is ideal for testing, development, and demonstration purposes.
npm install graphql-fake-data-generator --save-dev
yarn add -D graphql-fake-data-generator
pnpm add -D graphql-fake-data-generator
To create a GraphQL schema from a string:
const schemaString = `
type Query {
users: [User]
}
type User {
id: ID
name: String
}
`
Parse the schema to convert it into a usable format:
import { parseGraphQLSchema } from 'graphql-fake-data-generator'
const parsedSchema = parseGraphQLSchema(schema)
Analyze the parsed schema to understand its structure:
import { analyzeSchema } from 'graphql-fake-data-generator'
const analyzedSchema = analyzeSchema(parsedSchema)
Generate mock data based on the analyzed schema:
import { generateMockData } from 'graphql-fake-data-generator'
const mockData = generateMockData(analyzedSchema)
import { parseGraphQLSchema, analyzeSchema, generateMockData } from 'graphql-fake-data-generator'
const schemaString = `
type Query {
users: [User]
}
type User {
id: ID
name: String
}
`
const parsedSchema = parseGraphQLSchema(schema)
const analyzedSchema = analyzeSchema(parsedSchema)
const mockData = generateMockData(analyzedSchema)
{
users: [
{
id": "14c56ec5-1f79-4d9e-aab9-759ba6dcc950",
name": "terra",
email": "conscendo"
}
]
}
To load a GraphQL schema from a file:
import { readFileSync } from 'fs'
import { parseGraphQLSchema, analyzeSchema, generateMockData } from 'graphql-fake-data-generator'
// Method 1: Read existing GraphQL schema file and convert it into string.
const schemaString = readFileSync('path/to/your/schema.graphql', 'utf-8')
const parsedSchema = parseGraphQLSchema(schemaString)
// Method 2: Pass the GraphQL schema file path directly to the parseGraphQLSchema function.
const parsedSchema = parseGraphQLSchema('./schema.graphql')
const analyzedSchema = analyzeSchema(parsedSchema)
const mockData = generateMockData(analyzedSchema)
Specify the length of lists in the generated mock data:
const mockDataWithListLength = generateMockData(analyzedSchema, {
listLength: 10, // Specifies the number of items in list fields
})
NB: Please be aware that if your schema does not return an array, the 'listLength' parameter will be unavailable. Including it in the options will result in an error.
Provide custom data generators for specific fields: If the default data generated appears unrealistic, you have the option to customize the output for specific fields
import { faker } from 'faker-js/faker'
const mockDataWithCustomData = generateMockData(analyzedSchema, {
query: {
name: () => faker.person.fullName(),
email: () => faker.internet.email(),
},
})
In the example above, the name
and email
fields of the users
query will use the provided custom generator functions. This allows for specific customization of the mock data returned for these fields.
{
users: [
{
id=: "14c56ec5-1f79-4d9e-aab9-759ba6dcc950",
name: "Claude Hyatt",
email: "Janessa23@hotmail.com"
}
]
}