Skip to content

Instantly share code, notes, and snippets.

@feliperohdee
Created April 12, 2021 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feliperohdee/2390762df78b341622a5b205e69d4f54 to your computer and use it in GitHub Desktop.
Save feliperohdee/2390762df78b341622a5b205e69d4f54 to your computer and use it in GitHub Desktop.
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