From 97502e2dcf46fc9d4d711782106bcbadd51f477f Mon Sep 17 00:00:00 2001 From: Nils Knappmeier Date: Mon, 7 Aug 2017 23:54:02 +0200 Subject: [PATCH] BREAKING: Target files are now generated as "name.graphql.types.ts" Closes #30 --- .gitignore | 1 + .thought/partials/usage.md.hbs | 4 +-- CONTRIBUTING.md | 2 +- README.md | 33 ++++++++++--------- examples/example-usage.ts | 2 +- ....graphqls.ts => example.graphqls.types.ts} | 3 +- src/runCli.ts | 2 +- test/converter-spec.ts | 7 ++++ 8 files changed, 31 insertions(+), 23 deletions(-) rename examples/graphql/schema/{example.graphqls.ts => example.graphqls.types.ts} (85%) diff --git a/.gitignore b/.gitignore index e50aded..de597a6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ node_modules *.js.map *.d.ts *.js +/test/schemas/*.graphqls.types.ts diff --git a/.thought/partials/usage.md.hbs b/.thought/partials/usage.md.hbs index 082c3e4..2431942 100644 --- a/.thought/partials/usage.md.hbs +++ b/.thought/partials/usage.md.hbs @@ -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 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 65a764b..3e86762 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,4 +1,4 @@ -## Contributing +# Contributing Contributions and feedback are always welcome. The expected procedure is the following: diff --git a/README.md b/README.md index a97bf23..8a00f4d 100644 --- a/README.md +++ b/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 @@ -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 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. @@ -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 = Result | Promise | - ((args: Args, context: Ctx, info: GraphQLResolveInfo) => Result | Promise) + ((args: Args, context: Ctx) => Result | Promise) /** * The base query @@ -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 = { @@ -124,7 +123,7 @@ type Context = { // Implement the generated interface class Root implements schema.Query { - person(args: {name: string}) { + person (args: {name: string}) { return new Person(args.name, 1981) } } @@ -133,16 +132,16 @@ class Person implements schema.Person { 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 { + async friends (): Promise { return Promise.resolve([ new Person(this.name + "'s first friend", this.yearOfBirth - 1), new Person(this.name + "'s second friend", this.yearOfBirth - 2) @@ -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). \ No newline at end of file diff --git a/examples/example-usage.ts b/examples/example-usage.ts index a772d3d..dab69d1 100644 --- a/examples/example-usage.ts +++ b/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 = { diff --git a/examples/graphql/schema/example.graphqls.ts b/examples/graphql/schema/example.graphqls.types.ts similarity index 85% rename from examples/graphql/schema/example.graphqls.ts rename to examples/graphql/schema/example.graphqls.types.ts index d5f5c4a..1e8859f 100644 --- a/examples/graphql/schema/example.graphqls.ts +++ b/examples/graphql/schema/example.graphqls.types.ts @@ -1,9 +1,8 @@ /* tslint:disable */ -import {GraphQLResolveInfo} from 'graphql'; export namespace schema { export type GraphqlField = Result | Promise | - ((args: Args, context: Ctx, info: GraphQLResolveInfo) => Result | Promise) + ((args: Args, context: Ctx) => Result | Promise) /** * The base query diff --git a/src/runCli.ts b/src/runCli.ts index e243534..dc44eec 100644 --- a/src/runCli.ts +++ b/src/runCli.ts @@ -25,7 +25,7 @@ export async function runCli (cliArgs: CliArgs): Promise { 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) diff --git a/test/converter-spec.ts b/test/converter-spec.ts index f4f6270..61fe774 100644 --- a/test/converter-spec.ts +++ b/test/converter-spec.ts @@ -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))