Skip to content

Commit

Permalink
feat: Add support for validating the GraphQL schema string
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasdao committed Dec 12, 2021
1 parent 960b795 commit edcebeb
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 5 deletions.
182 changes: 180 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@
"author": "Nicolas Dao",
"license": "BSD-3-Clause",
"devDependencies": {
"@rollup/plugin-node-resolve": "^13.0.6",
"chai": "^4.3.4",
"eslint": "^7.32.0",
"mocha": "^9.1.2",
"rollup": "^2.58.0",
"rollup-plugin-multi-input": "^1.3.1",
"standard-version": "^9.3.1"
},
"dependencies": {
"graphql": "^16.1.0"
}
}
6 changes: 5 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import multiInput from 'rollup-plugin-multi-input'
import { nodeResolve } from '@rollup/plugin-node-resolve'

export default {
input: ['src/**/*.mjs'], // Thanks to 'rollup-plugin-multi-input', an array can be used instead of a single string.
Expand All @@ -8,5 +9,8 @@ export default {
chunkFileNames: '[name]-[hash].cjs',
entryFileNames: '[name].cjs'
},
plugins:[multiInput()]
plugins:[
multiInput(),
nodeResolve()
]
}
12 changes: 10 additions & 2 deletions src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { parse } from 'graphql'

export const parseToAST = parse

export function Schemax(...items) {
const _typeResolutions = []
Expand Down Expand Up @@ -71,9 +73,12 @@ const _compileBody = (body, options) => {
if (Array.isArray(_body)) {
const enums = _body.filter(x => x && x != '__required' && x != '!' && x != '__noempty' && x != '!0' && x.indexOf('__name') != 0 && x.indexOf('#') != 0)
if (!enums.length)
throw new Error('Invalid \'body\'. Array cannot be empty.')
throw new Error('Invalid enum. Array cannot be empty.')
if (enums.some(x => typeof(x) != 'string'))
throw new Error('Invalid \'body\' item. When \'body\' is an array, all items must be strings.')
throw new Error('Invalid enum item. When \'body\' is an array, all items must be strings.')
const invalidEnum = enums.find(e => /[^a-zA-Z0-9_]/.test(e) || /^[0-9]/.test(e))
if (invalidEnum)
throw new Error(`Invalid enum '${invalidEnum}'. Enums can only have letters, numbers, or underscores, and the first character can't be a number.`)
required = _body.some(x => x == '__required' || x == '!')
noempty = _body.some(x => x == '__noempty' || x == '!0')
const enumsName = _body.find(x => x && (x.indexOf('__name') == 0 || x.indexOf('#') == 0))
Expand Down Expand Up @@ -341,6 +346,9 @@ const _transpileSchema = (items, typeResolutions) => {
schema += '}'
}

// Validate the GraphQL schema
parse(schema)

return schema
}

Expand Down
Loading

0 comments on commit edcebeb

Please sign in to comment.