{"id":25456019,"url":"https://github.com/ardeois/graphql-codegen-typescript-mock-data","last_synced_at":"2025-11-02T09:30:36.031Z","repository":{"id":35525515,"uuid":"218142015","full_name":"ardeois/graphql-codegen-typescript-mock-data","owner":"ardeois","description":"[GraphQL Codegen Plugin](https://github.com/dotansimha/graphql-code-generator) for building mock data based on the schema.","archived":false,"fork":false,"pushed_at":"2024-12-29T09:27:01.000Z","size":1302,"stargazers_count":134,"open_issues_count":10,"forks_count":49,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-03T14:15:36.952Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ardeois.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-28T20:45:43.000Z","updated_at":"2024-09-20T15:27:53.000Z","dependencies_parsed_at":"2024-02-28T21:45:25.195Z","dependency_job_id":"c2f44193-15f2-4a43-9c69-8ca6313ec75c","html_url":"https://github.com/ardeois/graphql-codegen-typescript-mock-data","commit_stats":{"total_commits":256,"total_committers":27,"mean_commits":9.481481481481481,"dds":0.3046875,"last_synced_commit":"788bd72ef7fb41512879fe06497201e3e48a6d42"},"previous_names":[],"tags_count":93,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ardeois%2Fgraphql-codegen-typescript-mock-data","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ardeois%2Fgraphql-codegen-typescript-mock-data/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ardeois%2Fgraphql-codegen-typescript-mock-data/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ardeois%2Fgraphql-codegen-typescript-mock-data/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ardeois","download_url":"https://codeload.github.com/ardeois/graphql-codegen-typescript-mock-data/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239389483,"owners_count":19630310,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-18T01:01:31.291Z","updated_at":"2025-11-02T09:30:35.994Z","avatar_url":"https://github.com/ardeois.png","language":"TypeScript","funding_links":[],"categories":["Projects using `@faker-js/faker`"],"sub_categories":[],"readme":"# graphql-codegen-typescript-mock-data\n\n## Description\n\n[GraphQL Codegen Plugin](https://github.com/dotansimha/graphql-code-generator) for building mock data based on the schema.\n\n## Installation\n\n`yarn add -D graphql-codegen-typescript-mock-data`\n\n## Configuration\n\n### typesFile (`string`, defaultValue: `null`)\n\nDefines the file path containing all GraphQL types. This file can also be generated through graphql-codegen\n\n### useTypeImports(boolean, defaultValue: false)\n\nWill use import type {} rather than import {} when importing only types. This gives compatibility with TypeScript's \"importsNotUsedAsValues\": \"error\" option\n\n### addTypename (`boolean`, defaultValue: `false`)\n\nAdds `__typename` property to mock data\n\n### enumsAsTypes (`boolean`, defaultValue: `false`)\n\nChanges enums to TypeScript string union types\n\n### terminateCircularRelationships (`boolean | 'immediate'`, defaultValue: `false`)\n\nWhen enabled, prevents circular relationships from triggering infinite recursion. After the first resolution of a\nspecific type in a particular call stack, subsequent resolutions will return an empty object cast to the correct type.\n\nWhen enabled with `immediate`, it will only resolve the relationship once, independently of the call stack. Use this option if you're experiencing `out of memory` errors while generating mocks.\n\n### prefix (`string`, defaultValue: `a` for consonants \u0026 `an` for vowels)\n\nThe prefix to add to the mock function name. Cannot be empty since it will clash with the associated\ntypescript definition from `@graphql-codegen/typescript`\n\n### listElementCount (`number`, defaultValue: `1`)\n\nHow many elements should be generated for lists. For example, with `listElementCount: 3` a schema field `names: [String!]!` would generate `3` names in each mock.\n\n### enumValues (`string`, defaultValue: `change-case-all#pascalCase`)\n\nChanges the case of the enums. The format of the converter must be a valid `module#method`. You can also use `keep` to\nkeep all GraphQL names as-is. Available case functions in `change-case-all` are `camelCase`, `capitalCase`, `constantCase`,\n`dotCase`, `headerCase`, `noCase`, `paramCase`, `pascalCase`, `pathCase`, `sentenceCase`, `snakeCase`, `lowerCase`,\n`localeLowerCase`, `lowerCaseFirst`, `spongeCase`, `titleCase`, `upperCase`, `localeUpperCase` and `upperCaseFirst`\n[See more](https://github.com/btxtiger/change-case-all)\n\n### typeNames (`string`, defaultValue: `change-case-all#pascalCase`)\n\nChanges the case of types. The format of the converter must be a valid `module#method`. You can also use `keep` to\nkeep all GraphQL names as-is. Available case functions in `change-case-all` are `camelCase`, `capitalCase`, `constantCase`,\n`dotCase`, `headerCase`, `noCase`, `paramCase`, `pascalCase`, `pathCase`, `sentenceCase`, `snakeCase`, `lowerCase`,\n`localeLowerCase`, `lowerCaseFirst`, `spongeCase`, `titleCase`, `upperCase`, `localeUpperCase` and `upperCaseFirst`\n[See more](https://github.com/btxtiger/change-case-all)\n\n### scalars (`{ [Scalar: string]: GeneratorOptions }`, defaultValue: `undefined`)\n\nAllows you to define mappings for your custom scalars. Allows you to map any GraphQL Scalar to a\n[casual](https://github.com/boo1ean/casual#embedded-generators) embedded generator (string or\nfunction key) with optional arguments, or a or [faker](https://fakerjs.dev/api/) generator with optional arguments\n\nFor detailed configuration options, see [GeneratorOptions](#generatoroptions-type) documentation.\n\nExamples using **casual**\n\n```yaml\nplugins:\n  - typescript-mock-data:\n      scalars:\n        Date: date # gets translated to casual.date()\n```\n\n**With arguments**\n\n```yaml\nplugins:\n  - typescript-mock-data:\n      scalars:\n        Date: # gets translated to casual.date('YYYY-MM-DD')\n          generator: date\n          arguments: 'YYYY-MM-DD'\n```\n\nExamples using **faker**\n\n```yaml\nplugins:\n  - typescript-mock-data:\n      scalars:\n        Date: date.past # gets translated to faker.date.past()\n```\n\n**With arguments**\n\n```yaml\nplugins:\n  - typescript-mock-data:\n      scalars:\n        Date: # gets translated to faker.date.past(10)\n          generator: date.past\n          arguments: 10\n```\n\n**Custom value generator**\n\n```yaml\nplugins:\n  - add: \"import { arrayBufferGenerator } from '../generators';\"\n  - typescript-mock-data:\n      scalars:\n        ArrayBuffer: arrayBufferGenerator()\n```\n\n### typesPrefix (`string`, defaultValue: '')\n\nUseful if you have globally exported types under a certain namespace.\ne.g If the types file is something like this\n\n```\ndeclare namespace Api {\n type User {\n  ...\n }\n}\n```\n\nSetting the `typesPrefix` to `Api.` will create the following mock data\n\n```\nexport const aUser = (overrides?: Partial\u003cApi.User\u003e): Api.User =\u003e {\n```\n\n### enumsPrefix (`string`, defaultValue: '')\n\nSimilar to `typesPrefix`, but for enum types\n\n```\ndeclare namespace Api {\n enum Status {\n  ...\n }\n}\n```\n\nSetting the `enumsPrefix` to `Api.` will create the following mock data\n\n```\nexport const aUser = (overrides?: Partial\u003cUser\u003e): User =\u003e {\n   status: overrides \u0026\u0026 overrides.hasOwnProperty('status') ? overrides.status! : Api.Status.Online,\n}\n```\n\n### typeNamesMapping (`{ [typeName: string]: string }`, defaultValue: `{}`)\n\nAllows you to define mappings to rename the types. This is useful when you want to override the generated type name. For example, if you have a type called `User` and you want to rename it to `RenamedUser` you can do the following:\n\n```\nplugins:\n  - typescript-mock-data:\n      typesFile: '../generated-types.ts'\n      typeNamesMapping:\n        User: RenamedUser\n```\n\nThis will generate the following mock function:\n\n```\nexport const aUser = (overrides?: Partial\u003cRenamedUser\u003e): RenamedUser =\u003e {\n```\n\n**Note:** It is not possible to rename your enums using this option.\n\n### transformUnderscore (`boolean`, defaultValue: `true`)\n\nWhen disabled, underscores will be retained for type names when the case is changed. It has no effect if `typeNames` is set to `keep`.\n\n### dynamicValues (`boolean`, defaultValue: `false`)\n\nWhen enabled, values will be generated dynamically when the mock function is called rather than statically when the mock function is generated. The values are generated consistently from a [casual seed](https://github.com/boo1ean/casual#seeding) that can be manually configured using the generated `seedMocks(seed: number)` function, as shown in [this test](https://github.com/JimmyPaolini/graphql-codegen-typescript-mock-data/blob/dynamic-mode/tests/dynamicValues/spec.ts#L13).\n\n### useImplementingTypes (`boolean`, defaultValue: `false`)\n\nWhen enabled, it will support the useImplementingTypes GraphQL codegen configuration.\n\n- When a GraphQL interface is used for a field, this flag will use the implementing types, instead of the interface itself.\n\n### defaultNullableToNull (`boolean`, defaultValue: `false`)\n\nWhen enabled, it will set all nullable fields to null per default instead of generating a value.\n\n### fieldGeneration (`{ [typeName: string]: { [fieldName: string]: GeneratorOptions } }`, defaultValue: `undefined`)\n\nThis setting allows you to add specific generation to a field for a given type. For example if you have a type called `User` and a field called `birthDate` you can override any generated value there as follows:\n\n```yaml\nplugins:\n  - typescript-mock-data:\n      scalars:\n        Date: date.future\n      fieldGeneration:\n        User:\n          birthDate: date.past\n```\n\nNote that even if `birthDate` is a scalar of `Date` type, its value will still be overridden.\n\nIf you want to use a specific generator for **all** fields of a given name, you can declare it under a property called `_all`:\n\n```yaml\nplugins:\n  - typescript-mock-data:\n      scalars:\n        Date: date.future\n      fieldGeneration:\n        _all:\n          email: internet.email\n        AdminUser:\n          email: 'admin@email.com'\n```\n\nIn the above example all resolvers with the name `email` will use the `internet.email` generator. However since we specified a specific email for `AdminUser` that will take precedence over the `_all` generated value.\n\nFor detailed configuration options, see [GeneratorOptions](#generatoroptions-type) documentation.\n\n### generateLibrary (`'casual' | 'faker'`, defaultValue: `'faker'`)\n\nSelect a library to generate mock values. The default is [faker](https://github.com/faker-js/faker), Other options include [casual](https://github.com/boo1ean/casual)\ncasual is not maintained and will be remove in future major versions.\nfaker is useful when you want to use a mock function with the dynamicValues option enabled in the browser.\n\n### `GeneratorOptions` type\n\nThis type is used in `scalars` and `fieldGeneration` options.\n\nExamples using **faker**\n\n**With arguments**\n\n```yaml\nplugins:\n  - typescript-mock-data:\n      scalars:\n        Date: # gets translated to faker.date.past(10)\n          generator: date.past\n          arguments: 10\n```\n\n**With multiple arguments**\n\n```yaml\nplugins:\n  - typescript-mock-data:\n      scalars:\n        Description: # gets translated to faker.lorem.paragraphs(3, '\\n')\n          generator: lorem.paragraphs\n          arguments:\n            - 3\n            - '\\n'\n```\n\n**Shorthand if you don't have arguments**\n\n```yaml\nplugins:\n  - typescript-mock-data:\n      scalars:\n        Date: date.past # gets translated to faker.date.past()\n```\n\n**With extra function call**\n\n```yaml\nfieldName: # gets translated to faker.date.past().toLocaleDateString()\n  generator: date.past\n  extra:\n    function: toLocaleDateString\n```\n\n**With extra function call arguments**\n\n```yaml\nfieldName: # gets translated to faker.date.past().toLocaleDateString('en_GB)\n  generator: date.past\n  extra:\n    function: toLocaleDateString\n    arguments: 'en_GB'\n```\n\n**Custom value generator**\n\n```yaml\n# gets translated as is\nfieldName: arrayBufferGenerator()\n```\n\nExamples using **casual** (deprecated)\n\n**Shorthand if you don't have arguments**\n\n```yaml\nfieldName: date # gets translated to casual.date()\n```\n\n**With arguments**\n\n```yaml\nfieldName: # gets translated to casual.date('YYYY-MM-DD')\n  generator: date\n  arguments: 'YYYY-MM-DD'\n```\n\n**With multiple arguments**\n\n```yaml\nfieldName: # gets translated to casual.integer(-100, 100)\n  generator: integer\n  arguments:\n    - -100\n    - 100\n```\n\n**With extra function call**\n\n```yaml\nfieldName: # gets translated to casual.integer.toFixed()\n  generator: integer\n  extra:\n    function: toFixed\n```\n\n**With extra function call arguments**\n\n```yaml\nfieldName: # gets translated to casual.integer.toFixed(3)\n  generator: integer\n  extra:\n    function: toFixed\n    arguments: 3\n```\n\n## Examples of usage\n\n**codegen.yml**\n\n```yaml\noverwrite: true\nschema: schema.graphql\ngenerates:\n  src/generated-types.ts:\n    plugins:\n      - 'typescript'\n  src/mocks/generated-mocks.ts:\n    plugins:\n      - typescript-mock-data:\n          typesFile: '../generated-types.ts'\n          enumValues: upper-case#upperCase\n          typeNames: keep\n          scalars:\n            AWSTimestamp: number.int # gets translated to faker.number.int()\n```\n\n### With `eslint-disable` rule\n\n**codegen.yml**\n\n```yaml\noverwrite: true\nschema: schema.graphql\ngenerates:\n  src/generated-types.ts:\n    plugins:\n      - 'typescript'\n  src/mocks/generated-mocks.ts:\n    plugins:\n      - add:\n          content: '/* eslint-disable @typescript-eslint/no-use-before-define,@typescript-eslint/no-unused-vars,no-prototype-builtins */'\n      - typescript-mock-data:\n          typesFile: '../generated-types.ts'\n          enumValues: upper-case#upperCase\n          typeNames: keep\n          scalars:\n            AWSTimestamp: number.int # gets translated to faker.number.int()\n```\n\n## Example of generated code\n\nGiven the following schema:\n\n```graphql\nscalar AWSTimestamp\n\ntype Avatar {\n  id: ID!\n  url: String!\n}\n\ntype User {\n  id: ID!\n  login: String!\n  avatar: Avatar\n  status: Status!\n  updatedAt: AWSTimestamp\n}\n\ntype Query {\n  user: User!\n}\n\ninput UpdateUserInput {\n  id: ID!\n  login: String\n  avatar: Avatar\n}\n\nenum Status {\n  ONLINE\n  OFFLINE\n}\n\ntype Mutation {\n  updateUser(user: UpdateUserInput): User\n}\n```\n\nThe code generated will look like:\n\n```typescript\nexport const anAvatar = (overrides?: Partial\u003cAvatar\u003e): Avatar =\u003e {\n  return {\n    id: overrides \u0026\u0026 overrides.hasOwnProperty('id') ? overrides.id! : '0550ff93-dd31-49b4-8c38-ff1cb68bdc38',\n    url: overrides \u0026\u0026 overrides.hasOwnProperty('url') ? overrides.url! : 'aliquid',\n  };\n};\n\nexport const anUpdateUserInput = (overrides?: Partial\u003cUpdateUserInput\u003e): UpdateUserInput =\u003e {\n  return {\n    id: overrides \u0026\u0026 overrides.hasOwnProperty('id') ? overrides.id! : '1d6a9360-c92b-4660-8e5f-04155047bddc',\n    login: overrides \u0026\u0026 overrides.hasOwnProperty('login') ? overrides.login! : 'qui',\n    avatar: overrides \u0026\u0026 overrides.hasOwnProperty('avatar') ? overrides.avatar! : anAvatar(),\n  };\n};\n\nexport const aUser = (overrides?: Partial\u003cUser\u003e): User =\u003e {\n  return {\n    id: overrides \u0026\u0026 overrides.hasOwnProperty('id') ? overrides.id! : 'a5756f00-41a6-422a-8a7d-d13ee6a63750',\n    login: overrides \u0026\u0026 overrides.hasOwnProperty('login') ? overrides.login! : 'libero',\n    avatar: overrides \u0026\u0026 overrides.hasOwnProperty('avatar') ? overrides.avatar! : anAvatar(),\n    status: overrides \u0026\u0026 overrides.hasOwnProperty('status') ? overrides.status! : Status.Online,\n    updatedAt: overrides \u0026\u0026 overrides.hasOwnProperty('updatedAt') ? overrides.updatedAt! : 1458071232,\n  };\n};\n```\n\n### Usage in tests\n\nThose helper functions can be used in our unit tests:\n\n```typescript\nconst user = aUser({ login: 'johndoe' });\n\n// will create a user object with `login` property overridden to `johndoe`\n```\n\n### Dealing with Timezone\n\nIf some properties use generated dates, the result could different depending on the timezone of your machine.\n\nTo force a timezone, you can set environment variable `TZ`:\n\n```bash\nTZ=UTC graphql-codegen\n```\n\nThis will force the timezone to `UTC`, whatever the timezone of your machine or CI\n\n### Contributing\n\nFeel free to open issues and pull requests. We always welcome support from the community.\n\nTo run this project locally:\n\n- Use Node \u003e= 16\n- Make sure that you have the latest Yarn version (https://yarnpkg.com/lang/en/docs/install/)\n- Clone this repo using `git clone`\n- Run `yarn`\n- Run `yarn build` to build the package\n- Run `yarn test` to make sure everything works\n\n### License\n\n[![GitHub license](https://img.shields.io/badge/license-MIT-lightgrey.svg?maxAge=2592000)](https://raw.githubusercontent.com/ardeois/graphql-codegen-typescript-mock-data/master/LICENSE)\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fardeois%2Fgraphql-codegen-typescript-mock-data","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fardeois%2Fgraphql-codegen-typescript-mock-data","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fardeois%2Fgraphql-codegen-typescript-mock-data/lists"}