Introduction
A simulation is only as good as its data
Fake data is hugely valuable for local development and testing, but how do we obtain it, and how do we keep it fresh?
🤔 Fixtures, or static snapshots of realistic records keep a shared resource of fake data, but maintaining them is a tedious game of whack-a-mole field and id updates.
🧐 Factories partially alleviate this problem by automating the generation of your fixtures. As your schema evolves, you can change a single factory rather than patch a thousand individual records. But a schema defines more than just the shape of its records. Just as important, it also defines the relationships between those records, and factories don't help you there.
🤩 Graphgen is a whole-meal solution for creating complete datasets that contain not only realistic records, but also realistic relationships between those records. Data generated by graphgen is always valid, so for example, if an organization has many members, then graphgen will always generate a realistic number of them.
Getting started
Graphgen is available on both npmjs.org and deno.land/x, see the installation guide to get going.
1. Get the code
Graphgen can be used in both Node and Deno, however most code examples will be for Node.
NPM
$ npm i @frontside/graphgen
import { createGraphGen } from "@frontside/graphgen";
Deno
import { createGraphGen } from "https://deno.land/x/graphgen/mod.ts";
2. Create your world
Graphgen uses GraphQL to express the shape of the
generated data, including relationships. Create a world.graphql
file and add a
type to it.
type Article {
title: String!
}
type Author {
name: String!
articles: [Article] @size(mean: 5, max: 20)
}
3. Create your generator
import { readFileSync } from "fs";
import { createGraphGen } from "@frontside/graphgen";
const source = readFileSync("./world.graphql");
const graphgen = createGraphGen({
source,
sourcename: "world.graphql",
});
4. Generate realistic data
let author = graphgen.create("Author");
for (let article of author.articles) {
console.log(`${author.name} wrote ${article.title}`);
}