Skip to main content

@graphql-tools/mock

Interfaces#

Type aliases#

Variables#

Functions#

Type aliases#

GetArgs#

Ƭ GetArgs<KeyT\>: object

Type parameters#

NameTypeDefault
KeyTKeyTypeConstraintsstring

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


IMockFn#

Ƭ IMockFn: () => unknown

Type declaration#

▸ (): unknown

Returns: unknown

Defined in: packages/mock/src/types.ts:3


IMocks#

Ƭ IMocks: object

Type declaration#

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#

NameTypeDefault
KeyTKeyTypeConstraintsstring

Type declaration#

NameType
$refobject
$ref.keyKeyT
$ref.typeNamestring

Defined in: packages/mock/src/types.ts:185


SetArgs#

Ƭ SetArgs<KeyT\>: object

Type parameters#

NameTypeDefault
KeyTKeyTypeConstraintsstring

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


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

Functions#

addMocksToSchema#

addMocksToSchema(__namedParameters: IMockOptions): 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:93


assertIsRef#

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

Type parameters#

NameTypeDefault
KeyTKeyTypeConstraintsstring

Parameters#

NameType
maybeRefunknown
message?string

Returns: asserts maybeRef is Ref<KeyT\>

Defined in: packages/mock/src/types.ts:196


createMockStore#

createMockStore(options: { mocks?: IMocks ; schema: GraphQLSchema ; typePolicies?: { [typeName: string]: TypePolicy; } }): 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/index.ts:37


deepResolveMockList#

deepResolveMockList(mockList: MockList): unknown[]

Parameters#

NameType
mockListMockList

Returns: unknown[]

Defined in: packages/mock/src/MockList.ts:69


isMockList#

isMockList(obj: any): obj is MockList

internal

Parameters#

NameType
objany

Returns: obj is MockList

Defined in: packages/mock/src/MockList.ts:4


isRecord#

isRecord(obj: unknown): obj is object

Parameters#

NameType
objunknown

Returns: obj is object

Defined in: packages/mock/src/types.ts:205


isRef#

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

Type parameters#

NameTypeDefault
KeyTKeyTypeConstraintsstring

Parameters#

NameType
maybeRefunknown

Returns: maybeRef is Ref<KeyT\>

Defined in: packages/mock/src/types.ts:192


mockServer#

mockServer(schema: GraphQLSchema | ITypeDefinitions, mocks: IMocks, preserveResolvers?: boolean): 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
schemaGraphQLSchema | ITypeDefinitions-The schema to which to add mocks. This can also be a set of type definitions instead.
mocksIMocks-The 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