Crate mockforge_graphql

Crate mockforge_graphql 

Source
Expand description

§MockForge GraphQL

GraphQL mocking library for MockForge with schema-based query execution.

This crate provides GraphQL mocking capabilities including:

  • Schema-Based Mocking: Define GraphQL schemas and automatically generate resolvers
  • Query & Mutation Support: Handle queries, mutations, and subscriptions
  • Type System: Full GraphQL type system support (scalars, objects, interfaces, unions)
  • Introspection: Built-in introspection queries for tooling
  • Playground Integration: GraphQL Playground UI for interactive testing

§Overview

MockForge GraphQL allows you to define GraphQL schemas and automatically mock resolvers with realistic data. Perfect for frontend development and integration testing.

§Quick Start

§Basic GraphQL Server

use mockforge_graphql::start;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    // Start GraphQL server on port 4000
    start(4000).await?;
    Ok(())
}

§With Custom Schema

use mockforge_graphql::{create_graphql_router, GraphQLSchema};
use mockforge_core::LatencyProfile;

let schema = GraphQLSchema::generate_basic_schema();
assert!(schema.schema().sdl().contains("type Query"));
let latency = Some(LatencyProfile::with_normal_distribution(80, 20.0));
let _router = create_graphql_router(latency).await?;

§GraphQL Schema Example

Define your GraphQL schema:

type Query {
  user(id: ID!): User
  users(limit: Int = 10): [User!]!
}

type Mutation {
  createUser(input: CreateUserInput!): User!
  updateUser(id: ID!, input: UpdateUserInput!): User!
}

type User {
  id: ID!
  name: String!
  email: String!
  posts: [Post!]!
}

type Post {
  id: ID!
  title: String!
  content: String!
  author: User!
}

input CreateUserInput {
  name: String!
  email: String!
}

MockForge automatically generates resolvers with realistic data:

# Query
curl -X POST http://localhost:4000/graphql \
  -H "Content-Type: application/json" \
  -d '{"query": "{ user(id: \"123\") { id name email } }"}'

# Mutation
curl -X POST http://localhost:4000/graphql \
  -H "Content-Type: application/json" \
  -d '{"query": "mutation { createUser(input: {name: \"Alice\", email: \"alice@example.com\"}) { id } }"}'

§GraphQL Playground

Access the interactive GraphQL Playground at:

http://localhost:4000/playground

The Playground provides:

  • Schema explorer
  • Query editor with auto-complete
  • Response viewer
  • Request history

§Features

§Automatic Resolver Generation

  • Generates realistic data based on field names and types
  • Maintains referential integrity between related types
  • Supports nested queries and relationships

§Latency Simulation

  • Simulate network delays for realistic testing
  • Per-resolver latency configuration
  • Random or fixed latency profiles

§Error Injection

  • Simulate GraphQL errors and partial responses
  • Configure error rates per resolver
  • Test error handling in client applications

§Key Modules

§Examples

See the examples directory for complete working examples.

§Documentation

Re-exports§

pub use executor::create_graphql_router;
pub use executor::start_graphql_server;
pub use executor::GraphQLExecutor;
pub use registry::GraphQLSchemaRegistry;
pub use schema::GraphQLSchema;
pub use graphql_tracing::create_graphql_span;
pub use graphql_tracing::create_resolver_span;
pub use graphql_tracing::record_graphql_error;
pub use graphql_tracing::record_graphql_success;
pub use graphql_tracing::record_resolver_error;
pub use graphql_tracing::record_resolver_success;

Modules§

executor
GraphQL execution engine
graphql_tracing
Distributed tracing for GraphQL queries and mutations
registry
GraphQL Schema Registry - SpecRegistry implementation for GraphQL
schema
GraphQL schema parsing and generation

Functions§

create_router
Create a GraphQL router with latency support
start
Start GraphQL server with default configuration
start_with_latency
Start GraphQL server with latency configuration