Skip to main content

Module generators

Module generators 

Source
Expand description

Generator trait and registry used by ResponseBuilder to produce values for GraphQL types, plus the built-in generators for the standard GraphQL scalars.

§Built-in defaults

Generators::default pre-registers a generator for each of the five standard GraphQL scalars:

TypeGeneratorDefault range
BooleanBooleanGeneratortrue or false
IntIntGenerator0..=100
FloatFloatGenerator-1.0..=1.0
StringStringGenerator1–10 alphanumeric chars
IDIdGenerator0..=100, stringified

Each per-type struct is public, so a custom generator can construct one directly (for example, to call a configured IntGenerator inline) or register a tuned instance via ResponseBuilder::with_generator to override the default range.

§Accessing defaults from a custom generator

Every Generator::generate call receives a &mut Generators<R> — the same registry the builder is using, including any defaults from Generators::default and any overrides registered with ResponseBuilder::with_generator. A custom generator can delegate back to the registry in two ways:

  • Generators::generate_scalar — fill a leaf field by named scalar type, falling back to a default StringGenerator if nothing is registered. This is the common path for composite generators that only want to customize a few fields and let defaults handle the rest.
  • Generators::try_generate — dispatch to any registered generator (composite or leaf) by type name, returning None if none is registered. Useful when a custom generator wants to compose results from other registered types.

Structs§

BooleanGenerator
Generates a random boolean.
FloatGenerator
Generates a random float in the given inclusive range.
Generators
Registry of Generators keyed by GraphQL type name.
IdGenerator
Generates a random integer ID in the given inclusive range, serialized as a string.
IntGenerator
Generates a random integer in the given inclusive range.
StringGenerator
Generates a random alphanumeric string with length in the given inclusive range.

Traits§

Generator
A pluggable generator for the value of a named GraphQL type.