1.0.18 • Published 1 month ago

graphql-mock-data-generator v1.0.18

Weekly downloads
-
License
ISC
Repository
-
Last release
1 month ago

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 ScalarWhat it generates
AvatarURL to an avatar of user
VideoPreviewURL to a video
PhotoPreviewURL to an image
TimestampA recent timestamp
EmailAn e-mail ID
IntInteger
LongLong integer
FloatFloat
Booleantrue/false
PhoneA Phone number
NameFull name (possibly with title sometimes)
UrlA random URL
AddressA full address
SentenceA loreum ipsum sentence
ParagraphA loreum paragraph
ObjectJSON without predefined type
StringA loreum word
IDUUID 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

1.0.18

1 month ago

1.0.13

1 month ago

1.0.17

10 months ago

1.0.16

10 months ago

1.0.15

10 months ago

1.0.14

10 months ago

1.0.12

10 months ago