Documentation
API Reference
Packages
mock

@graphql-tools/mock

Classes

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 KeyTypeConstraints = string

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:37


IMockFn

Ƭ IMockFn: () => unknown

Type declaration

▸ (): unknown

Returns

unknown

Defined in

packages/mock/src/types.ts:4


IMocks

Ƭ IMocks<TResolvers>: { [TTypeName in keyof TResolvers]?: { [TFieldName in keyof TResolvers[TTypeName]]: TResolvers[TTypeName][TFieldName] extends Function ? Function : TResolvers[TTypeName][TFieldName] } } & { [typeOrScalarName: string]: IScalarMock | ITypeMock; }

Type parameters

NameType
TResolversIResolvers

Defined in

packages/mock/src/types.ts:10


IScalarMock

Ƭ IScalarMock: unknown | IMockFn

Defined in

packages/mock/src/types.ts:5


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:6


KeyTypeConstraints

Ƭ KeyTypeConstraints: string | number

Defined in

packages/mock/src/types.ts:24


Ref

Ƭ Ref<KeyT>: Object

Type parameters

NameType
KeyTextends KeyTypeConstraints = string

Type declaration

NameType
$ref{ key: KeyT ; typeName: string }
$ref.keyKeyT
$ref.typeNamestring

Defined in

packages/mock/src/types.ts:216


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) => store .get(userRef, ['friends', 'edges']) .map((e) => store.get(e, 'node')) .filter((userRef) => store.get(userRef, 'sex') === 'male') }) } }
applyOnNodes?(nodeRefs: Ref[], args: TArgs) => Ref[]-
cursorFn?(nodeRef: Ref) => string-

Defined in

packages/mock/src/pagination.ts:13


SetArgs

Ƭ SetArgs<KeyT>: Object

Type parameters

NameType
KeyTextends KeyTypeConstraints = string

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:62


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:26

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:33

Functions

addMocksToSchema

addMocksToSchema<TResolvers>(«destructured»): 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 }) => {
         const ref = store.get('Query', 'ROOT', 'viewer');
         store.set(ref, 'name', newName);
         return ref;
      }
    }
  }
 });

Query and Mutation type will use key 'ROOT'.

Type parameters

NameType
TResolversIResolvers

Parameters

NameType
«destructured»IMockOptions<TResolvers>

Returns

GraphQLSchema

Defined in

packages/mock/src/addMocksToSchema.ts:92


assertIsRef

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

Type parameters

NameType
KeyTextends KeyTypeConstraints = string

Parameters

NameType
maybeRefunknown
message?string

Returns

asserts maybeRef is Ref<KeyT>

Defined in

packages/mock/src/types.ts:229


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');
// > "Hello World"
store.set('User', 1, 'name', 'Alexandre');
store.get('User', 1, 'name');
// > "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:702


deepResolveMockList

deepResolveMockList(mockList): unknown[]

Parameters

NameType
mockListMockList

Returns

unknown[]

Defined in

packages/mock/src/MockList.ts:72


isMockList

isMockList(obj): obj is MockList

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:238


isRef

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

Type parameters

NameType
KeyTextends KeyTypeConstraints = string

Parameters

NameType
maybeRefunknown

Returns

maybeRef is Ref<KeyT>

Defined in

packages/mock/src/types.ts:223


mockServer

mockServer<TResolvers>(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 {}.

Type parameters

Name
TResolvers

Parameters

NameTypeDefault valueDescription
schemaTypeSourceundefinedThe schema to which to add mocks. This can also be a set of type definitions instead.
mocksIMocks<TResolvers>undefinedThe 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

relayStylePaginationMock<TContext, TArgs>(store, «destructured»?): IFieldResolver<Ref, TContext, TArgs, any>

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

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

Type parameters

NameType
TContextTContext
TArgsextends RelayPaginationParams = RelayPaginationParams

Parameters

NameTypeDescription
storeIMockStorethe MockStore
«destructured»RelayStylePaginationMockOptions<TContext, TArgs>-

Returns

IFieldResolver<Ref, TContext, TArgs, any>

Defined in

packages/mock/src/pagination.ts:99