The Guild LogoThe Guild Monogram
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 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:31


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


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


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


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<string>[], args: TArgs) => Ref<string>[]Use this option to apply filtering or sorting on the nodes given the arguments the paginated field receives. ts { User: { friends: mockedRelayStylePagination< unknown, RelayPaginationParams & { sortByBirthdateDesc?: boolean} >( store, { applyOnEdges: (edges, { sortByBirthdateDesc }) => { if (!sortByBirthdateDesc) return edges return _.sortBy(edges, (e) => store.get(e, ['node', 'birthdate'])) } }), } }
cursorFn?(nodeRef: Ref<string>) => stringThe function that'll be used to compute the cursor of a node. By default, it'll use MockStore internal reference Ref's key as cursor.

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


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

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<TResolvers>(__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 }) => { 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<any, any, Record<string, any>, any>

Parameters#

NameType
__namedParametersIMockOptions<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:209


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?IMocks<IResolvers<any, any, Record<string, any>, any>>The 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:218


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


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, __namedParameters?): IFieldResolver<Ref<string>, 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
__namedParametersRelayStylePaginationMockOptions<TContext, TArgs>-

Returns#

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

Defined in#

packages/mock/src/pagination.ts:99