From 393eb5ef3a85e3786e8dd53110a61a52f8cc19ed Mon Sep 17 00:00:00 2001 From: Allan Lukwago Date: Thu, 8 Jun 2017 20:18:22 +0300 Subject: [PATCH 1/6] globing and merging types in *.gql files --- packages/cli/src/index.ts | 7 ++++++- packages/util/package.json | 10 +++++++++- packages/util/src/index.ts | 1 + packages/util/src/schema.ts | 13 +++++++++++++ test/util.js | 5 +++++ 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index c8ce132..5af4079 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -6,6 +6,7 @@ import { readFile, writeToFile, PossibleSchemaInput, + gqlGlobHandler } from '@gql2ts/util'; import { ISchemaToInterfaceOptions, generateNamespace } from '@gql2ts/from-schema'; import fromQuery from '@gql2ts/from-query'; @@ -66,7 +67,11 @@ if (!process.stdin.isTTY) { }); process.stdin.on('end', () => run(JSON.parse(input), program as any)); } else if (fileName) { - const schema: string = readFile(fileName); + let schema: string = ''; + if (fileName.includes('*') && fileName.includes('.gql')) { + schema = gqlGlobHandler(fileName); + } + schema = readFile(fileName); run(schema, program as any); } else { console.error('No input specified. Please use stdin or a file name.'); diff --git a/packages/util/package.json b/packages/util/package.json index c6a0ba8..80ee979 100644 --- a/packages/util/package.json +++ b/packages/util/package.json @@ -28,6 +28,14 @@ }, "homepage": "https://github.com/avantcredit/gql2ts#readme", "dependencies": { - "graphql": "^0.9.2" + "glob": "^7.1.2", + "graphql": "^0.9.2", + "graphql-tag": "^2.2.2", + "graphql-tools": "^1.0.0", + "merge-graphql-schemas": "0.0.17" + }, + "peerDependencies": { + "graphql-tag": "^2.2.2", + "graphql-tools": "^1.0.0" } } diff --git a/packages/util/src/index.ts b/packages/util/src/index.ts index 8c571de..4169dae 100644 --- a/packages/util/src/index.ts +++ b/packages/util/src/index.ts @@ -8,4 +8,5 @@ export { isList, isNonNullable, isEnum, + gqlGlobHandler } from './schema'; diff --git a/packages/util/src/schema.ts b/packages/util/src/schema.ts index af040d8..b601de3 100644 --- a/packages/util/src/schema.ts +++ b/packages/util/src/schema.ts @@ -8,6 +8,9 @@ import { GraphQLList, GraphQLEnumType, } from 'graphql'; +import * as glob from 'glob'; +import * as fs from 'fs'; +import { mergeTypes } from 'merge-graphql-schemas'; export type PossibleIntrospectionInputs = { data: IntrospectionQuery } | IntrospectionQuery; export type PossibleSchemaInput = GraphQLSchema | string | PossibleIntrospectionInputs; @@ -41,3 +44,13 @@ export function isList (type: GraphQLType): type is GraphQLList { export function isEnum (type: GraphQLType): type is GraphQLEnumType { return type instanceof GraphQLEnumType; } + +const normalizeWhitespace: (str: string) => string = str => str.replace(/\s+/g, ' ').trim(); + +export const gqlGlobHandler: (pattern: string) => string = (pattern) => { + const files: string[] = glob.sync(pattern); + const content: string[] = files.map((fileName: string) => fs.readFileSync(fileName).toString()); + const schema: string = mergeTypes(content).join(''); + fs.writeFile('.test', normalizeWhitespace(schema)); + return schema; +}; diff --git a/test/util.js b/test/util.js index c710c9e..7e0821e 100644 --- a/test/util.js +++ b/test/util.js @@ -36,6 +36,11 @@ describe('schema', () => { it('throws on other', () => { expect(() => utils.schemaFromInputs({})).to.throw('Invalid Schema Input'); }) + + it('should glob .gql files and return a merged schema', () => { + const schema = utils.gqlGlobHandler('**/*.gql'); + expect(utils.schemaFromInputs(schema)).to.be.instanceof(GraphQLSchema); + }) }); describe('IO stuff', () => { From c2e70ada22c28f6dfe25ceeaa08cd3064f070a68 Mon Sep 17 00:00:00 2001 From: Allan Lukwago Date: Thu, 8 Jun 2017 20:25:10 +0300 Subject: [PATCH 2/6] test stubs --- test/data/products.gql | 8 ++++++++ test/data/vendor.gql | 8 ++++++++ 2 files changed, 16 insertions(+) create mode 100644 test/data/products.gql create mode 100644 test/data/vendor.gql diff --git a/test/data/products.gql b/test/data/products.gql new file mode 100644 index 0000000..4d0715a --- /dev/null +++ b/test/data/products.gql @@ -0,0 +1,8 @@ + type Product { + id: Int! + description: String + price: Int + } + type Query { + products: [Product] + } \ No newline at end of file diff --git a/test/data/vendor.gql b/test/data/vendor.gql new file mode 100644 index 0000000..ad82e2c --- /dev/null +++ b/test/data/vendor.gql @@ -0,0 +1,8 @@ + type Client { + id: Int! + name: String + age: Int + } + type Query { + clients: [Client] + } From 154b5d5d604220b36cf944eb43a197dd06dc4a48 Mon Sep 17 00:00:00 2001 From: Allan Lukwago Date: Thu, 8 Jun 2017 20:46:44 +0300 Subject: [PATCH 3/6] util / schema file correction --- packages/util/src/schema.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/util/src/schema.ts b/packages/util/src/schema.ts index b601de3..50b86d0 100644 --- a/packages/util/src/schema.ts +++ b/packages/util/src/schema.ts @@ -45,12 +45,9 @@ export function isEnum (type: GraphQLType): type is GraphQLEnumType { return type instanceof GraphQLEnumType; } -const normalizeWhitespace: (str: string) => string = str => str.replace(/\s+/g, ' ').trim(); - export const gqlGlobHandler: (pattern: string) => string = (pattern) => { const files: string[] = glob.sync(pattern); const content: string[] = files.map((fileName: string) => fs.readFileSync(fileName).toString()); const schema: string = mergeTypes(content).join(''); - fs.writeFile('.test', normalizeWhitespace(schema)); return schema; }; From 41592e4fe31cfe0e3506b8787a9effce1ccffea1 Mon Sep 17 00:00:00 2001 From: Allan Lukwago Date: Sun, 18 Jun 2017 16:05:15 +0300 Subject: [PATCH 4/6] glob functionality enhancements & corrections --- packages/cli/Readme.md | 2 +- packages/cli/src/index.ts | 7 +------ packages/util/package.json | 9 +-------- packages/util/src/fileIO.ts | 12 +++++++++++- packages/util/src/index.ts | 3 +-- packages/util/src/schema.ts | 10 ---------- test/util.js | 4 ++-- 7 files changed, 17 insertions(+), 30 deletions(-) diff --git a/packages/cli/Readme.md b/packages/cli/Readme.md index ad57f0a..d1944f0 100644 --- a/packages/cli/Readme.md +++ b/packages/cli/Readme.md @@ -5,7 +5,7 @@ npm install -g gql2ts ``` ```shell -Usage: index [options] +Usage: index [options] <**/**.gql> Options: diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 5af4079..c8ce132 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -6,7 +6,6 @@ import { readFile, writeToFile, PossibleSchemaInput, - gqlGlobHandler } from '@gql2ts/util'; import { ISchemaToInterfaceOptions, generateNamespace } from '@gql2ts/from-schema'; import fromQuery from '@gql2ts/from-query'; @@ -67,11 +66,7 @@ if (!process.stdin.isTTY) { }); process.stdin.on('end', () => run(JSON.parse(input), program as any)); } else if (fileName) { - let schema: string = ''; - if (fileName.includes('*') && fileName.includes('.gql')) { - schema = gqlGlobHandler(fileName); - } - schema = readFile(fileName); + const schema: string = readFile(fileName); run(schema, program as any); } else { console.error('No input specified. Please use stdin or a file name.'); diff --git a/packages/util/package.json b/packages/util/package.json index cdd702b..5779a47 100644 --- a/packages/util/package.json +++ b/packages/util/package.json @@ -30,13 +30,6 @@ "dependencies": { "glob": "^7.1.2", "graphql": "^0.10.1", - "graphql-tag": "^2.2.2", - "graphql-tools": "^1.0.0", - "merge-graphql-schemas": "0.0.17" - }, - "peerDependencies": { - "graphql-tag": "^2.2.2", - "graphql-tools": "^1.0.0", - "graphql": "^0.10.1" + "merge-graphql-schemas": "0.0.19" } } diff --git a/packages/util/src/fileIO.ts b/packages/util/src/fileIO.ts index af3a399..0b3e2dc 100644 --- a/packages/util/src/fileIO.ts +++ b/packages/util/src/fileIO.ts @@ -1,6 +1,16 @@ import * as fs from 'fs'; +import * as glob from 'glob'; +import { mergeTypes } from 'merge-graphql-schemas'; export const badWriteHandler: (err?: Error) => void = err => { if (err) { throw err; } }; -export const readFile: (fileName: string) => string = fileName => JSON.parse(fs.readFileSync(fileName).toString()); +export const readFile: (fileName: string) => string = fileName => { + const isGqlFile: boolean = ['gql', 'graphql'].some(ext => fileName.includes(ext)); + if (isGqlFile) { + const globbedFilePaths: string[] = glob.sync(fileName); + const content: string[] = globbedFilePaths.map((filePath: string) => fs.readFileSync(filePath).toString()); + return mergeTypes(content).join(''); + } + return JSON.parse(fs.readFileSync(fileName).toString()); +}; export const writeToFile: (fileName: string, data: string) => void = (fileName, data) => fs.writeFile(fileName, data, badWriteHandler); diff --git a/packages/util/src/index.ts b/packages/util/src/index.ts index 4169dae..207a543 100644 --- a/packages/util/src/index.ts +++ b/packages/util/src/index.ts @@ -7,6 +7,5 @@ export { schemaFromInputs, isList, isNonNullable, - isEnum, - gqlGlobHandler + isEnum } from './schema'; diff --git a/packages/util/src/schema.ts b/packages/util/src/schema.ts index 50b86d0..af040d8 100644 --- a/packages/util/src/schema.ts +++ b/packages/util/src/schema.ts @@ -8,9 +8,6 @@ import { GraphQLList, GraphQLEnumType, } from 'graphql'; -import * as glob from 'glob'; -import * as fs from 'fs'; -import { mergeTypes } from 'merge-graphql-schemas'; export type PossibleIntrospectionInputs = { data: IntrospectionQuery } | IntrospectionQuery; export type PossibleSchemaInput = GraphQLSchema | string | PossibleIntrospectionInputs; @@ -44,10 +41,3 @@ export function isList (type: GraphQLType): type is GraphQLList { export function isEnum (type: GraphQLType): type is GraphQLEnumType { return type instanceof GraphQLEnumType; } - -export const gqlGlobHandler: (pattern: string) => string = (pattern) => { - const files: string[] = glob.sync(pattern); - const content: string[] = files.map((fileName: string) => fs.readFileSync(fileName).toString()); - const schema: string = mergeTypes(content).join(''); - return schema; -}; diff --git a/test/util.js b/test/util.js index 7e0821e..1c4085c 100644 --- a/test/util.js +++ b/test/util.js @@ -37,8 +37,8 @@ describe('schema', () => { expect(() => utils.schemaFromInputs({})).to.throw('Invalid Schema Input'); }) - it('should glob .gql files and return a merged schema', () => { - const schema = utils.gqlGlobHandler('**/*.gql'); + it('should glob .gql or graphql files and return a merged schema', () => { + const schema = utils.readFile('**/*.gql'); expect(utils.schemaFromInputs(schema)).to.be.instanceof(GraphQLSchema); }) }); From 56579ce8fa8c72323d083abdf9dde1290baf6ad5 Mon Sep 17 00:00:00 2001 From: Allan Lukwago Date: Sun, 18 Jun 2017 16:06:45 +0300 Subject: [PATCH 5/6] add missing new line --- test/data/products.gql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/data/products.gql b/test/data/products.gql index 4d0715a..b4c8771 100644 --- a/test/data/products.gql +++ b/test/data/products.gql @@ -5,4 +5,5 @@ } type Query { products: [Product] - } \ No newline at end of file + } + \ No newline at end of file From 9f269e543bde124b5e8bd77a28b2d6ec5604ab67 Mon Sep 17 00:00:00 2001 From: Allan Lukwago Date: Tue, 20 Jun 2017 06:59:01 +0300 Subject: [PATCH 6/6] merge-graphql-schemas version update --- packages/util/package.json | 2 +- packages/util/src/fileIO.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/util/package.json b/packages/util/package.json index 5779a47..543cace 100644 --- a/packages/util/package.json +++ b/packages/util/package.json @@ -30,6 +30,6 @@ "dependencies": { "glob": "^7.1.2", "graphql": "^0.10.1", - "merge-graphql-schemas": "0.0.19" + "merge-graphql-schemas": "0.0.22" } } diff --git a/packages/util/src/fileIO.ts b/packages/util/src/fileIO.ts index 0b3e2dc..bb5602e 100644 --- a/packages/util/src/fileIO.ts +++ b/packages/util/src/fileIO.ts @@ -8,7 +8,7 @@ export const readFile: (fileName: string) => string = fileName => { if (isGqlFile) { const globbedFilePaths: string[] = glob.sync(fileName); const content: string[] = globbedFilePaths.map((filePath: string) => fs.readFileSync(filePath).toString()); - return mergeTypes(content).join(''); + return mergeTypes(content); } return JSON.parse(fs.readFileSync(fileName).toString()); };