Skip to content

Commit

Permalink
BREAKING: Target files are now generated as "name.graphql.types.ts"
Browse files Browse the repository at this point in the history
Closes #30
  • Loading branch information
nknapp committed Aug 16, 2017
1 parent fe35e2a commit 97502e2
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ node_modules
*.js.map
*.d.ts
*.js
/test/schemas/*.graphqls.types.ts
4 changes: 2 additions & 2 deletions .thought/partials/usage.md.hbs
Expand Up @@ -10,9 +10,9 @@ The source GraphQL-schema `example.graphqls` that looks like

{{include 'examples/graphql/schema/example.graphqls' 'graphql'}}

will be converted into the following `example.graphqls.ts`:
will be converted into the following `example.graphqls.types.ts`:

{{include 'examples/graphql/schema/example.graphqls.ts' 'ts'}}
{{include 'examples/graphql/schema/example.graphqls.types.ts' 'ts'}}

Note that all the field (non-argument) types can either be

Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
@@ -1,4 +1,4 @@
## Contributing
# Contributing

Contributions and feedback are always welcome. The expected procedure is the following:

Expand Down
33 changes: 17 additions & 16 deletions README.md
@@ -1,10 +1,9 @@
# graphql-typewriter

[![NPM version](https://badge.fury.io/js/graphql-typewriter.svg)](http://badge.fury.io/js/graphql-typewriter)
[![NPM version](https://img.shields.io/npm/v/graphql-typewriter.svg)](https://npmjs.com/package/graphql-typewriter)
[![Travis Build Status](https://travis-ci.org/nknapp/graphql-typewriter.svg?branch=master)](https://travis-ci.org/nknapp/graphql-typewriter)
[![Coverage Status](https://img.shields.io/coveralls/nknapp/graphql-typewriter.svg)](https://coveralls.io/r/nknapp/graphql-typewriter)


> Easy TypeScript interfaces for your GraphQL server

Expand All @@ -22,12 +21,13 @@ Usage: graphql-typewriter [options]
Convert all .graphqls schema-files in the current directory tree into typescript
interfaces that can be used to implement a graphql-root for this schema.
Options:
-h, --help output usage information
-V, --version output the version number
-x, --exclude <dirs> a list of directories to exclude
--dont-save-same-file do not save a file if the contents has not changed. This read each target file prior to loading
-h, --help output usage information
```

`graphql-typewriter` is assumed to be run in the root folder of a npm-project.
Expand Down Expand Up @@ -57,15 +57,14 @@ type Person {
```


will be converted into the following `example.graphqls.ts`:
will be converted into the following `example.graphqls.types.ts`:

```ts
/* tslint:disable */
import {GraphQLResolveInfo} from 'graphql';

export namespace schema {
export type GraphqlField<Args, Result, Ctx> = Result | Promise<Result> |
((args: Args, context: Ctx, info: GraphQLResolveInfo) => Result | Promise<Result>)
((args: Args, context: Ctx) => Result | Promise<Result>)

/**
* The base query
Expand Down Expand Up @@ -114,8 +113,8 @@ For fields with arguments, only the latter two apply.
With this interface, you can write the following program (`example-usage.ts`):

```ts
import {graphql, buildSchema} from 'graphql'
import {schema} from './graphql/schema/example.graphqls'
import { graphql, buildSchema } from 'graphql'
import { schema } from './graphql/schema/example.graphqls.types'
import * as fs from 'fs'

type Context = {
Expand All @@ -124,7 +123,7 @@ type Context = {

// Implement the generated interface
class Root implements schema.Query<Context> {
person(args: {name: string}) {
person (args: {name: string}) {
return new Person(args.name, 1981)
}
}
Expand All @@ -133,16 +132,16 @@ class Person implements schema.Person<Context> {
name: string
yearOfBirth: number

constructor(name: string, yearOfBirth: number) {
constructor (name: string, yearOfBirth: number) {
this.name = name
this.yearOfBirth = yearOfBirth
}

age(_, context: Context) {
age (_, context: Context) {
return context.year - this.yearOfBirth
}

async friends(): Promise<Person[]> {
async friends (): Promise<Person[]> {
return Promise.resolve([
new Person(this.name + "'s first friend", this.yearOfBirth - 1),
new Person(this.name + "'s second friend", this.yearOfBirth - 2)
Expand Down Expand Up @@ -189,15 +188,17 @@ The output of this program is



## License
# License

`graphql-typewriter` is published under the MIT-license.

`graphql-typewriter` is published under the MIT-license.
See [LICENSE.md](LICENSE.md) for details.

## Release-Notes

# Release-Notes

For release notes, see [CHANGELOG.md](CHANGELOG.md)

## Contributing guidelines
# Contributing guidelines

See [CONTRIBUTING.md](CONTRIBUTING.md).
2 changes: 1 addition & 1 deletion examples/example-usage.ts
@@ -1,5 +1,5 @@
import { graphql, buildSchema } from 'graphql'
import { schema } from './graphql/schema/example.graphqls'
import { schema } from './graphql/schema/example.graphqls.types'
import * as fs from 'fs'

type Context = {
Expand Down
@@ -1,9 +1,8 @@
/* tslint:disable */
import {GraphQLResolveInfo} from 'graphql';

export namespace schema {
export type GraphqlField<Args, Result, Ctx> = Result | Promise<Result> |
((args: Args, context: Ctx, info: GraphQLResolveInfo) => Result | Promise<Result>)
((args: Args, context: Ctx) => Result | Promise<Result>)

/**
* The base query
Expand Down
2 changes: 1 addition & 1 deletion src/runCli.ts
Expand Up @@ -25,7 +25,7 @@ export async function runCli (cliArgs: CliArgs): Promise<any> {
const converter = new Converter()

const promises = files.map(async (sourceFile) => {
const targetFile = sourceFile + '.ts'
const targetFile = sourceFile + '.types.ts'
try {
const source = await mfs.read(sourceFile, {encoding: 'utf-8'})
const ts = await converter.convert(source)
Expand Down
7 changes: 7 additions & 0 deletions test/converter-spec.ts
Expand Up @@ -36,6 +36,13 @@ describe('gql2ts-for-server:', function () {

it(`should handle ${file} correctly`, async function () {
const source = fixture(file)
// The naming scheme of test fixtures is intentionally
// different from the naming schema of the executable
// The executable converts a.graphql to a.graphql.types.ts
// and the test-fixture is "a.ts"
// We don't want test fixture generated by accidentally
// running 'graphql-typewriter' in the project dir
// That would always make the test pass...
const target = source.replace(/\.graphqls$/,'.ts')

const result = await converter.convert(read(source))
Expand Down

0 comments on commit 97502e2

Please sign in to comment.