@graphql-tools/mock
Table of contents
Classes
Interfaces
Type Aliases
- AllNodesFn
- GetArgs
- IMockFn
- IMocks
- IScalarMock
- ITypeMock
- KeyTypeConstraints
- Ref
- RelayPageInfo
- RelayPaginationParams
- RelayStylePaginationMockOptions
- SetArgs
- TypePolicy
Variables
Functions
- addMocksToSchema
- assertIsRef
- createMockStore
- deepResolveMockList
- isMockList
- isRecord
- isRef
- mockServer
- relayStylePaginationMock
Type Aliases
AllNodesFn
Ƭ AllNodesFn<TContext
, TArgs
>: (parent
: Ref
, args
: TArgs
, context
: TContext
, info
: GraphQLResolveInfo
) => Ref
[]
Type parameters
Name | Type |
---|---|
TContext | TContext |
TArgs | extends RelayPaginationParams |
Type declaration
▸ (parent
, args
, context
, info
): Ref
[]
Parameters
Name | Type |
---|---|
parent | Ref |
args | TArgs |
context | TContext |
info | GraphQLResolveInfo |
Returns
Ref
[]
Defined in
packages/mock/src/pagination.ts:6 (opens in a new tab)
GetArgs
Ƭ GetArgs<KeyT
>: Object
Type parameters
Name | Type |
---|---|
KeyT | extends KeyTypeConstraints = string |
Type declaration
Name | Type | Description |
---|---|---|
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 | - |
typeName | string | - |
Defined in
packages/mock/src/types.ts:31 (opens in a new tab)
IMockFn
Ƭ IMockFn: () => unknown
Type declaration
▸ (): unknown
Returns
unknown
Defined in
packages/mock/src/types.ts:4 (opens in a new tab)
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
Name | Type |
---|---|
TResolvers | IResolvers |
Defined in
packages/mock/src/types.ts:8 (opens in a new tab)
IScalarMock
Ƭ IScalarMock: unknown
| IMockFn
Defined in
packages/mock/src/types.ts:5 (opens in a new tab)
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 (opens in a new tab)
KeyTypeConstraints
Ƭ KeyTypeConstraints: string
| number
Defined in
packages/mock/src/types.ts:18 (opens in a new tab)
Ref
Ƭ Ref<KeyT
>: Object
Type parameters
Name | Type |
---|---|
KeyT | extends KeyTypeConstraints = string |
Type declaration
Name | Type |
---|---|
$ref | { key : KeyT ; typeName : string } |
$ref.key | KeyT |
$ref.typeName | string |
Defined in
packages/mock/src/types.ts:198 (opens in a new tab)
RelayPageInfo
Ƭ RelayPageInfo: Object
Type declaration
Name | Type |
---|---|
endCursor | string |
hasNextPage | boolean |
hasPreviousPage | boolean |
startCursor | string |
Defined in
packages/mock/src/pagination.ts:77 (opens in a new tab)
RelayPaginationParams
Ƭ RelayPaginationParams: Object
Type declaration
Name | Type |
---|---|
after? | string |
before? | string |
first? | number |
last? | number |
Defined in
packages/mock/src/pagination.ts:70 (opens in a new tab)
RelayStylePaginationMockOptions
Ƭ RelayStylePaginationMockOptions<TContext
, TArgs
>: Object
Type parameters
Name | Type |
---|---|
TContext | TContext |
TArgs | extends RelayPaginationParams |
Type declaration
Name | Type | Description |
---|---|---|
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 [] | 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 | The 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 (opens in a new tab)
SetArgs
Ƭ SetArgs<KeyT
>: Object
Type parameters
Name | Type |
---|---|
KeyT | extends KeyTypeConstraints = string |
Type declaration
Name | Type | Description |
---|---|---|
fieldArgs? | string | { [argName: string] : any ; } | Optional arguments when querying the field. See GetArgs#fieldArgs |
fieldName? | string | - |
key | KeyT | - |
noOverride? | boolean | If the value for this field is already set, it won't be overridden. Propagates down do nested set . |
typeName | string | - |
value? | unknown | { [fieldName: string] : any ; } | - |
Defined in
packages/mock/src/types.ts:56 (opens in a new tab)
TypePolicy
Ƭ TypePolicy: Object
Type declaration
Name | Type | Description |
---|---|---|
keyFieldName? | string | false | The 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 (opens in a new tab)
Variables
defaultMocks
• Const
defaultMocks: Object
Type declaration
Name | Type |
---|---|
Boolean | () => boolean |
Float | () => number |
ID | () => string |
Int | () => number |
String | () => string |
Defined in
packages/mock/src/MockStore.ts:34 (opens in a new tab)
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
Name | Type |
---|---|
TResolvers | IResolvers |
Parameters
Name | Type |
---|---|
«destructured» | IMockOptions <TResolvers > |
Returns
GraphQLSchema
Defined in
packages/mock/src/addMocksToSchema.ts:92 (opens in a new tab)
assertIsRef
▸ assertIsRef<KeyT
>(maybeRef
, message?
): asserts maybeRef is Ref<KeyT>
Type parameters
Name | Type |
---|---|
KeyT | extends KeyTypeConstraints = string |
Parameters
Name | Type |
---|---|
maybeRef | unknown |
message? | string |
Returns
asserts maybeRef is Ref<KeyT>
Defined in
packages/mock/src/types.ts:209 (opens in a new tab)
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
Name | Type | Description |
---|---|---|
options | Object | - |
options.mocks? | IMocks | The mocks functions to use. |
options.schema | GraphQLSchema | The schema to based mocks on. |
options.typePolicies? | Object | - |
Returns
Defined in
packages/mock/src/MockStore.ts:635 (opens in a new tab)
deepResolveMockList
▸ deepResolveMockList(mockList
): unknown
[]
Parameters
Name | Type |
---|---|
mockList | MockList |
Returns
unknown
[]
Defined in
packages/mock/src/MockList.ts:69 (opens in a new tab)
isMockList
▸ isMockList(obj
): obj is MockList
Parameters
Name | Type |
---|---|
obj | any |
Returns
obj is MockList
Defined in
packages/mock/src/MockList.ts:4 (opens in a new tab)
isRecord
▸ isRecord(obj
): obj is Object
Parameters
Name | Type |
---|---|
obj | unknown |
Returns
obj is Object
Defined in
packages/mock/src/types.ts:218 (opens in a new tab)
isRef
▸ isRef<KeyT
>(maybeRef
): maybeRef is Ref<KeyT>
Type parameters
Name | Type |
---|---|
KeyT | extends KeyTypeConstraints = string |
Parameters
Name | Type |
---|---|
maybeRef | unknown |
Returns
maybeRef is Ref<KeyT>
Defined in
packages/mock/src/types.ts:205 (opens in a new tab)
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
Name | Type | Default value | Description |
---|---|---|---|
schema | TypeSource | undefined | The schema to which to add mocks. This can also be a set of type definitions instead. |
mocks | IMocks <TResolvers > | undefined | The mocks to add to the schema. |
preserveResolvers | boolean | false | Set 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
Defined in
packages/mock/src/mockServer.ts:19 (opens in a new tab)
relayStylePaginationMock
▸ relayStylePaginationMock<TContext
, TArgs
>(store
, «destructured»?
): IFieldResolver
<Ref
, TContext
, TArgs
, any
>
Produces a resolver that'll mock a Relay-style cursor pagination (opens in a new tab).
const schemaWithMocks = addMocksToSchema({
schema,
resolvers: (store) => ({
User: {
friends: relayStylePaginationMock(store),
}
}),
})
Type parameters
Name | Type |
---|---|
TContext | TContext |
TArgs | extends RelayPaginationParams = RelayPaginationParams |
Parameters
Name | Type | Description |
---|---|---|
store | IMockStore | the MockStore |
«destructured» | RelayStylePaginationMockOptions <TContext , TArgs > | - |
Returns
IFieldResolver
<Ref
, TContext
, TArgs
, any
>