graphql-paper
Version:
A flexible in-memory store based on a GraphQL Schema
38 lines (36 loc) • 1.25 kB
JavaScript
;
var graphql = require('graphql');
var typeExists = require('../graphql/type-exists.js');
var typeDoesNotExist = require('./errors/type-does-not-exist.js');
var typeIsNotDocumentCompatible = require('./errors/type-is-not-document-compatible.js');
var validateField = require('./validate-field.js');
var nullDocument = require('../document/null-document.js');
function validate(graphqlSchema, document, store, validators) {
if (nullDocument.isNullDocument(document)) {
return;
}
const typeName = document.__typename;
const type = graphqlSchema.getType(typeName);
if (!type) {
throw new Error(`Type ${typeName} does not exist in the graphql schema.`);
}
if (!graphql.isObjectType(type)) {
throw new typeIsNotDocumentCompatible.TypeIsNotDocumentCompatible({
type
});
}
if (!typeExists.typeExists(graphqlSchema, typeName)) {
throw new typeDoesNotExist.TypeDoesNotExist({
typename: typeName
});
}
const combinedValidators = [...validators.document, validateField.validateField(validators.field)];
combinedValidators.forEach(validator => validator.validate({
store,
graphqlSchema,
document,
type
}));
}
exports.validate = validate;
//# sourceMappingURL=validate.js.map