@valueflows/vf-graphql
Version:
Reference GraphQL implementation of the ValueFlows spec
66 lines (53 loc) • 1.99 kB
JavaScript
/**
* Entrypoint for inflated GraphQL schema and validation helpers
*
* @package: vf-graphql
* @since: 2019-02-11
*/
const { buildASTSchema, findBreakingChanges, printSchema } = require('graphql')
const { mergeTypeDefs } = require('@graphql-tools/merge')
const { schemaModules, bridgingSchemas } = require('./schema-manifest')
function buildSchema(moduleIds, extensionSchemas, merge_options, build_options) {
// default to all modules
if (!moduleIds || !moduleIds.length) {
moduleIds = Object.keys(schemaModules)
}
if (!merge_options) {
merge_options = { throwOnConflict: true }
}
if (!build_options) {
build_options = { assumeValidSDL: false }
}
// util & pagination types are always required
moduleIds.push('util', 'pagination')
// ensure each schema is only loaded once
moduleIds = moduleIds.filter((value, index, self) => {
return self.indexOf(value) === index
}).sort()
// automatically load in any 'bridging' schemas implied by the provided module IDs
const bridgingModules = Object.keys(bridgingSchemas)
.filter((bridge) => {
const bridgeDependencies = bridge.split('.')
for (let i = 0, l = bridgeDependencies.length; i < l; ++i) {
if (moduleIds.indexOf(bridgeDependencies[i]) === -1) {
return false
}
}
return true
})
// bring in the schema fragment strings
const fragments = moduleIds.concat(bridgingModules).map(td => {
const schemaData = schemaModules[td] || bridgingSchemas[td]
if (!schemaData) {
throw new Error(`Unknown vf-graphql schema module ID: ${td}`)
}
return schemaData
})
// merge fragments and build GraphQLSchema object
return buildASTSchema(mergeTypeDefs(fragments.concat(extensionSchemas || []), merge_options), build_options)
}
module.exports = {
buildSchema,
printSchema,
validate: (oSchema, moduleIds, extensionSchemas) => findBreakingChanges(buildSchema(moduleIds, extensionSchemas), oSchema),
}