mgdg
    TypeScript icon, indicating that this package has built-in type declarations

    1.1.2 • Public • Published

    Mock GraphQL Data Generator

    Takes a valid GraphQL Schema + Data Resolvers and generates valid query and mutation responses

    Usage

    Define Resolvers

    // resolvers/Rocket.ts
    
    import { MockResolvedValue, ShallowProperties } from 'mgdg'
    import { Rocket } from '../../api'
    import faker from 'faker'
    
    const MockRocket = (
      _parent,
      args,
    ): MockResolvedValue<ShallowProperties<Rocket>> => {
      return {
        id: () => faker.random.uuid(),
        name: () => faker.random.word(),
        type: () => faker.random.word(),
      }
    }
    
    export default MockRocket

    Setup Mock Server

    // mocker.ts
    
    import MockFactory from 'mgdg'
    
    // Valid GraphQL Schema
    import schemaJson from './schema'
    
    import Rocket from './resolvers/Rocket'
    
    export default new MockFactory(
      schemaJson as any,
      { mocks: { Rocket }
    )

    Execute Query

    import gql from 'graphql-tag'
    
    import mocker from './mocker'
    
    const query = gql`
      query Rocket($id: ID!) {
        rocket(id: $id) {
          id
          name
          type
        }
      }
    `
    
    const rocketResults = mocker.mockQuery(query, { variables: { id: 'rocket-1' } })
    
    console.log(rocketResults)

    For more details, see this demo

    Install

    npm i mgdg

    DownloadsWeekly Downloads

    1,820

    Version

    1.1.2

    License

    ISC

    Unpacked Size

    42.8 kB

    Total Files

    22

    Last publish

    Collaborators

    • donovanhiland