0.2.3 • Published 6 years ago

@zachariahtimothy/graphql-resolver-codegen v0.2.3

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

Note: This project is still WIP.

Introduction

Generate TS Resolvers

Feature

  1. Autogenerate resolver types
  2. Supports graphql-import
  3. Suports prettier i.e. code is generated following the code styling practices of your project.

Usage

graphql-resolver-codegen --help
Usage: graphql-resolver-codegen <command> -s [schema-path] -o [output-path] -g [generator] -i
[interfaces]

Options:
  --help             Show help                                         [boolean]
  --version          Show version number                               [boolean]
  -s, --schema-path  GraphQL schema file path                         [required]
  -o, --output       Output file/folder path [default:
                     ./generated/resolvers[.ts]]
  -g, --generator    Generator to use [default: typescript, options: reason]
  -i, --interfaces   Path to the interfaces folder used for scaffolding
  -f, --force        Force write files when there is a clash while scaffolding

  Possible commands: scaffold, interfaces

Commands

CommandDescriptionFlag -sFlag -oFlag -gFlag -iFlag -f
interfacesGenerate type safe interfaces for resolvers based on your schema.Path to input schemaPath to generate interfaces file attypescriptNANA
scaffoldScaffold resolvers based on your schema that rely on the generated interface for type safety.Path to input schemaPath to folder for generating resolverstypescriptPath to the generated interfaces fileForce write resolver files when there is a collision with existing files

Note: while using scaffold, typemap.ts will always be generated irrespective of -f flag.

Example

To generate both resolvers and typings for a given GraphQL schema, run the following commands
  1. Run graphql-resolver-codegen interfaces -s <schema-path> -o <output-path>/generated/resolvers.ts -g typescript

  2. Run graphql-resolver-codegen scaffold -s <schema-path> -o <output-path> -g typescript -i <output-path>/generated/resolvers.ts

Not the scaffold command take an additional argument i which adds import for generated interfaces in scaffolded code.

To see an example in action, please open the example directory.

Design Decisions

  1. Code generator imports all the generated types interfaces and exports a collective Types interface in typemap.ts.

  2. Interface for Context is generated in a separate file called Context.ts.

  3. The command scaffold always writes the typemap.ts file, irrespective of the -f flag.