Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/cli/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ npm install -g gql2ts
```

```shell
Usage: index [options] <schema.json> <query.gql>
Usage: index [options] <schema.json> <query.gql> <**/**.gql>

Options:

Expand Down
4 changes: 3 additions & 1 deletion packages/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
},
"homepage": "https://github.com/avantcredit/gql2ts#readme",
"dependencies": {
"graphql": "^0.10.1"
"glob": "^7.1.2",
"graphql": "^0.10.1",
"merge-graphql-schemas": "0.0.22"
}
}
12 changes: 11 additions & 1 deletion packages/util/src/fileIO.ts
Original file line number Diff line number Diff line change
@@ -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);
}
return JSON.parse(fs.readFileSync(fileName).toString());
};
export const writeToFile: (fileName: string, data: string) => void =
(fileName, data) => fs.writeFile(fileName, data, badWriteHandler);
2 changes: 1 addition & 1 deletion packages/util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export {
schemaFromInputs,
isList,
isNonNullable,
isEnum,
isEnum
} from './schema';
9 changes: 9 additions & 0 deletions test/data/products.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type Product {
id: Int!
description: String
price: Int
}
type Query {
products: [Product]
}

8 changes: 8 additions & 0 deletions test/data/vendor.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type Client {
id: Int!
name: String
age: Int
}
type Query {
clients: [Client]
}
5 changes: 5 additions & 0 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ describe('schema', () => {
it('throws on other', () => {
expect(() => utils.schemaFromInputs({})).to.throw('Invalid Schema Input');
})

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);
})
});

describe('IO stuff', () => {
Expand Down