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-mock-data-generator

v1.0.18

Published

## Installation

Downloads

344

Readme

graphql-mock-data-generator

Installation

$ npm i graphql-mock-data-generator

Usage

Create a schema file

Create a schema file and copy the path with respect to the root directory

You can use these scalars to get relevant fake data generated

| Name of the Scalar | What it generates | |--------------------|-------------------------------------------| | Avatar | URL to an avatar of user | | VideoPreview | URL to a video | | PhotoPreview | URL to an image | | Timestamp | A recent timestamp | | Email | An e-mail ID | | Int | Integer | | Long | Long integer | | Float | Float | | Boolean | true/false | | Phone | A Phone number | | Name | Full name (possibly with title sometimes) | | Url | A random URL | | Address | A full address | | Sentence | A loreum ipsum sentence | | Paragraph | A loreum paragraph | | Object | JSON without predefined type | | String | A loreum word | | ID | UUID string |

You can use your custom enums also while building the schema.

To make a field return a constant value always, use the @const directive.

Here is an example config/schema.graphql file.

# config/schema.graphql
type User {
    userId: ID
    firstName: Name
    lastName: Name
    name: Name
    profileImageUrl: Avatar
}

enum Operator {
    AND
    OR
}

type Keyword {
    term: String
    keywordType: String
}

type Conditions {
    topicKeywords: Keyword
    operator: Operator
}

type Criteria {
    andConditions: Conditions
    excludeConditions: Conditions
}

type Topic {
    id: ID
    modifiedTime: Timestamp
    description: Paragraph
    name: Name
    canEdit: Boolean
    criterias: [Criteria]
    listeningMediaTypes: String
    ownerUser: User
    additional: String
    topicId: String @const(value: "1234")
}

Create a Run Control (Optional)

In the root of the project create a mock-gql.config.json file in case you want to override original configuration.

// mock-gql.config.json
{
  "schema": "config/schema.graphql", // path to schema file
  "mocks": 80, // number of mocks generated for each entity,
  "port": 4000
}

Add a script

Add a script in your package.json file

{
  "scripts": {
    "mock":"graphql-mock-data-generator"
  }
}

Run the script

$ npm run mock

An apollo server will be started

Schema generated successfully
Generating mock data...
🚀Visit the API at http://localhost:4000/

Querying

Open http://localhost:4000 and run this query,

query($input: FeedInput) {
    getTopics_mock(input: $input) {
        count
        hasMore
        items {
            ...SimpleTopics
        }
    }
}

fragment SimpleTopics on Topic {
    id
    name
    canEdit
    description
    ownerUser {
        userId
        name
        profileImageUrl
    }
    topicId
    additional
}

Mocking an error from the server

While using this server, you can generate errors by sending appropriate headers. You just need to send the statuscode (>=400) in the headers you want the server to respond with.

Here's an example of it inside the useQuery hook of @apollo/client in React

const query = gql`
...
`
const { data, loading, error } = useQuery(query, {
  variables: {
    ...
  },
  context: {
    headers: {
      statusCode: 400,
    },
  },
})

This will return a 400 error from the server