GraphQL Mock is a flexible and powerful tool for creating mock GraphQL servers. It allows you to define your schema, queries, and custom resolvers with ease, making it ideal for testing, prototyping, or developing front-end applications without relying on a real backend.
- Schema-Driven: Define your GraphQL schema using
.graphqls
files. - Configurable Queries: Easily specify queries, mutations, and subscriptions via a
queries.json
configuration file. - Custom Resolvers: Extend functionality with custom TypeScript scripts for complex resolver logic.
- Real-Time Subscriptions: Support for GraphQL subscriptions using
graphql-subscriptions
andsubscriptions-transport-ws
. - Hot Reloading: Automatically reloads schema and resolvers on file changes for a seamless development experience.
- Integrated Playground: Access the GraphQL Playground for interactive querying and testing.
Ensure you have Deno installed on your machine.
-
Clone the Repository
git clone https://github.com/yourusername/graphql_mock.git cd graphql_mock
-
Install Dependencies
GraphQL Mock uses both Deno and npm packages. Ensure you have Node.js installed to handle npm dependencies.
npm install
GraphQL Mock reads your schema and query configurations to start a mock GraphQL server.
--schema
or-s
: Path to your GraphQL schema file. Default isschema.graphqls
.--queries
or-q
: Path to your queries configuration file. Default isqueries.json
.--port
or-p
: Port number for the server to listen on. Default is4000
.
deno run --allow-net --allow-read index.ts --schema=path/to/schema.graphqls --queries=path/to/queries.json --port=4000
Example:
deno run --allow-net --allow-read index.ts
This command starts the server with default configurations:
- Schema:
schema.graphqls
- Queries:
queries.json
- Port:
4000
Define your GraphQL schema in a .graphqls
file. This schema outlines the types, queries, mutations, and subscriptions your mock server will support.
Example schema.graphqls
:
type Query {
hello: String
}
type Mutation {
setMessage(message: String!): String
}
type Subscription {
messageAdded: String
}
Configure your queries, mutations, and subscriptions in a queries.json
file. This file maps GraphQL operations to their mock implementations or static responses.
Example queries.json
:
{
"init": "scripts/init.ts",
"queries": [
{
"type": "Query",
"field": "hello",
"return": "Hello, world!"
},
{
"type": "Mutation",
"field": "setMessage",
"parameters": {
"message": "Hello"
},
"tsScript": "scripts/setMessage.ts",
"publish": [
{
"subscription": "messageAdded",
"payload": {
"messageAdded": "return"
}
}
]
},
{
"type": "Subscription",
"field": "messageAdded",
"schedule": {
"interval": 5000
},
"return": "New message received!"
}
],
"resolveReferences": [
{
"type": "User",
"tsScript": "scripts/resolveUser.ts"
}
]
}
For complex logic, you can define custom resolvers using TypeScript scripts. These scripts should export specific functions based on the operation type.
The init
script runs when the server starts or reloads. It's useful for initializing global state or setting up PubSub.
Example scripts/init.ts
:
export function init({ pubsub, globalState }) {
globalState.message = "Initial message";
pubsub.publish("messageAdded", { messageAdded: globalState.message });
}
Custom resolver for queries.
Example scripts/setMessage.ts
:
export async function mutate(type, field, args, { pubsub, globalState }) {
globalState.message = args.message;
pubsub.publish("messageAdded", { messageAdded: globalState.message });
return globalState.message;
}
Used for federated schemas to resolve references.
Example scripts/resolveUser.ts
:
export async function resolveReference(type, reference, { pubsub, globalState }) {
return {
id: reference.id,
name: "John Doe"
};
}
Start the server with your configuration:
deno run --allow-net --allow-read index.ts --schema=schema.graphqls --queries=queries.json --port=4000
-
Access GraphQL Playground:
Open http://localhost:4000/graphql in your browser to interact with the GraphQL Playground.
-
Subscriptions:
Subscriptions are available at the same endpoint using WebSockets.
Here's a simple example to get you started.
-
Define Schema (
schema.graphqls
):type Query { hello: String } type Mutation { setMessage(message: String!): String } type Subscription { messageAdded: String }
-
Configure Queries (
queries.json
):{ "init": "scripts/init.ts", "queries": [ { "type": "Query", "field": "hello", "return": "Hello, world!" }, { "type": "Mutation", "field": "setMessage", "tsScript": "scripts/setMessage.ts", "publish": [ { "subscription": "messageAdded", "payload": { "messageAdded": "return" } } ] }, { "type": "Subscription", "field": "messageAdded", "schedule": { "interval": 5000 }, "return": "New message received!" } ] }
-
Create Init Script (
scripts/init.ts
):export function init({ pubsub, globalState }) { globalState.message = "Initial message"; pubsub.publish("messageAdded", { messageAdded: globalState.message }); }
-
Create Mutation Resolver (
scripts/setMessage.ts
):export async function mutate(type, field, args, { pubsub, globalState }) { globalState.message = args.message; pubsub.publish("messageAdded", { messageAdded: globalState.message }); return globalState.message; }
-
Start the Server:
deno run --allow-net --allow-read index.ts
-
Interact via GraphQL Playground:
-
Query:
query { hello }
-
Mutation:
mutation { setMessage(message: "Hello, GraphQL Mock!") }
-
Subscription:
subscription { messageAdded }
-
Contributions are welcome! Please follow these steps:
-
Fork the Repository
-
Create a Feature Branch
git checkout -b feature/YourFeature
-
Commit Your Changes
git commit -m "Add YourFeature"
-
Push to the Branch
git push origin feature/YourFeature
-
Open a Pull Request
Please ensure your code follows the project's coding standards and includes relevant tests.
This project is licensed under the MIT License.