The Guild LogoThe Guild Monogram

Search docs

Search icon

Products by The Guild

Products

Hive logoHive blurred logo

Hive

Schema Registry for your GraphQL Workflows

GraphQL Tools

GraphQL Tools

A set of utilities for faster GraphQL development

Get Started

@graphql-tools/mock#

Table of contents#

Classes#

  • MockList
  • MockStore

Interfaces#

Type aliases#

Variables#

Functions#

Type aliases#

AllNodesFn#

Ƭ AllNodesFn<TContext, TArgs\>: (parent: Ref, args: TArgs, context: TContext, info: GraphQLResolveInfo) => Ref[]

Type parameters#

NameType
TContextTContext
TArgsextends RelayPaginationParams

Type declaration#

▸ (parent, args, context, info): Ref[]

Parameters#
NameType
parentRef
argsTArgs
contextTContext
infoGraphQLResolveInfo
Returns#

Ref[]

Defined in#

packages/mock/src/pagination.ts:6


GetArgs#

Ƭ GetArgs<KeyT\>: Object

Type parameters#

NameType
KeyTextends KeyTypeConstraintsstring

Type declaration#

NameTypeDescription
defaultValue?unknown | { [fieldName: string]: any; }If no value found, insert the defaultValue.
fieldArgs?string | { [argName: string]: any; }Optional arguments when querying the field. Querying the field with the same arguments will return the same value. Deep equality is checked. ts store.get('User', 1, 'friend', { id: 2 }) === store.get('User', 1, 'friend', { id: 2 }) store.get('User', 1, 'friend', { id: 2 }) !== store.get('User', 1, 'friend') Args can be a record, just like args argument of field resolver or an arbitrary string.
fieldName?string-
key?KeyT-
typeNamestring-

Defined in#

packages/mock/src/types.ts:24


IMockFn#

Ƭ IMockFn: () => unknown

Type declaration#

▸ (): unknown

Returns#

unknown

Defined in#

packages/mock/src/types.ts:3


IMocks#

Ƭ IMocks: Object

Index signature#

[typeOrScalarName: string]: IScalarMock | ITypeMock

Defined in#

packages/mock/src/types.ts:7


IScalarMock#

Ƭ IScalarMock: unknown | IMockFn

Defined in#

packages/mock/src/types.ts:4


ITypeMock#

Ƭ ITypeMock: () => { [fieldName: string]: unknown | IMockFn; } | { [fieldName: string]: IMockFn; }

Type declaration#

▸ (): { [fieldName: string]: unknown | IMockFn; } | { [fieldName: string]: IMockFn; }

Returns#

{ [fieldName: string]: unknown | IMockFn; } | { [fieldName: string]: IMockFn; }

Defined in#

packages/mock/src/types.ts:5


KeyTypeConstraints#

Ƭ KeyTypeConstraints: string | number

Defined in#

packages/mock/src/types.ts:11


Ref#

Ƭ Ref<KeyT\>: Object

Type parameters#

NameType
KeyTextends KeyTypeConstraintsstring

Type declaration#

NameType
$refObject
$ref.keyKeyT
$ref.typeNamestring

Defined in#

packages/mock/src/types.ts:191


RelayPageInfo#

Ƭ RelayPageInfo: Object

Type declaration#

NameType
endCursorstring
hasNextPageboolean
hasPreviousPageboolean
startCursorstring

Defined in#

packages/mock/src/pagination.ts:77


RelayPaginationParams#

Ƭ RelayPaginationParams: Object

Type declaration#

NameType
after?string
before?string
first?number
last?number

Defined in#

packages/mock/src/pagination.ts:70


RelayStylePaginationMockOptions#

Ƭ RelayStylePaginationMockOptions<TContext, TArgs\>: Object

Type parameters#

NameType
TContextTContext
TArgsextends RelayPaginationParams

Type declaration#

NameTypeDescription
allNodesFn?AllNodesFn<TContext, TArgs\>A function that'll be used to get all the nodes used for pagination. By default, it will use the nodes of the field this pagination is attached to. This option is handy when several paginable fields should share the same base nodes: ts { User: { friends: mockedRelayStylePagination(store), maleFriends: mockedRelayStylePagination(store, { allNodesFn: (userRef) =&gt; store .get(userRef, ['friends', 'edges']) .map((e) =&gt; store.get(e, 'node')) .filter((userRef) =&gt; store.get(userRef, 'sex') === 'male') }) } }
applyOnNodes?(nodeRefs: Ref<string\>[], args: TArgs) => Ref<string\>[]-
cursorFn?(nodeRef: Ref<string\>) => string-

Defined in#

packages/mock/src/pagination.ts:13


SetArgs#

Ƭ SetArgs<KeyT\>: Object

Type parameters#

NameType
KeyTextends KeyTypeConstraintsstring

Type declaration#

NameTypeDescription
fieldArgs?string | { [argName: string]: any; }Optional arguments when querying the field. see GetArgs#fieldArgs
fieldName?string-
keyKeyT-
noOverride?booleanIf the value for this field is already set, it won't be overridden. Propagates down do nested set.
typeNamestring-
value?unknown | { [fieldName: string]: any; }-

Defined in#

packages/mock/src/types.ts:49


TypePolicy#

Ƭ TypePolicy: Object

Type declaration#

NameTypeDescription
keyFieldName?string | falseThe name of the field that should be used as store key. If false, no field will be used and id or _id will be used, otherwise we'll generate a random string as key.

Defined in#

packages/mock/src/types.ts:13

Variables#

defaultMocks#

Const defaultMocks: Object

Type declaration#

NameType
Boolean() => boolean
Float() => number
ID() => string
Int() => number
String() => string

Defined in#

packages/mock/src/MockStore.ts:34

Functions#

addMocksToSchema#

addMocksToSchema(__namedParameters): GraphQLSchema

Given a schema and a MockStore, returns an executable schema that will use the provided MockStore to execute queries.

const schema = buildSchema(` type User { id: ID! name: String! } type Query { me: User! } `) const store = createMockStore({ schema }); const mockedSchema = addMocksToSchema({ schema, store });

If a resolvers parameter is passed, the query execution will use the provided resolvers if, one exists, instead of the default mock resolver.

const schema = buildSchema(` type User { id: ID! name: String! } type Query { me: User! } type Mutation { setMyName(newName: String!): User! } `) const store = createMockStore({ schema }); const mockedSchema = addMocksToSchema({ schema, store, resolvers: { Mutation: { setMyName: (_, { newName }) =&gt; { const ref = store.get('Query', 'ROOT', 'viewer'); store.set(ref, 'name', newName); return ref; } } } });

Query and Mutation type will use key 'ROOT'.

Parameters#

NameType
__namedParametersIMockOptions

Returns#

GraphQLSchema

Defined in#

packages/mock/src/addMocksToSchema.ts:92


assertIsRef#

assertIsRef<KeyT\>(maybeRef, message?): asserts maybeRef is Ref<KeyT\>

Type parameters#

NameType
KeyTextends KeyTypeConstraintsstring

Parameters#

NameType
maybeRefunknown
message?string

Returns#

asserts maybeRef is Ref<KeyT\>

Defined in#

packages/mock/src/types.ts:202


createMockStore#

createMockStore(options): IMockStore

Will create MockStore for the given schema.

A MockStore will generate mock values for the given schem when queried.

It will stores generated mocks, so that, provided with same arguments the returned values will be the same.

Its API also allows to modify the stored values.

Basic example:

store.get('User', 1, 'name'); // &gt; "Hello World" store.set('User', 1, 'name', 'Alexandre'); store.get('User', 1, 'name'); // &gt; "Alexandre"

The storage key will correspond to the "key field" of the type. Field with name id or _id will be by default considered as the key field for the type. However, use typePolicies to precise the field to use as key.

Parameters#

NameTypeDescription
optionsObject-
options.mocks?IMocksThe mocks functions to use.
options.schemaGraphQLSchemaThe schema to based mocks on.
options.typePolicies?Object-

Returns#

IMockStore

Defined in#

packages/mock/src/MockStore.ts:635


deepResolveMockList#

deepResolveMockList(mockList): unknown[]

Parameters#

NameType
mockListMockList

Returns#

unknown[]

Defined in#

packages/mock/src/MockList.ts:69


isMockList#

isMockList(obj): obj is MockList

internal

Parameters#

NameType
objany

Returns#

obj is MockList

Defined in#

packages/mock/src/MockList.ts:4


isRecord#

isRecord(obj): obj is object

Parameters#

NameType
objunknown

Returns#

obj is object

Defined in#

packages/mock/src/types.ts:211


isRef#

isRef<KeyT\>(maybeRef): maybeRef is Ref<KeyT\>

Type parameters#

NameType
KeyTextends KeyTypeConstraintsstring

Parameters#

NameType
maybeRefunknown

Returns#

maybeRef is Ref<KeyT\>

Defined in#

packages/mock/src/types.ts:198


mockServer#

mockServer(schema, mocks, preserveResolvers?): IMockServer

A convenience wrapper on top of addMocksToSchema. It adds your mock resolvers to your schema and returns a client that will correctly execute your query with variables. Note: when executing queries from the returned server, context and root will both equal {}.

Parameters#

NameTypeDefault valueDescription
schemaTypeSourceundefinedThe schema to which to add mocks. This can also be a set of type definitions instead.
mocksIMocksundefinedThe mocks to add to the schema.
preserveResolversbooleanfalseSet to true to prevent existing resolvers from being overwritten to provide mock data. This can be used to mock some parts of the server and not others.

Returns#

IMockServer

Defined in#

packages/mock/src/mockServer.ts:19


relayStylePaginationMock#

Const relayStylePaginationMock<TContext, TArgs\>(store, __namedParameters?): IFieldResolver<Ref<string\>, TContext, TArgs, any\>

Produces a resolver that'll mock a Relay-style cursor pagination.

const schemaWithMocks = addMocksToSchema({ schema, resolvers: (store) =&gt; ({ User: { friends: relayStylePaginationMock(store), } }), })

Type parameters#

NameType
TContextTContext
TArgsextends RelayPaginationParamsRelayPaginationParams

Parameters#

NameTypeDescription
storeIMockStorethe MockStore
__namedParametersRelayStylePaginationMockOptions<TContext, TArgs\>-

Returns#

IFieldResolver<Ref<string\>, TContext, TArgs, any\>

Defined in#

packages/mock/src/pagination.ts:99