https://github.com/stipsan/graphql-field-resolver-to-typescript
Export ts definitions from your server .graphql files to strictly type your field resolvers
https://github.com/stipsan/graphql-field-resolver-to-typescript
Last synced: about 1 month ago
JSON representation
Export ts definitions from your server .graphql files to strictly type your field resolvers
- Host: GitHub
- URL: https://github.com/stipsan/graphql-field-resolver-to-typescript
- Owner: stipsan
- License: mit
- Created: 2017-10-18T14:38:28.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T08:17:38.000Z (over 2 years ago)
- Last Synced: 2024-05-02T06:14:35.469Z (about 2 years ago)
- Language: TypeScript
- Homepage:
- Size: 160 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# graphql-field-resolver-to-typescript ยท [](https://circleci.com/gh/stipsan/graphql-field-resolver-to-typescript) [](https://www.npmjs.com/package/graphql-field-resolver-to-typescript) [](https://github.com/semantic-release/semantic-release)
> This project started its life as a fork of [graphql-typewriter](https://www.npmjs.com/package/graphql-typewriter)
# Usage
## ย ย ๐ Local package.json
```bash
$ yarn add --dev graphql-field-resolver-to-typescript
$ gqlfr2ts **/*.graphql -o schema.d.ts
```
## ย ๐ย Global dependency
```bash
$ npm install -g graphql-field-resolver-to-typescript
$ gqlfr2ts **/*.graphql -o schema.d.ts
```
## ๐๐จย Using [npx](https://github.com/zkat/npx#npx1----execute-npm-package-binaries)
```bash
$ npx graphql-field-resolver-to-typescript **/*.graphql -o schema.d.ts
```
# API
```bash
$ gqlfr2ts
```
## ``
You can provide a list over files or use stdin to pipe from other cli programs.
## `--output`, `-o` `path`
Optionally specify where to write the output. If not specified it'll pipe to stdout so you can pipe it to any cli program you want.
## Examples
```bash
$ gqlfr2ts schema.graphql
```
Processes the file schema.graphql and prints to stdout
```bash
$ cat schema.graphql | gqlfr2ts --output schema.d.ts
```
Processes the input from stdin and writes output to schema.d.ts
```bash
$ gqlfr2ts RootQuery.graphql User.graphql --output schema.d.ts
```
Stitch a schema together and output a complete typescript definition for all the related resolvers
```bash
$ cat **/*.graphql | gqlfr2ts > schema.d.ts
```
Stitch a schema together from stdin and pipe it to stdout and use the shell to write output to file.
This is the most performant solution when you have a lot of files that combine to a big schema.
## `.graphql` to `.ts` examples
### input
```graphql
type RootQuery {
# A field description
field1: TypeA
# Another field description
field2: TypeB
}
# A simple type
# Multiline description
type TypeA {
name: String
size: Int
}
# Another more complex type
type TypeB {
nested: [TypeA]
}
schema {
query: RootQuery
}
```
### output
```typescript
/* tslint:disable */
import { GraphQLResolveInfo } from 'graphql'
type ID = string
export type GraphqlField =
| Result
| Promise
| ((
source: Source,
args: Args,
context: Ctx,
info: GraphQLResolveInfo
) => Result | Promise)
export interface RootQuery {
/**
* A field description
*/
field1?: GraphqlField, {}, TypeA | undefined, Ctx>
/**
* Another field description
*/
field2?: GraphqlField, {}, TypeB | undefined, Ctx>
}
/**
* A simple type
* Multiline description
*/
export interface TypeA {
name?: GraphqlField, {}, string | undefined, Ctx>
size?: GraphqlField, {}, number | undefined, Ctx>
}
/**
* Another more complex type
*/
export interface TypeB {
nested?: GraphqlField<
TypeB,
{},
(TypeA | undefined)[] | undefined,
Ctx
>
}
export interface field1Args {}
export interface field2Args {}
export const defaultResolvers = {}
```
### usage in resolvers
```
@TODO this will come soon, this is a very young project and there's a lot of edge cases to iron out
```