Created
April 12, 2021 14:32
-
-
Save feliperohdee/2390762df78b341622a5b205e69d4f54 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const graphql = require('graphql'); | |
const _ = require('lodash'); | |
const constants = require('../constants'); | |
const { | |
parse, | |
validate, | |
validateSchema, | |
execute, | |
specifiedRules | |
} = graphql; | |
const parseMemoized = _.memoize(source => { | |
return parse(source); | |
}); | |
module.exports = async (args = {}) => { | |
let { | |
schema, | |
source | |
} = args; | |
// no need to validate schema each time in production, it is validated in tests | |
if (!constants.config.production) { | |
const schemaValidationErrors = validateSchema(schema); | |
if (_.size(schemaValidationErrors)) { | |
return { | |
errors: schemaValidationErrors | |
}; | |
} | |
} | |
const documentAST = parseMemoized(source); | |
if (!constants.config.production) { | |
// enable fields with same name and different types on union types | |
const rules = _.filter(specifiedRules, rule => { | |
return rule.name !== 'OverlappingFieldsCanBeMergedRule'; | |
}); | |
const errors = validate( | |
schema, | |
documentAST, | |
rules | |
); | |
if (_.size(errors)) { | |
return { | |
errors | |
}; | |
} | |
} | |
return await execute( | |
schema, | |
documentAST, | |
args.rootValue || {}, | |
args.contextValue || {}, | |
args.variableValues || {}, | |
args.operationName || null | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment