Skip to content

CLI

Install the apollo-mock binary for your platform with:

Terminal window
curl -fsSL https://raw.githubusercontent.com/apollographql/apollo-mock/main/install.sh | sh

1. Generate fake data

--graphql-dir points at a directory containing .graphql/.graphqls files: exactly one of them must be the schema (the file containing at least one type system definition, e.g. type/enum/ directive), the rest are treated as operation files (see Mocking operations below). sample/graphql has an example of both.

Terminal window
ollama pull llama3.2
apollo-mock generate --graphql-dir sample/graphql --output-dir data --provider ollama

Use --model to pick another model and --base-url for a non-default Ollama endpoint.

Alternatively, use the Anthropic API with --provider anthropic:

Terminal window
export ANTHROPIC_API_KEY=sk-ant-...
apollo-mock generate --graphql-dir sample/graphql --output-dir data --provider anthropic

The API key can also be passed with --api-key instead of the ANTHROPIC_API_KEY environment variable. Use --model to pick another Claude model (default: claude-haiku-4-5).

Or use the Vercel AI Gateway with --provider vercel, which can route to any model it supports using a creator/model name (e.g. openai/gpt-5-mini):

Terminal window
export AI_GATEWAY_API_KEY=...
apollo-mock generate --graphql-dir sample/graphql --output-dir data --provider vercel

The API key can also be passed with --gateway-api-key instead of the AI_GATEWAY_API_KEY environment variable. Use --model to pick another model (default: anthropic/claude-haiku-4.5) and --base-url for a non-default gateway endpoint.

For testing, --provider random fills the schema with random data (lorem ipsum text, random enum/number/boolean values) without calling any LLM:

Terminal window
apollo-mock generate --graphql-dir sample/graphql --output-dir data --provider random

This writes, under data/:

  • entity/<TypeName>/<id>.json: one file per schema-based fake entity. Scalar and enum fields are filled by the LLM; fields pointing to other types are linked with {__typename, id} references that the server hydrates at execution time. Regenerating with the same --output-dir keeps existing entities stable across schema changes: fields removed from the schema are dropped, only newly added fields are asked from the LLM, and ids never change.
  • operation/<operationName>.json: one file per named operation using the @mock directive (see below), mapping the GraphQL path in the document to the mocked value.

Mocking operations

Besides schema-based entities, individual operations can be mocked with the @mock directive:

directive @mock(hint: String, value: Any) on QUERY | MUTATION | SUBSCRIPTION | FIELD
  • @mock(value: ...) uses the given GraphQL literal verbatim.
  • @mock(hint: "...") passes the hint to the data provider, which generates a value matching the field’s (or, at the operation root, the whole selection set’s) shape.
query GetProducts {
products {
name
price @mock(value: 9.99)
category @mock(hint: "a category for kitchen appliances") {
name
}
}
}

writes operation/GetProducts.json as {"products.price": 9.99, "products.category": {"name": "..."}}. A @mock directive on the operation itself (as opposed to a field) mocks the whole response, stored under the empty-string path.

2. Serve the fake data

Terminal window
apollo-mock serve --schema sample/graphql/schema.graphqls --data data

Then:

Terminal window
curl -s http://localhost:4000/graphql \
-H "Content-Type: application/json" \
-d '{"query": "{ products { name price category { name } reviews { rating author } } }"}'

Pass --port 0 to have the OS pick a free port automatically.