Skip to main content

Function: entityGraph()

entityGraph<TEntityFields, TEntityRelations>(arbitraries, relations, constraints?): Arbitrary<EntityGraphValue<TEntityFields, TEntityRelations>>

Defined in: packages/fast-check/src/arbitrary/entityGraph.ts:112

Generates interconnected entities with relationships based on a schema definition.

This arbitrary creates structured data where entities can reference each other through defined relationships. The generated values automatically include links between entities, making it ideal for testing graph structures, relational data, or interconnected object models.

The output is an object where each key corresponds to an entity type and the value is an array of entities of that type. Entities contain both their data fields and relationship links.

Type Parameters

Type Parameter
TEntityFields
TEntityRelations extends EntityGraphRelations<TEntityFields>

Parameters

ParameterTypeDescription
arbitrariesEntityGraphArbitraries<TEntityFields>Defines the data fields for each entity type (non-relational properties)
relationsTEntityRelationsDefines how entities reference each other (relational properties)
constraintsEntityGraphContraints<TEntityFields>Optional configuration to customize generation behavior

Returns

Arbitrary<EntityGraphValue<TEntityFields, TEntityRelations>>

Examples

// Generate a simple directed graph where nodes link to other nodes
fc.entityGraph(
{ node: { id: fc.stringMatching(/^[A-Z][a-z]*$/) } },
{ node: { linkTo: { arity: 'many', type: 'node' } } },
)
// Produces: { node: [{ id: "Abc", linkTo: [<node#1>, <node#0>] }, ...] }
// Generate employees with managers and teams
fc.entityGraph(
{
employee: { name: fc.string() },
team: { name: fc.string() }
},
{
employee: {
manager: { arity: '0-1', type: 'employee' }, // Optional manager
team: { arity: '1', type: 'team' } // Required team
},
team: {}
}
)

Remarks

Since 4.5.0