Coder Social home page Coder Social logo

cybernetics / graphql-code-generator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dotansimha/graphql-code-generator

0.0 1.0 0.0 2.19 MB

GraphQL code generator with flexible support for custom templates

Home Page: https://medium.com/@dotansimha/graphql-code-generator-a34e3785e6fb

License: MIT License

Shell 0.80% JavaScript 1.57% TypeScript 94.53% HTML 3.10%

graphql-code-generator's Introduction

GraphQL Code Generator

npm version Build Status code style: prettier renovate-app badge

Overview

GraphQL Code Generator v0.11 — Generate React and Angular Apollo Components, Resolver signatures and much more!

What’s new in graphql-code-generator 0.9.0? @ Medium

GraphQL Codegen blog post & examples @ Medium

GraphQL code generator, with flexible support for multiple languages and platforms, and the ability to create custom generated projects based on GraphQL schema or operations.

GraphQL entities are defined as static and typed, which means they can be analyzed and use as a base for generating everything.

This generator generates both models (based on GraphQL server-side schema), and documents (client-side operations, such as query, mutation as subscription).

Use Cases

The codegen let's you either use a predefined template, or write your own.

The predefined templates are common templates that helps GraphQL developers.

For example, if you develop a server-side with TypeScript and GraphQL, you can use the codegen with the TypeScript template to generate Typings for you server side, based on your schema.

If you develop a client-side with TypeScript, Angular and GraphQL, you can use the codegen with the TypeScript template to generate Typings for you client-side, based on your schema and your queries/mutations.

Available Templates:

Language Purpose Package Name & Docs
TypeScript Generate server-side TypeScript types, and client-side typings graphql-codegen-typescript-template
MongoDB TypeScript Models Generate server-side TypeScript types, with MongoDB models graphql-codegen-typescript-mongodb-template
Apollo Angular Typescript Generate TypeScript types, and Apollo Angular Services graphql-codegen-apollo-angular-template
React Apollo Typescript Generate TypeScript types, and React Apollo Components graphql-codegen-typescript-react-apollo-template
XSD Generate XSD file graphql-xsd
Introspection Generate Introspection file graphql-codegen-introspection-template

If you are looking for the Flow / Swift generators, please note that we will implement it soon again, but you can use 0.5.5 from NPM.

Note: In order to use GraphQL directives feature, please use GraphQL > 0.9.4 as peer dependency for the generator!

Installation

To install the generator, use the following:

$ npm install --save-dev graphql-code-generator graphql
// Or, with Yarn
$ yarn add -D graphql-code-generator graphql

Then, install the template package you wish to use, for example:

$ npm install --save-dev graphql-codegen-typescript-template
// Or, with Yarn
$ yarn add -D graphql-codegen-typescript-template

And then to use it, execute it from NPM script, or use $(npm bin)/gql-gen ... from the command line. If you are using Yarn, you can just use yarn gql-gen ...

You can also install it as global NPM module and use it with gql-gen executable.

Usage Examples

This package offers both modules exports (to use with NodeJS/JavaScript application), or CLI util.

CLI usage is as follow:

$ gql-gen [options] [documents ...]
  • With local introspection JSON file, generate TypeScript types:

      $ gql-gen --schema mySchema.json --template graphql-codegen-typescript-template --out ./typings/ "./src/**/*.graphql"
    
  • With local introspection JSON file, generate TypeScript files, from GraphQL documents inside code files (.ts):

      $ gql-gen --schema mySchema.json --template graphql-codegen-typescript-template --out ./typings/ "./src/**/*.ts"
    
  • With remote GraphQL endpoint that requires Authorization, generate TypeScript types:

      $ gql-gen --schema http://localhost:3010/graphql --header "Authorization: MY_KEY" --template graphql-codegen-typescript-template --out ./typings/ "./src/**/*.graphql"
    

Note: when specifying a glob path (with * or **), make sure to wrap the argument with double quotes ("...").

CLI Options

Allowed flags:

Flag Name Type Description
-s,--schema String Local or remote path to GraphQL schema: Introspection JSON file, GraphQL server endpoint to fetch the introspection from, local file that exports GraphQLSchema, JSON object or AST string, or a Glob expression for .graphql files ("./src/**/*.graphql")
-r,--require String Path to a require extension, read this for more info
-h,--header String Header to add to the introspection HTTP request when using remote endpoint
-t,--template String Template name, for example: "typescript" (not required when using --project)
-p,--project String Project directory with templates (refer to "Custom Templates" section)
--config String Path to project config JSON file (refer to "Custom Templates" section), defaults to gqlgen.json
-o,--out String Path for output file/directory. When using single-file generator specify filename, and when using multiple-files generator specify a directory
-m,--skip-schema void If specified, server side schema won't be generated through the template (enums won't omit)
-c,--skip-documents void If specified, client side documents won't be generated through the template
--no-overwrite void If specified, the generator will not override existing files
-w --watch boolean Enables watch mode for regenerating documents from schema
documents... [String] Space separated paths of .graphql files or code files (glob path is supported) that contains GraphQL documents inside strings, or with either gql or graphql tag (JavaScript), this field is optional - if no documents specified, only server side schema types will be generated

Output Examples

This repository includes some examples for generated outputs under dev-test directory.

Integrate with your project

To use inside an existing project, I recommend to add a pre-build script that executes the code generator, inside you package.json, for example:

{
  "name": "my-project",
  "scripts": {
    "prebuild": "gql-gen ...",
    "build": "..."
  }
}

Generator-specific config

Some of the generators supports a custom config, which you can specify using gqlgen.json like that:

{
  "generatorConfig": {
    "printTime": true
  }
}

Or, you can set the value using environment variables, before executing the codegen, with the CODEGEN_ prefix:

CODEGEN_PRINT_TIME=true gql-gen --template ...

Programmatic Usage

If you with to use this package as a library and execute it from your code, do the following:

import { generate } from 'graphql-code-generator';

function doSomething() {
  const generatedFiles = await generate({
    template: 'typescript',
    url: 'http://127.0.0.1:3000/graphql',
    out: process.cwd() + '/models/'
  });
}

The method generate accepts two parameters: (options: CLIOptions, saveToFile: boolean), and returns Promise<FileOutput[]>.

Custom Templates

To create custom template, or generate a whole project from GraphQL schema, refer to Custom Templates Documentation

Watch mode

The watch mode is enabled by passing --watch option. To use the watch mode you need to have Watchman installed in your system. To support glob patterns, version 3.7 and above is required.

Prettier Support

The generator will automatically executes prettier on the output code, when possible. It will automatically use the correct parser according to the file extensions of the output file. In case of an error, prettier will be ignored, and will write the file as-is.

If you project has prettier config file, the generator will use it and respect your code-style.

TypeScript Support

If you are using TypeScript and would like to use your GraphQL Schema from a local file (using --schema), you can use --require to load a require extension.

For example, install ts-node from NPM and use it this way:

gql-gen --require ts-node/register --template typescript --schema ./src/my-schema.ts --out ./src/models/

This way, the file ./src/my-schema.ts is loaded directly as TypeScript file, and you don't need to compile it to plain JavaScript before using it.

Other Environments

If you are using GraphQL with environment different from NodeJS and wish to generate types and interfaces for your platform, start by installing NodeJS and the package as global, and then add the generation command to your build process.

Difference with apollo-codegen

apollo-codegen generates a similar results, but it based on code that generates the results. This package uses templates (with Handlebars) to generate results, and it basically supports any output because you can simply create you template and then compile it with your GraphQL schema and GraphQL operations and get a more customized result. This package also allow you to create custom templates, regardless the built-in generators, so you can use your schema as source to any generated result you need.

Troubleshoot

If you have issues with the generator, feel free open issues in this repository.

If you report a bug or execution issue, please run the generator with DEBUG=true gql-gel ... and provide the debug log.

Contributing

Feel free to open issues (for bugs/questions) and create pull requests (add generators / fix bugs).

License

GitHub license

MIT

graphql-code-generator's People

Contributors

alexgorbatchev avatar altschuler avatar anthonyzou avatar ardatan avatar arvind-chandrashekar-ck-zz avatar darthtrevino avatar davidyaha avatar dotansimha avatar dxcx avatar eritikass avatar fohte avatar fredyc avatar greenkeeper[bot] avatar guilhermehubner avatar jonaskello avatar jontem avatar justinjiadev avatar jycouet avatar kamilkisiela avatar lucasavila00 avatar michaelchiche avatar micimize avatar mixail-novikov avatar orta avatar prevostc avatar renovate-bot avatar renovate[bot] avatar sgoll avatar vojtastanek avatar zephraph avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.