@vantreeseba/graphql-mocks-codegen
v5.2.0
Published
GraphQL Code Generator plugin that emits a SchemaTypeMap for typing @vantreeseba/graphql-mocks pools.
Maintainers
Readme
@vantreeseba/graphql-mocks-codegen
A GraphQL Code Generator plugin that emits a SchemaTypeMap,
a { TypeName: GeneratedType } map of every object type in your schema. Hand it to
@vantreeseba/graphql-mocks's
buildMocks<TTypes> and the mock pools come back typed without a cast, with no map to keep in step by hand.
Install
npm install --save-dev @vantreeseba/graphql-mocks-codegen @graphql-codegen/cli @graphql-codegen/typescript graphql
npm install --save-dev @vantreeseba/graphql-mocks @faker-js/fakerQuick start
Emit the map beside the file your typescript plugin writes:
// codegen.ts
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: './schema.graphql',
generates: {
'./src/__generated__/graphql.ts': { plugins: ['typescript'] },
'./src/__generated__/schema-type-map.ts': {
plugins: ['@vantreeseba/graphql-mocks-codegen'],
config: { typesImportPath: './graphql.js' },
},
},
};
export default config;Run npx graphql-codegen, then:
// src/mocks.ts
import { readFileSync } from 'node:fs';
import { buildMocks } from '@vantreeseba/graphql-mocks';
import type { SchemaTypeMap } from './__generated__/schema-type-map.js';
export const mocks = buildMocks<SchemaTypeMap>(readFileSync('./schema.graphql', 'utf8'));
mocks.User; // User[], typedDocumentation
- Install and configure, with every option and the generated output
- Common mistakes
- How to use graphql-mocks, which shows where the map fits in
- Index for agents, as
llms.txt - Changelog
