Skip to content

Commit 41ba453

Browse files
committed
feat: Load GraphQL modules and return an executable schema!
0 parents  commit 41ba453

File tree

9 files changed

+12835
-0
lines changed

9 files changed

+12835
-0
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
out

.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "standard"
3+
}

.gitignore

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
# Created by https://www.gitignore.io/api/node,code
3+
4+
### Code ###
5+
# Visual Studio Code - https://code.visualstudio.com/
6+
.settings/
7+
.vscode/
8+
tsconfig.json
9+
jsconfig.json
10+
11+
### Node ###
12+
# Logs
13+
logs
14+
*.log
15+
npm-debug.log*
16+
yarn-debug.log*
17+
yarn-error.log*
18+
19+
# Runtime data
20+
pids
21+
*.pid
22+
*.seed
23+
*.pid.lock
24+
25+
# Directory for instrumented libs generated by jscoverage/JSCover
26+
lib-cov
27+
28+
# Coverage directory used by tools like istanbul
29+
coverage
30+
31+
# nyc test coverage
32+
.nyc_output
33+
34+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
35+
.grunt
36+
37+
# Bower dependency directory (https://bower.io/)
38+
bower_components
39+
40+
# node-waf configuration
41+
.lock-wscript
42+
43+
# Compiled binary addons (https://nodejs.org/api/addons.html)
44+
build/Release
45+
46+
# Dependency directories
47+
node_modules/
48+
jspm_packages/
49+
50+
# TypeScript v1 declaration files
51+
typings/
52+
53+
# Optional npm cache directory
54+
.npm
55+
56+
# Optional eslint cache
57+
.eslintcache
58+
59+
# Optional REPL history
60+
.node_repl_history
61+
62+
# Output of 'npm pack'
63+
*.tgz
64+
65+
# Yarn Integrity file
66+
.yarn-integrity
67+
68+
# dotenv environment variables file
69+
.env
70+
71+
# parcel-bundler cache (https://parceljs.org/)
72+
.cache
73+
74+
# next.js build output
75+
.next
76+
77+
# nuxt.js build output
78+
.nuxt
79+
80+
# vuepress build output
81+
.vuepress/dist
82+
83+
# Serverless directories
84+
.serverless
85+
86+
87+
# End of https://www.gitignore.io/api/node,code
88+
89+
out
90+
tests

.travis.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
language: node_js
2+
node_js:
3+
- "node"
4+
cache:
5+
directories:
6+
- ~/.npm
7+
script:
8+
- npm test
9+
- npm run docs
10+
notifications:
11+
email: false
12+
after_success:
13+
- npx travis-deploy-once --pro "npm run semantic-release"
14+
branches:
15+
except:
16+
- /^v\d+\.\d+\.\d+$/
17+
deploy:
18+
provider: pages
19+
skip-cleanup: true
20+
github-token: $GITHUB_TOKEN
21+
keep-history: true
22+
local-dir: out
23+
on:
24+
branch: master

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 eventOne, Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# GraphQL Module Loader
2+
3+
## Usage
4+
5+
```javascript
6+
const { moduleLoader } = require('gql-module-loader')
7+
const { ApolloServer } = require('apollo-server-express')
8+
9+
const Root = {
10+
schema: {
11+
typeDefs: gql``,
12+
resolvers: {}
13+
}
14+
}
15+
const Auth = {
16+
schema: {
17+
typeDefs: gql``,
18+
resolvers: {}
19+
}
20+
}
21+
22+
const { schema, rawSchema } = moduleLoader([Root, Auth])
23+
24+
const server = new ApolloServer({
25+
schema
26+
})
27+
```

lib/index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const { makeExecutableSchema } = require('graphql-tools')
2+
3+
/**
4+
* Loads GraphQL modules
5+
* @exports moduleLoader
6+
* @param {Array} modules Modules to import
7+
*
8+
* @module gql-module-loader
9+
* @returns {object} Loaded GraphQL schema
10+
*
11+
* @example
12+
* const { moduleLoader } = require('gql-module-loader')
13+
* const { schema, rawSchema } = moduleLoader([Root, Auth])
14+
*/
15+
const moduleLoader = modules => {
16+
const typeDefs = []
17+
let resolvers = {}
18+
for (const mod of modules) {
19+
// import GraphQL schemas
20+
if (mod.schema) {
21+
// merge type defs
22+
if (mod.schema.typeDefs.constructor === Array) {
23+
typeDefs.push(...mod.schema.typeDefs)
24+
} else {
25+
typeDefs.push(mod.schema.typeDefs)
26+
}
27+
28+
// merge resolvers
29+
resolvers = { ...resolvers, ...mod.schema.resolvers }
30+
}
31+
}
32+
33+
const rawSchema = {
34+
typeDefs,
35+
resolvers
36+
}
37+
const schema = makeExecutableSchema(rawSchema)
38+
39+
return { schema, rawSchema }
40+
}
41+
42+
module.exports = { moduleLoader }

0 commit comments

Comments
 (0)