node_modules.graphql.utilities.isValidJSValue.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of apollo-client-maven-plugin Show documentation
Show all versions of apollo-client-maven-plugin Show documentation
Maven plugin for generating graphql clients
The newest version!
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
exports.isValidJSValue = isValidJSValue;
var _iterall = require('iterall');
var _invariant = require('../jsutils/invariant');
var _invariant2 = _interopRequireDefault(_invariant);
var _isNullish = require('../jsutils/isNullish');
var _isNullish2 = _interopRequireDefault(_isNullish);
var _definition = require('../type/definition');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Given a JavaScript value and a GraphQL type, determine if the value will be
* accepted for that type. This is primarily useful for validating the
* runtime values of query variables.
*/
function isValidJSValue(value, type) {
// A value must be provided if the type is non-null.
if (type instanceof _definition.GraphQLNonNull) {
if ((0, _isNullish2.default)(value)) {
return ['Expected "' + String(type) + '", found null.'];
}
return isValidJSValue(value, type.ofType);
}
if ((0, _isNullish2.default)(value)) {
return [];
}
// Lists accept a non-list value as a list of one.
if (type instanceof _definition.GraphQLList) {
var itemType = type.ofType;
if ((0, _iterall.isCollection)(value)) {
var errors = [];
(0, _iterall.forEach)(value, function (item, index) {
errors.push.apply(errors, isValidJSValue(item, itemType).map(function (error) {
return 'In element #' + index + ': ' + error;
}));
});
return errors;
}
return isValidJSValue(value, itemType);
}
// Input objects check each defined field.
if (type instanceof _definition.GraphQLInputObjectType) {
if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) !== 'object' || value === null) {
return ['Expected "' + type.name + '", found not an object.'];
}
var fields = type.getFields();
var _errors = [];
// Ensure every provided field is defined.
Object.keys(value).forEach(function (providedField) {
if (!fields[providedField]) {
_errors.push('In field "' + providedField + '": Unknown field.');
}
});
// Ensure every defined field is valid.
Object.keys(fields).forEach(function (fieldName) {
var newErrors = isValidJSValue(value[fieldName], fields[fieldName].type);
_errors.push.apply(_errors, newErrors.map(function (error) {
return 'In field "' + fieldName + '": ' + error;
}));
});
return _errors;
}
!(type instanceof _definition.GraphQLScalarType || type instanceof _definition.GraphQLEnumType) ? (0, _invariant2.default)(0, 'Must be input type') : void 0;
// Scalar/Enum input checks to ensure the type can parse the value to
// a non-null value.
try {
var parseResult = type.parseValue(value);
if ((0, _isNullish2.default)(parseResult) && !type.isValidValue(value)) {
return ['Expected type "' + type.name + '", found ' + JSON.stringify(value) + '.'];
}
} catch (error) {
return ['Expected type "' + type.name + '", found ' + JSON.stringify(value) + ': ' + error.message];
}
return [];
}