{"id":13727229,"url":"https://github.com/develomark/graphql-cli-generate-fragments","last_synced_at":"2025-04-10T13:34:44.605Z","repository":{"id":57156927,"uuid":"118763894","full_name":"develomark/graphql-cli-generate-fragments","owner":"develomark","description":"Generate Fragments for Graphql Schemas","archived":false,"fork":false,"pushed_at":"2018-12-04T13:18:21.000Z","size":77,"stargazers_count":48,"open_issues_count":6,"forks_count":17,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T12:13:46.016Z","etag":null,"topics":["graphcool","graphql","graphql-cli","graphql-server","prisma","react"],"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/develomark.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}},"created_at":"2018-01-24T12:41:55.000Z","updated_at":"2024-09-02T17:42:39.000Z","dependencies_parsed_at":"2022-08-30T05:32:42.592Z","dependency_job_id":null,"html_url":"https://github.com/develomark/graphql-cli-generate-fragments","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/develomark%2Fgraphql-cli-generate-fragments","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/develomark%2Fgraphql-cli-generate-fragments/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/develomark%2Fgraphql-cli-generate-fragments/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/develomark%2Fgraphql-cli-generate-fragments/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/develomark","download_url":"https://codeload.github.com/develomark/graphql-cli-generate-fragments/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248018050,"owners_count":21034047,"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":["graphcool","graphql","graphql-cli","graphql-server","prisma","react"],"created_at":"2024-08-03T01:03:45.226Z","updated_at":"2025-04-10T13:34:44.571Z","avatar_url":"https://github.com/develomark.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# graphql-cli-generate-fragments [![npm](https://img.shields.io/npm/v/graphql-cli-generate-fragments.svg)](https://www.npmjs.com/package/graphql-cli-generate-fragments)\n\nGenerates GraphQL fragments for each type in the project schema.\n\n## Installation\n\n```npm i -g graphql-cli graphql-cli-generate-fragments```\n\n## Usage\n```\ngraphql generate-fragments\n\nGenerate Fragments for Graphql Schemas\n\nOptions:\n  --dotenv         Path to .env file                                    [string]\n  -p, --project    Project name                                         [string]\n  --output, -o     Output folder                                        [string]\n  --save, -s       Save settings to config file     [boolean] [default: \"false\"]\n\"false\"]\n  --generator, -g  Generate to 'js' or 'graphq'                 [string]\n  --verbose        Show verbose output messages     [boolean] [default: \"false\"]\n  -h, --help       Show help                                           [boolean]\n  -v, --version    Show version number                                 [boolean]\n```\n\n\n### Graphql Fragments Generation\nCreates graphql fragments containing the fields for each type in the supplied schema.\n\nFor more information on fragments and their use: (https://www.apollographql.com/docs/react/features/fragments.html)\n\nThe first time you use fragment generation in your project, you need to provide an output folder for your fragments, and the generator you want to use:\n```shell\n$ graphql generate-fragments -p database -o src/generated -g graphql --save\n✔ Fragments for project database written to src/generated/database.fragments.js\n```\nThis will also save the configuration in your `.graphqlconfig` file (see below).\n\n### Automating `graphql generate-fragments`\nAfter you have set up fragment generation for all projects, you can simply run `graphql generate-fragments` without any parameters to process all projects:\n```shell\n$ graphql generate-fragments\n✔ Fragments for project app written to src/generated/app.fragments.graphql\n✔ Fragments for project database written to src/generated/database.fragments.js\n```\n\n## Fragments Usage and Examples\n\n### Generated Fragments\nThere are three types of fragments outputted by `graphql-cli-generate-fragments`.\n\nGiven the schema:\n\n```graphql\n\ntype User implements Node {\n  id: ID!\n  email: String!\n  password: String!\n  posts: [Post!]\n}\n\ntype Post  {\n  id: ID!\n  createdAt: DateTime!\n  updatedAt: DateTime!\n  isPublished: Boolean!\n  title: String!\n  text: String!\n  author: User!\n}\n\n```\n\nThe following fragments are generated:\n\n```graphql\n\nfragment User on User {\n  id\n  email\n  password\n  posts {\n    ...PostNoNesting\n  }\n}\n\nfragment Post on Post {\n  id\n  createdAt\n  updatedAt\n  isPublished\n  title\n  text\n  author {\n    ...UserNoNesting\n  }\n}\n\nfragment UserNoNesting on User {\n  id\n  email\n  password\n}\n\nfragment PostNoNesting on Post {\n  id\n  createdAt\n  updatedAt\n  isPublished\n  title\n  text\n}\n\n```\n\nNotice that we generate `_NoNesting` fragments, which do not include relations. Post and User would be recursive otherwise. If there is a recursive fragment you will receive a `\"Cannot spread fragment within itself\"` error.\n\n#### Deeply Nested Fragments\nWhen there is no recursive nesting of fragments it can be useful to include all related types queries. `_DeepNesting` fragments are generated for this use.\n\nGiven the following schema:\n\n```graphql\n\ntype User implements Node {\n  id: ID!\n  email: String!\n  password: String!\n  details: UserDetails!\n}\n\ntype UserDetails {\n  firstName: String!\n  lastName: String!\n  address: Address!\n}\n\ntype Address {\n  line1: String!\n  line2: String\n  county: String\n  postcode: String!\n}\n\n```\n\nThe following is also generated:\n\n```graphql\n\nfragment UserDeepNesting on User {\n  id\n  email\n  password\n  details {\n    ...UserDetails\n  }\n}\n\nfragment UserDetailsDeepNesting on UserDetails {\n  firstName\n  lastName\n  address {\n    ...Address\n  }\n}\n\nfragment AddressDeepNesting on Address {\n  line1\n  line2\n  county\n  postcode\n}\n\n```\n\n### Use with Apollo Graphql Tag Loader\n\nBy using [`graphql-tag/loader`](https://github.com/apollographql/graphql-tag) with Webpack you can import fragments into `.graphql` files:\n\n```graphql\n\n#import \"../generated/app.fragments.graphql\"\n\nquery CurrentUser {\n  currentUser {\n    ...User\n  }\n}\n\n```\n\nor into javascript\n\n```javascript\n\nimport { User } from \"../generated/app.fragments.graphql\"\n\nconst query = gql`\n    query CurrentUser {\n    currentUser {\n      ...User\n    }\n  }\n\n  ${User}\n\n```\n\n### Use with JS\n\nIf you are unable to use Webpack - fragments can be generated to javascript models (see below)\n\n```javascript\n\nimport { User } from \"../generated/app.fragments.js\"\n\nconst query = gql`\n    query CurrentUser {\n    currentUser {\n      ...User\n    }\n  }\n\n  ${User}\n\n```\n\n## Available generators\nThe following generators are provided:\n\n| Generator    | Purpose                                      |\n| ------------ | -------------------------------------------- |\n| graphql | Generates fragments for all types in schema  |\n| js | Wraps the graphql and exports them for import in javascript  |\n\n\n## `graphql-config` extensions\n\nTo store the project configuration for fragment generation, `graphql-cli-generate-fragments` uses two extension keys in the `graphql-config` configuration file. These keys can be set manually, or using the `--save` parameter.\n```diff\n# ./.graphqlconfig.yml\nprojects:\n  app:\n    schemaPath: src/schema.graphql\n    extensions:\n      endpoints:\n        default: 'http://localhost:4000'\n+   generate-fragments:\n+     output: src/generated/app.fragments.js\n+     generator: js\n  database:\n    schemaPath: src/generated/prisma.graphql\n    extensions:\n      prisma: database/prisma.yml\n+     generate-fragments:\n+       output: src/generated/database.fragments.graphql\n+       generator: graphql\n\n```\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details\n\n## Acknowledgments\n\n* [lachenmayer](https://github.com/lachenmayer) for [`graphql-fragment-codegen`](https://github.com/lachenmayer/graphql-fragment-codegen)\n* [kbrandwijk](https://github.com/kbrandwijk)/[supergraphql](https://github.com/supergraphql) for [`graphql-cli-prepare`](https://github.com/supergraphql/graphql-cli-prepare)\n\n\u003chr\u003e\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://img.shields.io/badge/built-with_♡-red.svg?style=for-the-badge\"/\u003e\u003ca href=\"https://github.com/develomark\" target=\"-_blank\"\u003e\u003cimg src=\"https://img.shields.io/badge/by-develomark-yellow.svg?longCache=true\u0026style=for-the-badge\"/\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelomark%2Fgraphql-cli-generate-fragments","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevelomark%2Fgraphql-cli-generate-fragments","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelomark%2Fgraphql-cli-generate-fragments/lists"}