UNPKG

@graphql-tools/mock

Version:

A set of utils for faster development of GraphQL tools

115 lines (114 loc) 4.24 kB
import { GraphQLSchema } from 'graphql'; import { GetArgs, IMocks, IMockStore, KeyTypeConstraints, MockGenerationBehavior, Ref, SetArgs, TypePolicy } from './types.js'; type Entity = { [key: string]: unknown; }; export declare class MockStore implements IMockStore { schema: GraphQLSchema; private mocks; private mockGenerationBehavior; private typePolicies; private store; constructor({ schema, mocks, mockGenerationBehavior, typePolicies, }: { schema: GraphQLSchema; mocks?: IMocks; mockGenerationBehavior?: MockGenerationBehavior; typePolicies?: { [typeName: string]: TypePolicy; }; }); has<KeyT extends KeyTypeConstraints = string>(typeName: string, key: KeyT): boolean; get<KeyT extends KeyTypeConstraints = string, ReturnKeyT extends KeyTypeConstraints = string>(_typeName: string | Ref<KeyT> | GetArgs<KeyT>, _key?: KeyT | { [fieldName: string]: any; } | string | string[], _fieldName?: string | string[] | { [fieldName: string]: any; } | string | { [argName: string]: any; }, _fieldArgs?: string | { [argName: string]: any; }): unknown | Ref<ReturnKeyT>; set<KeyT extends KeyTypeConstraints>(_typeName: string | Ref<KeyT> | SetArgs<KeyT>, _key?: KeyT | string | { [fieldName: string]: any; }, _fieldName?: string | { [fieldName: string]: any; } | unknown, _value?: unknown): void; reset(): void; filter(key: string, predicate: (val: Entity) => boolean): Entity[]; find(key: string, predicate: (val: Entity) => boolean): Entity | undefined; private getImpl; private setImpl; private normalizeValueToStore; private insert; private generateFieldValue; private generateFieldValueFromMocks; private generateKeyForType; private generateValueFromType; private getFieldType; private getType; private isKeyField; private getKeyFieldName; } /** * Will create `MockStore` for the given `schema`. * * A `MockStore` will generate mock values for the given schema when queried. * By default it'll generate random values, if this causes flakiness and you * need a more deterministic behavior, use `mockGenerationBehavior` option. * * It will store 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: * ```ts * 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. */ export declare function createMockStore(options: { /** * The `schema` to based mocks on. */ schema: GraphQLSchema; /** * The mocks functions to use. */ mocks?: IMocks; /** * Configures the default behavior for Scalar, Enum, Union, and Array types. * * When set to `random`, then every time a value is generated for a field with * one of these types * - For Unions and Enums one of the allowed values will be picked randomly * - For Arrays an array of random length will be generated * - For Int and Float scalars a random number will be generated * - For Boolean scalars either `true` or `false` will be returned randomly * * When set to `deterministic`, then * - For Unions and Enums the first allowed value is picked * - For Arrays an array of two elements will be generated * - For Int and Float scalars values 1 and 1.5 will be used respectively * - For Boolean scalars we'll always return `true` * - For String scalars we'll always return "Hello World" * * Regardless of the chosen behavior, for ID scalars a random UUID string * will always be generated. * * @default "random" */ mockGenerationBehavior?: MockGenerationBehavior; typePolicies?: { [typeName: string]: TypePolicy; }; }): IMockStore; export {};