
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
graphql-cli-generate-fragments-fix
Advanced tools
Generates GraphQL fragments for each type in the project schema.
npm i -g graphql-cli graphql-cli-generate-fragments
graphql generate-fragments
Generate Fragments for Graphql Schemas
Options:
--dotenv Path to .env file [string]
-p, --project Project name [string]
--output, -o Output folder [string]
--save, -s Save settings to config file [boolean] [default: "false"]
"false"]
--generator, -g Generate to 'js' or 'graphq' [string]
--verbose Show verbose output messages [boolean] [default: "false"]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
Creates graphql fragments containing the fields for each type in the supplied schema.
For more information on fragments and their use: (https://www.apollographql.com/docs/react/features/fragments.html)
The 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:
$ graphql generate-fragments -p database -o src/generated -g graphql --save
✔ Fragments for project database written to src/generated/database.fragments.js
This will also save the configuration in your .graphqlconfig file (see below).
graphql generate-fragmentsAfter you have set up fragment generation for all projects, you can simply run graphql generate-fragments without any parameters to process all projects:
$ graphql generate-fragments
✔ Fragments for project app written to src/generated/app.fragments.graphql
✔ Fragments for project database written to src/generated/database.fragments.js
There are three types of fragments outputted by graphql-cli-generate-fragments.
Given the schema:
type User implements Node {
id: ID!
email: String!
password: String!
posts: [Post!]
}
type Post {
id: ID!
createdAt: DateTime!
updatedAt: DateTime!
isPublished: Boolean!
title: String!
text: String!
author: User!
}
The following fragments are generated:
fragment User on User {
id
email
password
posts {
...PostNoNesting
}
}
fragment Post on Post {
id
createdAt
updatedAt
isPublished
title
text
author {
...UserNoNesting
}
}
fragment UserNoNesting on User {
id
email
password
}
fragment PostNoNesting on Post {
id
createdAt
updatedAt
isPublished
title
text
}
Notice 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.
When there is no recursive nesting of fragments it can be useful to include all related types queries. _DeepNesting fragments are generated for this use.
Given the following schema:
type User implements Node {
id: ID!
email: String!
password: String!
details: UserDetails!
}
type UserDetails {
firstName: String!
lastName: String!
address: Address!
}
type Address {
line1: String!
line2: String
county: String
postcode: String!
}
The following is also generated:
fragment UserDeepNesting on User {
id
email
password
details {
...UserDetails
}
}
fragment UserDetailsDeepNesting on UserDetails {
firstName
lastName
address {
...Address
}
}
fragment AddressDeepNesting on Address {
line1
line2
county
postcode
}
By using graphql-tag/loader with Webpack you can import fragments into .graphql files:
#import "../generated/app.fragments.graphql"
query CurrentUser {
currentUser {
...User
}
}
or into javascript
import { User } from "../generated/app.fragments.graphql"
const query = gql`
query CurrentUser {
currentUser {
...User
}
}
${User}
If you are unable to use Webpack - fragments can be generated to javascript models (see below)
import { User } from "../generated/app.fragments.js"
const query = gql`
query CurrentUser {
currentUser {
...User
}
}
${User}
The following generators are provided:
| Generator | Purpose |
|---|---|
| graphql | Generates fragments for all types in schema |
| js | Wraps the graphql and exports them for import in javascript |
graphql-config extensionsTo 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.
# ./.graphqlconfig.yml
projects:
app:
schemaPath: src/schema.graphql
extensions:
endpoints:
default: 'http://localhost:4000'
+ generate-fragments:
+ output: src/generated/app.fragments.js
+ generator: js
database:
schemaPath: src/generated/prisma.graphql
extensions:
prisma: database/prisma.yml
+ generate-fragments:
+ output: src/generated/database.fragments.graphql
+ generator: graphql
This project is licensed under the MIT License - see the LICENSE.md file for details
FAQs
Plugin for graphql-cli to generate fragments
The npm package graphql-cli-generate-fragments-fix receives a total of 0 weekly downloads. As such, graphql-cli-generate-fragments-fix popularity was classified as not popular.
We found that graphql-cli-generate-fragments-fix demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.