simple-graphql-assembler
Advanced tools
Comparing version 0.0.2 to 0.1.0
@@ -10,2 +10,3 @@ const path = require('path'); | ||
path.join(path.dirname(file), `${path.basename(file, '.gql')}.resolver.js`); | ||
const readFile = file => fs.readFileSync(file, { encoding: 'utf8' }); | ||
@@ -28,38 +29,23 @@ | ||
const assemble = (root = '.') => { | ||
const baseFiles = []; | ||
const baseResolvers = {}; | ||
const buildBaseResolvers = (baseResolverFiles = []) => { | ||
const errors = []; | ||
let errorFound = false; | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const files of findFiles(root)) { | ||
const [typedefs, resolvers] = files; | ||
const baseResolvers = baseResolverFiles.reduce((acc, curr) => { | ||
const rModule = include(curr); | ||
if (!rModule) return acc; | ||
baseFiles.push(...typedefs); | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const resolver of resolvers) { | ||
const rModule = include(resolver); | ||
if (rModule) { | ||
const [isValid, error] = validate(rModule); | ||
if (!isValid) { | ||
errorFound = true; | ||
errors.push({ name: resolver, error }); | ||
} else { | ||
const { __name, ...rest } = rModule; | ||
baseResolvers[__name] = rest; | ||
} | ||
} | ||
const [isValid, error] = validate(rModule); | ||
if (!isValid) { | ||
errors.push({ name: curr, error }); | ||
return acc; | ||
} | ||
} | ||
if (errorFound) { | ||
return { typeDefs: null, resolvers: null, errors }; | ||
} | ||
const { __name, ...rest } = rModule; | ||
return { ...acc, [__name]: rest }; | ||
}, {}); | ||
const typeDefs = baseFiles | ||
.map(readFile) | ||
.reduce((acc, curr) => `${acc}${curr}`, ''); | ||
return { baseResolvers, errors }; | ||
}; | ||
const buildFinalResolvers = (baseResolvers = {}) => { | ||
const resolvers = {}; | ||
@@ -89,5 +75,41 @@ | ||
return resolvers; | ||
}; | ||
const buildTypeDefinitions = (typeDefinitionFiles = []) => | ||
typeDefinitionFiles.map(readFile).reduce((acc, curr) => `${acc}${curr}`, ''); | ||
const assemble = (root = '.') => { | ||
const typeDefinitionFiles = []; | ||
const baseResolverFiles = []; | ||
Array.from(findFiles(root)).forEach(gql => { | ||
const [typeDefFiles, resolverFiles] = gql; | ||
typeDefinitionFiles.push(...typeDefFiles); | ||
baseResolverFiles.push(...resolverFiles); | ||
}); | ||
const { baseResolvers, errors } = buildBaseResolvers(baseResolverFiles); | ||
if (errors.length) { | ||
return { typeDefs: null, resolvers: null, errors }; | ||
} | ||
const typeDefs = buildTypeDefinitions(typeDefinitionFiles); | ||
const resolvers = buildFinalResolvers(baseResolvers); | ||
return { typeDefs, resolvers, errors: null }; | ||
}; | ||
const onlyTypeDefs = (root = '') => { | ||
const typeDefinitionFiles = []; | ||
Array.from(findFiles(root)).forEach(([typeDefFiles]) => { | ||
typeDefinitionFiles.push(...typeDefFiles); | ||
}); | ||
return buildTypeDefinitions(typeDefinitionFiles); | ||
}; | ||
module.exports = assemble; | ||
module.exports.onlyTypeDefs = onlyTypeDefs; |
{ | ||
"name": "simple-graphql-assembler", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Simple backend graphql shcema assembler for apollo projects", | ||
@@ -17,6 +17,11 @@ "author": "Metál Ádám <devmetal91@gmail.com> (https://github.com/devmetal)", | ||
"main": "index.js", | ||
"bin": { | ||
"make-typedefs": "./bin/index.js" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts", | ||
"package.lock", | ||
"/lib", | ||
"/bin", | ||
"!examples/", | ||
@@ -48,3 +53,4 @@ "!**/__fixtures__/", | ||
"dependencies": { | ||
"lodash": "^4.17.11" | ||
"lodash": "^4.17.11", | ||
"yargs": "^13.2.1" | ||
}, | ||
@@ -51,0 +57,0 @@ "engines": { |
@@ -105,1 +105,19 @@ [![Build Status](https://travis-ci.org/devmetal/simple-graphql-assembler.svg?branch=master)](https://travis-ci.org/devmetal/simple-graphql-assembler) | ||
``` | ||
### cli usage | ||
I added a tool for cli usage, the tool is called **make-typedefs** This tool will be traverse the current working directory for .gql files and with a very simple technique (copy) it will create one single file as a merged schema.gql. | ||
```bash | ||
Usage: make-typedefs [options] | ||
Options: | ||
--version Show version number [boolean] | ||
--root, -r Root folder of your gql definitions and resolvers [default: "."] | ||
--output, -o Output file for type definitions [default: "./schema.gql"] | ||
-h, --help Show help [boolean] | ||
Examples: | ||
make-typedefs -r . -o ./schema.gql Create the gql type definition file for | ||
your definitions from the current | ||
directory | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11989
12
167
123
2
4
+ Addedyargs@^13.2.1
+ Addedansi-regex@4.1.1(transitive)
+ Addedansi-styles@3.2.1(transitive)
+ Addedcamelcase@5.3.1(transitive)
+ Addedcliui@5.0.0(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addeddecamelize@1.2.0(transitive)
+ Addedemoji-regex@7.0.3(transitive)
+ Addedfind-up@3.0.0(transitive)
+ Addedget-caller-file@2.0.5(transitive)
+ Addedis-fullwidth-code-point@2.0.0(transitive)
+ Addedlocate-path@3.0.0(transitive)
+ Addedp-limit@2.3.0(transitive)
+ Addedp-locate@3.0.0(transitive)
+ Addedp-try@2.2.0(transitive)
+ Addedpath-exists@3.0.0(transitive)
+ Addedrequire-directory@2.1.1(transitive)
+ Addedrequire-main-filename@2.0.0(transitive)
+ Addedset-blocking@2.0.0(transitive)
+ Addedstring-width@3.1.0(transitive)
+ Addedstrip-ansi@5.2.0(transitive)
+ Addedwhich-module@2.0.1(transitive)
+ Addedwrap-ansi@5.1.0(transitive)
+ Addedy18n@4.0.3(transitive)
+ Addedyargs@13.3.2(transitive)
+ Addedyargs-parser@13.1.2(transitive)