WIP - add extractor, generate snippet_data
This commit is contained in:
222
node_modules/graphql/utilities/valueFromAST.mjs
generated
vendored
Normal file
222
node_modules/graphql/utilities/valueFromAST.mjs
generated
vendored
Normal file
@ -0,0 +1,222 @@
|
||||
import objectValues from '../polyfills/objectValues';
|
||||
import inspect from '../jsutils/inspect';
|
||||
import keyMap from '../jsutils/keyMap';
|
||||
import isInvalid from '../jsutils/isInvalid';
|
||||
import { Kind } from '../language/kinds';
|
||||
import { isScalarType, isEnumType, isInputObjectType, isListType, isNonNullType } from '../type/definition';
|
||||
|
||||
/**
|
||||
* Produces a JavaScript value given a GraphQL Value AST.
|
||||
*
|
||||
* A GraphQL type must be provided, which will be used to interpret different
|
||||
* GraphQL Value literals.
|
||||
*
|
||||
* Returns `undefined` when the value could not be validly coerced according to
|
||||
* the provided type.
|
||||
*
|
||||
* | GraphQL Value | JSON Value |
|
||||
* | -------------------- | ------------- |
|
||||
* | Input Object | Object |
|
||||
* | List | Array |
|
||||
* | Boolean | Boolean |
|
||||
* | String | String |
|
||||
* | Int / Float | Number |
|
||||
* | Enum Value | Mixed |
|
||||
* | NullValue | null |
|
||||
*
|
||||
*/
|
||||
export function valueFromAST(valueNode, type, variables) {
|
||||
if (!valueNode) {
|
||||
// When there is no node, then there is also no value.
|
||||
// Importantly, this is different from returning the value null.
|
||||
return;
|
||||
}
|
||||
|
||||
if (isNonNullType(type)) {
|
||||
if (valueNode.kind === Kind.NULL) {
|
||||
return; // Invalid: intentionally return no value.
|
||||
}
|
||||
|
||||
return valueFromAST(valueNode, type.ofType, variables);
|
||||
}
|
||||
|
||||
if (valueNode.kind === Kind.NULL) {
|
||||
// This is explicitly returning the value null.
|
||||
return null;
|
||||
}
|
||||
|
||||
if (valueNode.kind === Kind.VARIABLE) {
|
||||
var variableName = valueNode.name.value;
|
||||
|
||||
if (!variables || isInvalid(variables[variableName])) {
|
||||
// No valid return value.
|
||||
return;
|
||||
}
|
||||
|
||||
var variableValue = variables[variableName];
|
||||
|
||||
if (variableValue === null && isNonNullType(type)) {
|
||||
return; // Invalid: intentionally return no value.
|
||||
} // Note: This does no further checking that this variable is correct.
|
||||
// This assumes that this query has been validated and the variable
|
||||
// usage here is of the correct type.
|
||||
|
||||
|
||||
return variableValue;
|
||||
}
|
||||
|
||||
if (isListType(type)) {
|
||||
var itemType = type.ofType;
|
||||
|
||||
if (valueNode.kind === Kind.LIST) {
|
||||
var coercedValues = [];
|
||||
var _iteratorNormalCompletion = true;
|
||||
var _didIteratorError = false;
|
||||
var _iteratorError = undefined;
|
||||
|
||||
try {
|
||||
for (var _iterator = valueNode.values[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
||||
var itemNode = _step.value;
|
||||
|
||||
if (isMissingVariable(itemNode, variables)) {
|
||||
// If an array contains a missing variable, it is either coerced to
|
||||
// null or if the item type is non-null, it considered invalid.
|
||||
if (isNonNullType(itemType)) {
|
||||
return; // Invalid: intentionally return no value.
|
||||
}
|
||||
|
||||
coercedValues.push(null);
|
||||
} else {
|
||||
var itemValue = valueFromAST(itemNode, itemType, variables);
|
||||
|
||||
if (isInvalid(itemValue)) {
|
||||
return; // Invalid: intentionally return no value.
|
||||
}
|
||||
|
||||
coercedValues.push(itemValue);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
_didIteratorError = true;
|
||||
_iteratorError = err;
|
||||
} finally {
|
||||
try {
|
||||
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
||||
_iterator.return();
|
||||
}
|
||||
} finally {
|
||||
if (_didIteratorError) {
|
||||
throw _iteratorError;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return coercedValues;
|
||||
}
|
||||
|
||||
var coercedValue = valueFromAST(valueNode, itemType, variables);
|
||||
|
||||
if (isInvalid(coercedValue)) {
|
||||
return; // Invalid: intentionally return no value.
|
||||
}
|
||||
|
||||
return [coercedValue];
|
||||
}
|
||||
|
||||
if (isInputObjectType(type)) {
|
||||
if (valueNode.kind !== Kind.OBJECT) {
|
||||
return; // Invalid: intentionally return no value.
|
||||
}
|
||||
|
||||
var coercedObj = Object.create(null);
|
||||
var fieldNodes = keyMap(valueNode.fields, function (field) {
|
||||
return field.name.value;
|
||||
});
|
||||
var _iteratorNormalCompletion2 = true;
|
||||
var _didIteratorError2 = false;
|
||||
var _iteratorError2 = undefined;
|
||||
|
||||
try {
|
||||
for (var _iterator2 = objectValues(type.getFields())[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
|
||||
var field = _step2.value;
|
||||
var fieldNode = fieldNodes[field.name];
|
||||
|
||||
if (!fieldNode || isMissingVariable(fieldNode.value, variables)) {
|
||||
if (field.defaultValue !== undefined) {
|
||||
coercedObj[field.name] = field.defaultValue;
|
||||
} else if (isNonNullType(field.type)) {
|
||||
return; // Invalid: intentionally return no value.
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
var fieldValue = valueFromAST(fieldNode.value, field.type, variables);
|
||||
|
||||
if (isInvalid(fieldValue)) {
|
||||
return; // Invalid: intentionally return no value.
|
||||
}
|
||||
|
||||
coercedObj[field.name] = fieldValue;
|
||||
}
|
||||
} catch (err) {
|
||||
_didIteratorError2 = true;
|
||||
_iteratorError2 = err;
|
||||
} finally {
|
||||
try {
|
||||
if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
|
||||
_iterator2.return();
|
||||
}
|
||||
} finally {
|
||||
if (_didIteratorError2) {
|
||||
throw _iteratorError2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return coercedObj;
|
||||
}
|
||||
|
||||
if (isEnumType(type)) {
|
||||
if (valueNode.kind !== Kind.ENUM) {
|
||||
return; // Invalid: intentionally return no value.
|
||||
}
|
||||
|
||||
var enumValue = type.getValue(valueNode.value);
|
||||
|
||||
if (!enumValue) {
|
||||
return; // Invalid: intentionally return no value.
|
||||
}
|
||||
|
||||
return enumValue.value;
|
||||
}
|
||||
|
||||
if (isScalarType(type)) {
|
||||
// Scalars fulfill parsing a literal value via parseLiteral().
|
||||
// Invalid values represent a failure to parse correctly, in which case
|
||||
// no value is returned.
|
||||
var result;
|
||||
|
||||
try {
|
||||
result = type.parseLiteral(valueNode, variables);
|
||||
} catch (_error) {
|
||||
return; // Invalid: intentionally return no value.
|
||||
}
|
||||
|
||||
if (isInvalid(result)) {
|
||||
return; // Invalid: intentionally return no value.
|
||||
}
|
||||
|
||||
return result;
|
||||
} // Not reachable. All possible input types have been considered.
|
||||
|
||||
/* istanbul ignore next */
|
||||
|
||||
|
||||
throw new Error("Unexpected input type: \"".concat(inspect(type), "\"."));
|
||||
} // Returns true if the provided valueNode is a variable which is not defined
|
||||
// in the set of variables.
|
||||
|
||||
function isMissingVariable(valueNode, variables) {
|
||||
return valueNode.kind === Kind.VARIABLE && (!variables || isInvalid(variables[valueNode.name.value]));
|
||||
}
|
||||
Reference in New Issue
Block a user