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-codegen-faker-fixtures

v0.7.2

Published

Generate faker fixtures for your fields

Downloads

3,524

Readme

graphql-codegen-faker-fixtures

This repository contains a GraphQL Codegen plugin that generates fixture builders of your fragments based on your GraphQL schema and populates them with faker.

Installation

Run either of the following commands to add the package and it's dependencies, based on your package manager of choice:

# pnpm
pnpm i -D graphql-codegen-faker-fixtures @faker-js/faker deepmerge-ts

# npm
npm i --save-dev graphql-codegen-faker-fixtures @faker-js/faker deepmerge-ts

# yarn
yarn add -D graphql-codegen-faker-fixtures @faker-js/faker deepmerge-ts

Usage

Config file

Both codegen.yml and codegen.ts config files are permitted. It is recommended to use the typescript format which includes a config interface.

import type { CodegenConfig } from "graphql-codegen-faker-fixtures";

const config: CodegenConfig = {
  verbose: true,
  schema: "example/schema.graphql",
  documents: "example/queries.ts",
  generates: {
    "example/fixture_builders.ts": {
      plugins: ["graphql-codegen-faker-fixtures"],
      config: {
        typeImport: "@types",
        fakerjsSeed: 98765,
        scalars: {
          Email: {
            _default: "faker.internet.email()",
            secondaryEmail: "faker.internet.exampleEmail()",
          },
          String: {
            _default: "faker.lorem.word()",
            firstName: "faker.person.firstName()",
            lastName: "faker.person.lastName()",
            phone: "faker.phone.number()",
          },
          Boolean: { married: "faker.helpers.arrayElement(['yes', 'no'])" },
          ID: {
            id: "faker.string.symbol()",
            ["Person.id"]: "faker.string.nanoid()",
          },
        },
      },
    },
  },
  hooks: {
    afterOneFileWrite: ["prettier --write"],
  },
};

export default config;
schema: example/schema.graphql
documents: example/queries.ts
verbose: true
generates:
  example/fixture_builders.ts:
    plugins:
      - graphql-codegen-faker-fixtures
    config:
      typeImport: "@types"
      fakerjsSeed: 98765
      scalars:
        Email:
          _default: faker.internet.email()
          secondaryEmail: faker.internet.exampleEmail()
        String:
          _default: faker.lorem.word()
          firstName: faker.person.firstName()
          lastName: faker.person.lastName()
          phone: faker.phone.number()
        Boolean:
          married: faker.helpers.arrayElement(['yes', 'no'])
        ID:
          id: faker.string.symbol()
          Person.id: faker.string.nanoid()
hooks:
  afterOneFileWrite:
    - prettier --write

The example above and a copy in yml format can be found in the /examle directory and can be run with pnpm example:ts and pnpm example:yml respectively.

API

The config object includes the following fields:

buildersOnly (boolean)

When true the plugin will only return the fixture builder functions without the normal addition of a disclaimer, utility functions (repeat, deepmerge & random), utilty type (DeepPartial), and faker seed setup.

fakerjsSeed (number)

default value: 12345654321

Number used to seed faker. Reference

namingConvention ("keep" | "change-case-all#<case-type>")

default value: change-case-all#pascalCase

supported <case-type> options: camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase

Naming convention used in fixture builder generation. Reference

scalars (ScalarConfig)

Override default faker methods for a given scalar type. The default methods and configured scalar are:

| Scalar | Default faker method | | ------------- | -------------------------- | | Int | faker.number.int() | | Float | faker.number.float() | | Boolean | faker.datatype.boolean() | | ID | faker.string.uuid() | | String | faker.lorem.words() | | custom scalar | faker.lorem.words() |

Overriding a faker methods:

scalars: {
    String: {
        // Override the default method for the String scalar
        _default: "faker.lorem.word()"
        // Provide a faker method for a specific field name for the String scalar
        firstName: "faker.person.firstName()"
    },
    ID: {
        // Provide a faker method for a specific field name and type for the ID scalar
        ["Person.id"]: "faker.string.nanoid()",
    }
}

skipFields (string[])

Skip the given fields while generating the fixture builders. The notation is as follows:

skipFields: ['PersonFragment.email'],

skipFragments (string[])

Skip the given fragments while generating the fixture builders. The notation is as follows:

skipFragments: ['person'],

typeImport (string)

Import types generated from your fragments based on your GraphQL schema. These types provide typing of the fixture builder functions.

Development

Assuming you have pnpm installed with node@18, run:

pnpm install
pnpm start

Publishing

Publishing is done automatically via merging a changesets release pull request to main.