Catch the highlights of GraphQLConf 2023! Click for recordings. Or check out our recap blog post.
Docs
API Reference
Classes
MockStore

graphql-tools-monorepo / mock/src / MockStore

Class: MockStore

mock/src.MockStore

Implements

Table of contents

Constructors

Properties

Methods

Constructors

constructor

new MockStore(«destructured»): MockStore

Parameters

NameType
«destructured»Object
› mocks?IMocks
› schemaGraphQLSchema
› typePolicies?Object

Returns

MockStore

Defined in

packages/mock/src/MockStore.ts:56

Properties

schema

schema: GraphQLSchema

Implementation of

IMockStore.schema

Defined in

packages/mock/src/MockStore.ts:48

Methods

filter

filter(key, predicate): Entity[]

Parameters

NameType
keystring
predicate(val: Entity) => boolean

Returns

Entity[]

Defined in

packages/mock/src/MockStore.ts:193


find

find(key, predicate): undefined | Entity

Parameters

NameType
keystring
predicate(val: Entity) => boolean

Returns

undefined | Entity

Defined in

packages/mock/src/MockStore.ts:198


get

get<KeyT, ReturnKeyT>(_typeName, _key?, _fieldName?, _fieldArgs?): unknown

Get a field value from the store for the given type, key and field name — and optionally field arguments. If the field name is not given, a reference to the type will be returned.

If the the value for this field is not set, a value will be generated according to field return type and mock functions.

If the field’s output type is a ObjectType (or list of ObjectType), it will return a Ref (or array of Ref), ie a reference to an entity in the store.

Example:

store.get('Query', 'ROOT', 'viewer');
> { $ref: { key: 'abc-737dh-djdjd', typeName: 'User' } }
store.get('User', 'abc-737dh-djdjd', 'name')
> "Hello World"

Type parameters

NameType
KeyTextends KeyTypeConstraints = string
ReturnKeyTextends KeyTypeConstraints = string

Parameters

NameType
_typeNamestring | Ref<KeyT> | GetArgs<KeyT>
_key?string | string[] | KeyT | { [fieldName: string]: any; }
_fieldName?string | string[] | { [fieldName: string]: any; } | { [argName: string]: any; }
_fieldArgs?string | { [argName: string]: any; }

Returns

unknown

Implementation of

IMockStore.get

Defined in

packages/mock/src/MockStore.ts:76


has

has<KeyT>(typeName, key): boolean

Checks if a mock is present in the store for the given typeName and key.

Type parameters

NameType
KeyTextends KeyTypeConstraints = string

Parameters

NameType
typeNamestring
keyKeyT

Returns

boolean

Implementation of

IMockStore.has

Defined in

packages/mock/src/MockStore.ts:72


reset

reset(): void

Resets the mock store

Returns

void

Implementation of

IMockStore.reset

Defined in

packages/mock/src/MockStore.ts:189


set

set<KeyT>(_typeName, _key?, _fieldName?, _value?): void

Set a field value in the store for the given type, key and field name — and optionally field arguments.

If the the field return type is an ObjectType or a list of ObjectType, you can set references to other entity as value:

// set the viewer name
store.set('User', 1, 'name', 'Alexandre);
store.set('Query', 'ROOT', 'viewer', store.get('User', 1));
 
// set the friends of viewer
store.set('User', 2, 'name', 'Emily');
store.set('User', 3, 'name', 'Caroline');
store.set('User', 1, 'friends', [store.get('User', 2), store.get('User', 3)]);

But it also supports nested set:

store.set('Query', 'ROOT', 'viewer', {
 name: 'Alexandre',
 friends: [
   { name: 'Emily' }
   { name: 'Caroline }
 ]
});

Type parameters

NameType
KeyTextends KeyTypeConstraints

Parameters

NameType
_typeNamestring | Ref<KeyT> | SetArgs<KeyT>
_key?string | KeyT | { [fieldName: string]: any; }
_fieldName?unknown
_value?unknown

Returns

void

Implementation of

IMockStore.set

Defined in

packages/mock/src/MockStore.ts:143