-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
130 lines (113 loc) · 5.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* Copyright (c) 2017, Neap Pty Ltd.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
const path = require('path')
const fs = require('fs')
const glob = require('glob')
const { getSchemaParts } = require('./src/utils')
/*eslint-disable */
const CWD = process.cwd()
/*eslint-enable */
const getAppConfig = () => {
const appconfigPath = path.join(CWD, 'appconfig.json')
return fs.existsSync(appconfigPath) ? require(appconfigPath) : null
}
const isMissingSchema = gluedSchema => !gluedSchema || (!gluedSchema.schema && gluedSchema.query == 'type Query {' && gluedSchema.mutation == 'type Mutation {' && gluedSchema.subscription == 'type Subscription {')
// [description]
// @param {String} schemaFolderPath Path to the root folder containing the schema.graphql and resolver files.
// @param {Object} options Options object.
// @param {[String]} options.schemas inline schemas.
// @param {String|Array} options.ignore Defines globbing patterns to ignore certain files or folders (e.g., ignore: ['**/productquery.js', '**/variantquery.js'], ignore: ignore: 'variant/*').
// @param {String} options.mode Defines whether the GraphQL resolvers are defined using standard javascript files, typescript files or a custom globbing pattern.
// Valid values: 'js', 'ts', '<globbing pattern>'
// @return {Object} result
// @return {String} result.schema Aggregated string made of all the schemas defined in the various .graphql files under folder located at 'schemaFolderPath'
// @return {Object} result.resolve Aggregated object made of all the resolvers defined in the various .graphql files under folder located at 'schemaFolderPath'
//
const glue = (schemaFolderPath, options) => {
options = options || {}
let schemaPathInConfig = null
let ignore = null
let resolverFileGlob =
!options.mode || options.mode == 'js' ? '**/*.js' :
options.mode == 'ts' ? '**/*.ts' : options.mode
if (!schemaFolderPath) {
const appconfig = getAppConfig()
const graphql = (appconfig || {}).graphql
schemaPathInConfig = (graphql || {}).schema
ignore = (graphql || {}).ignore
}
const schemaFolderAbsPath = path.resolve(schemaFolderPath || schemaPathInConfig || 'schema')
const resolverFiles = path.join(schemaFolderAbsPath, resolverFileGlob)
const schemaGraphQlFiles = path.join(schemaFolderAbsPath, '**/*.{graphql,gql}')
const optionIgnore = options.ignore || ignore
const ignored = optionIgnore
? typeof(optionIgnore) == 'string'
? path.join(schemaFolderAbsPath, optionIgnore)
: optionIgnore.map(i => path.join(schemaFolderAbsPath, i))
: undefined
const jsFiles = glob.sync(resolverFiles, { ignore: ignored }) || []
const graphqlFiles = glob.sync(schemaGraphQlFiles, { ignore: ignored }) || []
const modules = jsFiles.map(f => require(f))
modules.push(...graphqlFiles.map(f => parseStringSchema(fs.readFileSync(f, 'utf8'))).filter(x => x))
if (options.schemas && Array.isArray(options.schemas) && options.schemas.length)
modules.push(...options.schemas.map(parseStringSchema).filter(x => x))
const gluedSchema = (modules || []).reduce((a, { schema, resolver, query, mutation, subscription }) => {
const s = schema && typeof(schema) == 'string' ? (a.schema + '\n' + schema).trim() : a.schema
const q = query && typeof(query) == 'string' ? (a.query + '\n' + query).trim() : a.query
const m = mutation && typeof(mutation) == 'string' ? (a.mutation + '\n' + mutation).trim() : a.mutation
const sub = subscription && typeof(subscription) == 'string' ? (a.subscription + '\n' + subscription).trim() : a.subscription
for(let key in resolver)
a.resolver[key] = Object.assign((a.resolver[key] || {}), (resolver[key] || {}))
return { schema: s, resolver: a.resolver, query: q, mutation: m, subscription: sub }
}, { schema: '', resolver: {}, query: 'type Query {', mutation: 'type Mutation {', subscription: 'type Subscription {' })
if (isMissingSchema(gluedSchema)) {
if (schemaPathInConfig)
throw new Error(`Missing GraphQL Schema: No schemas found under the path '${path.resolve(schemaPathInConfig)}' defined in the appconfig.json`)
else if (schemaFolderPath)
throw new Error(`Missing GraphQL Schema: No schemas found under the path '${path.resolve(schemaFolderPath)}'`)
else
throw new Error(`Missing GraphQL Schema: No schemas found under the path '${path.resolve('schema')}'`)
}
if (gluedSchema.query != 'type Query {') {
gluedSchema.query = gluedSchema.query + '\n}'
gluedSchema.schema = gluedSchema.schema + '\n' + gluedSchema.query
}
if (gluedSchema.mutation != 'type Mutation {') {
gluedSchema.mutation = gluedSchema.mutation + '\n}'
gluedSchema.schema = gluedSchema.schema + '\n' + gluedSchema.mutation
}
if (gluedSchema.subscription != 'type Subscription {') {
gluedSchema.subscription = gluedSchema.subscription + '\n}'
gluedSchema.schema = gluedSchema.schema + '\n' + gluedSchema.subscription
}
return { schema: gluedSchema.schema, resolver: gluedSchema.resolver }
}
/**
*
* @param {String} strSchema e.g., 'type Product { id:ID name:String } type Query { products(id:ID name:String): [Product] }'
*
* @return {String} output.schema
* @return {Object} output.resolver null
* @return {String} output.query
* @return {String} output.mutation
* @return {String} output.subscription
*/
const parseStringSchema = strSchema => {
const parts = getSchemaParts(strSchema)
if (!parts)
return null
else
return {
schema: parts.types ? parts.types.body : null,
resolver: null,
query: parts.query ? parts.query.body : null,
mutation: parts.mutation ? parts.mutation.body : null,
subscription: parts.subscription ? parts.subscription.body : null
}
}
module.exports = glue