项目作者: develomark

项目描述 :
Generate Fragments for Graphql Schemas
高级语言: TypeScript
项目地址: git://github.com/develomark/graphql-cli-generate-fragments.git
创建时间: 2018-01-24T12:41:55Z
项目社区:https://github.com/develomark/graphql-cli-generate-fragments

开源协议:MIT License

下载


graphql-cli-generate-fragments npm

Generates GraphQL fragments for each type in the project schema.

Installation

npm i -g graphql-cli graphql-cli-generate-fragments

Usage

  1. graphql generate-fragments
  2. Generate Fragments for Graphql Schemas
  3. Options:
  4. --dotenv Path to .env file [string]
  5. -p, --project Project name [string]
  6. --output, -o Output folder [string]
  7. --save, -s Save settings to config file [boolean] [default: "false"]
  8. "false"]
  9. --generator, -g Generate to 'js' or 'graphq' [string]
  10. --verbose Show verbose output messages [boolean] [default: "false"]
  11. -h, --help Show help [boolean]
  12. -v, --version Show version number [boolean]

Graphql Fragments Generation

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:

  1. $ graphql generate-fragments -p database -o src/generated -g graphql --save
  2. Fragments for project database written to src/generated/database.fragments.js

This will also save the configuration in your .graphqlconfig file (see below).

Automating graphql generate-fragments

After you have set up fragment generation for all projects, you can simply run graphql generate-fragments without any parameters to process all projects:

  1. $ graphql generate-fragments
  2. Fragments for project app written to src/generated/app.fragments.graphql
  3. Fragments for project database written to src/generated/database.fragments.js

Fragments Usage and Examples

Generated Fragments

There are three types of fragments outputted by graphql-cli-generate-fragments.

Given the schema:

  1. type User implements Node {
  2. id: ID!
  3. email: String!
  4. password: String!
  5. posts: [Post!]
  6. }
  7. type Post {
  8. id: ID!
  9. createdAt: DateTime!
  10. updatedAt: DateTime!
  11. isPublished: Boolean!
  12. title: String!
  13. text: String!
  14. author: User!
  15. }

The following fragments are generated:

  1. fragment User on User {
  2. id
  3. email
  4. password
  5. posts {
  6. ...PostNoNesting
  7. }
  8. }
  9. fragment Post on Post {
  10. id
  11. createdAt
  12. updatedAt
  13. isPublished
  14. title
  15. text
  16. author {
  17. ...UserNoNesting
  18. }
  19. }
  20. fragment UserNoNesting on User {
  21. id
  22. email
  23. password
  24. }
  25. fragment PostNoNesting on Post {
  26. id
  27. createdAt
  28. updatedAt
  29. isPublished
  30. title
  31. text
  32. }

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.

Deeply Nested Fragments

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:

  1. type User implements Node {
  2. id: ID!
  3. email: String!
  4. password: String!
  5. details: UserDetails!
  6. }
  7. type UserDetails {
  8. firstName: String!
  9. lastName: String!
  10. address: Address!
  11. }
  12. type Address {
  13. line1: String!
  14. line2: String
  15. county: String
  16. postcode: String!
  17. }

The following is also generated:

  1. fragment UserDeepNesting on User {
  2. id
  3. email
  4. password
  5. details {
  6. ...UserDetails
  7. }
  8. }
  9. fragment UserDetailsDeepNesting on UserDetails {
  10. firstName
  11. lastName
  12. address {
  13. ...Address
  14. }
  15. }
  16. fragment AddressDeepNesting on Address {
  17. line1
  18. line2
  19. county
  20. postcode
  21. }

Use with Apollo Graphql Tag Loader

By using graphql-tag/loader with Webpack you can import fragments into .graphql files:

  1. #import "../generated/app.fragments.graphql"
  2. query CurrentUser {
  3. currentUser {
  4. ...User
  5. }
  6. }

or into javascript

  1. import { User } from "../generated/app.fragments.graphql"
  2. const query = gql`
  3. query CurrentUser {
  4. currentUser {
  5. ...User
  6. }
  7. }
  8. ${User}

Use with JS

If you are unable to use Webpack - fragments can be generated to javascript models (see below)

  1. import { User } from "../generated/app.fragments.js"
  2. const query = gql`
  3. query CurrentUser {
  4. currentUser {
  5. ...User
  6. }
  7. }
  8. ${User}

Available generators

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 extensions

To 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.

  1. # ./.graphqlconfig.yml
  2. projects:
  3. app:
  4. schemaPath: src/schema.graphql
  5. extensions:
  6. endpoints:
  7. default: 'http://localhost:4000'
  8. + generate-fragments:
  9. + output: src/generated/app.fragments.js
  10. + generator: js
  11. database:
  12. schemaPath: src/generated/prisma.graphql
  13. extensions:
  14. prisma: database/prisma.yml
  15. + generate-fragments:
  16. + output: src/generated/database.fragments.graphql
  17. + generator: graphql

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments