All Projects → develomark → graphql-cli-generate-fragments

develomark / graphql-cli-generate-fragments

Licence: MIT license
Generate Fragments for Graphql Schemas

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to graphql-cli-generate-fragments

Prisma Kubernetes Deployment
Demo how to deploy a Prisma server to a Kubernetes cluster.
Stars: ✭ 89 (+106.98%)
Mutual labels:  graphcool, prisma
Prisma Tools
Prisma tools to help you generate CRUD system for GraphQL servers
Stars: ✭ 237 (+451.16%)
Mutual labels:  graphql-server, prisma
Graphql Up
Get a ready-to-use GraphQL API for your schema
Stars: ✭ 415 (+865.12%)
Mutual labels:  graphql-server, graphcool
bunjil
A GraphQL bastion server with schema merging, authentication and authorization with Policy Based Access Control
Stars: ✭ 25 (-41.86%)
Mutual labels:  graphql-server, prisma
Graphql Prisma Typescript
🏡 GraphQL server reference implementation (Airbnb clone) in Typescript using Prisma & graphql-yoga
Stars: ✭ 723 (+1581.4%)
Mutual labels:  graphql-server, prisma
finch-graphql
Local GraphQL API in the background process of a web extension.
Stars: ✭ 48 (+11.63%)
Mutual labels:  graphql-server, graphql-cli
calendso
Scheduling infrastructure for absolutely everyone.
Stars: ✭ 13,100 (+30365.12%)
Mutual labels:  prisma
graphql-playground
🎮 GraphQL IDE for better development workflows (GraphQL Subscriptions, interactive docs & collaboration)
Stars: ✭ 8,103 (+18744.19%)
Mutual labels:  prisma
database-schema-examples
Database Schema Examples we strive to support in Prisma
Stars: ✭ 94 (+118.6%)
Mutual labels:  prisma
express-server-jwt
A simple express server that handles JWT authentication
Stars: ✭ 34 (-20.93%)
Mutual labels:  prisma
graphql-to-sql
GraphQL model to SQL
Stars: ✭ 13 (-69.77%)
Mutual labels:  prisma
bramble
The Movio GraphQL Gateway
Stars: ✭ 423 (+883.72%)
Mutual labels:  graphql-server
stack
🥭 nxpm-stack lets you generate a complete and opinionated full-stack application in a Nx Workspace, ready to extend and deploy!
Stars: ✭ 98 (+127.91%)
Mutual labels:  prisma
ds-visualizer
This is an ongoing project based on data structures. We will be presenting and explaining the code of each basic data structure with the help of a visualizer.
Stars: ✭ 14 (-67.44%)
Mutual labels:  prisma
gondor
Nestjs Framework, Prisma database layer w/ Angular and Apollo.
Stars: ✭ 14 (-67.44%)
Mutual labels:  prisma
apollo-express-ts-server-boilerplate
No description or website provided.
Stars: ✭ 29 (-32.56%)
Mutual labels:  graphql-server
fullstack-ts-boilerplate
Full-stack boilerplate in TS using modern technology
Stars: ✭ 25 (-41.86%)
Mutual labels:  prisma
dataguide
🗄️ Prisma's Data Guide - A growing library of articles focused on making databases more approachable.
Stars: ✭ 101 (+134.88%)
Mutual labels:  prisma
thvu-blog
My digital home on the internet.
Stars: ✭ 51 (+18.6%)
Mutual labels:  prisma
docker-api-graphql
GraphQL API wrapper around the Docker Remote API. SpringBoot-based app, written in Kotlin
Stars: ✭ 13 (-69.77%)
Mutual labels:  graphql-server

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

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]

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:

$ 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).

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:

$ 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

Fragments Usage and Examples

Generated Fragments

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.

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:

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
}

Use with Apollo Graphql Tag Loader

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}

Use with JS

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}

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.

# ./.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

License

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

Acknowledgments


Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].