npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphql-fake-data-generator

v0.2.9

Published

## Description

Downloads

32

Readme

GraphQL Fake Data Generator

Description

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.

Installation

npm install graphql-fake-data-generator --save-dev
yarn add -D graphql-fake-data-generator
pnpm add -D graphql-fake-data-generator

Basic Usage

Create Schema from String

To create a GraphQL schema from a string:

const schemaString = `
  type Query {
    users: [User]
  }
  type User {
    id: ID
    name: String
  }
`

Parse Schema

Parse the schema to convert it into a usable format:

import { parseGraphQLSchema } from 'graphql-fake-data-generator'

const parsedSchema = parseGraphQLSchema(schema)

Analyze Schema

Analyze the parsed schema to understand its structure:

import { analyzeSchema } from 'graphql-fake-data-generator'

const analyzedSchema = analyzeSchema(parsedSchema)

Generate Mock Data

Generate mock data based on the analyzed schema:

import { generateMockData } from 'graphql-fake-data-generator'

const mockData = generateMockData(analyzedSchema)

Example:

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)

Result:

{
  users: [
    {
       id": "14c56ec5-1f79-4d9e-aab9-759ba6dcc950",
       name": "terra",
       email": "conscendo"
    }
  ]
}

Load Schema from File

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)

Generate Mock Data with Options

Customizing List Length

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.

Customizing Data

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.

Output

{
  users: [
    {
      id=: "14c56ec5-1f79-4d9e-aab9-759ba6dcc950",
      name: "Claude Hyatt",
      email: "[email protected]"
    }
  ]
}