@wuyuchentr/mock-graphql-server
v1.0.0
Published
Start a mock GraphQL server from SDL. Auto-generates data, supports pagination, latency, errors. Zero dependencies.
Maintainers
Readme
@wuyuchentr/mock-graphql-server
Start a mock GraphQL server from SDL. Auto-generates data, supports pagination, filtering, sorting, latency, and error simulation. Zero dependencies.
Perfect for frontend development and testing without a real backend.
Install
npm install -g @wuyuchentr/mock-graphql-serverUsage
CLI
# Start from a .graphql file
npx mock-graphql-server schema.graphql --port=4000
# With simulated latency
npx mock-graphql-server schema.graphql --latency=200
# With simulated errors (10%)
npx mock-graphql-server schema.graphql --error-rate=0.1API
const { mockServer } = require('@wuyuchentr/mock-graphql-server');
// From SDL string
const server = mockServer(`
type User {
id: ID!
name: String!
email: String!
age: Int
posts: [Post!]!
}
type Post {
id: ID!
title: String!
content: String!
}
type Query {
users(page: Int, limit: Int, sort: String, filter: String): [User!]!
user(id: ID!): User
}
`, {
latency: 100, // ms delay per request
errorRate: 0.05, // 5% chance of error
resolvers: {
Query: {
user: (args) => ({ id: args.id, name: 'Resolved User' }),
},
},
mocks: {
User: () => ({ name: 'Custom Mock User' }),
},
});
server.listen(4000, () => {
console.log('→ http://localhost:4000/graphql');
});Smart Mocking
Pagination
Queries with page and limit arguments get paginated results from a pool of 50 items.
query {
users(page: 1, limit: 10) { id name email }
}Sorting
query {
users(sort: "name", order: "desc") { id name }
}Filtering
query {
users(filter: "{\"status\": \"active\"}") { id name }
posts(filter: "{\"title\": {\"contains\": \"lorem\"}}") { id title }
}Supported operators: eq, ne, gt, gte, lt, lte, contains, in
Custom resolvers
resolvers: {
Query: {
user: (args) => ({ id: args.id, name: 'Bob' }),
users: (args) => [{ id: 1, name: 'Bob' }, { id: 2, name: 'Alice' }],
},
}How it works
- Parses the SDL to extract type definitions
- Generates a pool of mock data per query using field-name-aware heuristics
- Runs a lightweight HTTP server (Node.js
httpmodule) - Responds to
POST /graphqlrequests - Applies pagination, filtering, sorting from query arguments
- Injects configurable latency / error rate
