Skip to content

Commit

Permalink
feat(loader): added basic schema loader and validator from JSON intro…
Browse files Browse the repository at this point in the history
…spection file
  • Loading branch information
dotansimha committed Dec 14, 2016
1 parent 12b4458 commit 13b3dec
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 2 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,11 @@
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"dependencies": {
"@types/node": "^0.0.2",
"commander": "^2.9.0",
"graphql": "^0.8.2",
"typed-graphql": "^1.0.2"
}
}
Empty file added src/cli.ts
Empty file.
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env node

console.log('test');
import {loadSchema} from './scheme-loader';

console.log(loadSchema('./schema.json'));
24 changes: 24 additions & 0 deletions src/scheme-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {buildClientSchema, GraphQLSchema, IntrospectionQuery} from 'graphql';
import * as fs from 'fs';

export const loadFileContent = (filePath: string): IntrospectionQuery => {
if (fs.existsSync(filePath)) {
return <IntrospectionQuery>(JSON.parse(fs.readFileSync(filePath, 'utf8')));
} else {
throw new Error(`File ${filePath} does not exists!`);
}
};


export const validateSchema = (schema: IntrospectionQuery) => {
if (!schema.__schema) {
throw new Error('Invalid schema provided!');
}
};

export const loadSchema = (filePath: string): GraphQLSchema => {
const schemaObject = loadFileContent(filePath);
validateSchema(schemaObject);

return buildClientSchema(schemaObject);
};
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"module": "es2015",
"module": "commonjs",
"target": "es5",
"lib": ["es6", "dom"],
"noImplicitAny": false,
Expand Down
2 changes: 2 additions & 0 deletions typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference types="typed-graphql" />
/// <reference types="@types/node" />
28 changes: 28 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
# yarn lockfile v1


"@types/node@^0.0.2":
version "0.0.2"
resolved "https://registry.yarnpkg.com/@types/node/-/node-0.0.2.tgz#0da4934902fbf68a97a0f53204a35ad2224917bc"

ansi-align@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/ansi-align/-/ansi-align-1.1.0.tgz#2f0c1658829739add5ebb15e6b0c6e3423f016ba"
Expand Down Expand Up @@ -75,6 +79,12 @@ colors@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/colors/-/colors-1.1.2.tgz#168a4701756b6a7f51a12ce0c97bfa28c084ed63"

commander@^2.9.0:
version "2.9.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.9.0.tgz#9c99094176e12240cb22d6c5146098400fe0f7d4"
dependencies:
graceful-readlink ">= 1.0.0"

[email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
Expand Down Expand Up @@ -207,6 +217,16 @@ graceful-fs@^4.1.2:
version "4.1.11"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658"

"graceful-readlink@>= 1.0.0":
version "1.0.1"
resolved "https://registry.yarnpkg.com/graceful-readlink/-/graceful-readlink-1.0.1.tgz#4cafad76bc62f02fa039b2f94e9a3dd3a391a725"

graphql@^0.8.2:
version "0.8.2"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-0.8.2.tgz#eb1bb524b38104bbf2c9157f9abc67db2feba7d2"
dependencies:
iterall "1.0.2"

has-ansi@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/has-ansi/-/has-ansi-2.0.0.tgz#34f5049ce1ecdf2b0649af3ef24e45ed35416d91"
Expand Down Expand Up @@ -272,6 +292,10 @@ isarray@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"

[email protected]:
version "1.0.2"
resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.0.2.tgz#41a2e96ce9eda5e61c767ee5dc312373bb046e91"

latest-version@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/latest-version/-/latest-version-2.0.0.tgz#56f8d6139620847b8017f8f1f4d78e211324168b"
Expand Down Expand Up @@ -526,6 +550,10 @@ tslint@^4.0.2:
underscore.string "^3.3.4"
update-notifier "^1.0.2"

typed-graphql@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/typed-graphql/-/typed-graphql-1.0.2.tgz#4c0f788775d552df4d4ec3d73f25469252f40fb8"

typescript@^2.0.10:
version "2.1.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.1.1.tgz#41c2b64472f529331b2055c0424862b44ce58d42"
Expand Down

0 comments on commit 13b3dec

Please sign in to comment.