123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- import { forEach, isCollection } from 'iterall';
- import objectValues from '../polyfills/objectValues';
- import inspect from '../jsutils/inspect';
- import invariant from '../jsutils/invariant';
- import didYouMean from '../jsutils/didYouMean';
- import isObjectLike from '../jsutils/isObjectLike';
- import suggestionList from '../jsutils/suggestionList';
- import printPathArray from '../jsutils/printPathArray';
- import { type Path, addPath, pathToArray } from '../jsutils/Path';
- import { GraphQLError } from '../error/GraphQLError';
- import {
- type GraphQLInputType,
- isScalarType,
- isEnumType,
- isInputObjectType,
- isListType,
- isNonNullType,
- } from '../type/definition';
- type OnErrorCB = (
- path: $ReadOnlyArray<string | number>,
- invalidValue: mixed,
- error: GraphQLError,
- ) => void;
- export function coerceInputValue(
- inputValue: mixed,
- type: GraphQLInputType,
- onError?: OnErrorCB = defaultOnError,
- ): mixed {
- return coerceInputValueImpl(inputValue, type, onError);
- }
- function defaultOnError(
- path: $ReadOnlyArray<string | number>,
- invalidValue: mixed,
- error: GraphQLError,
- ) {
- let errorPrefix = 'Invalid value ' + inspect(invalidValue);
- if (path.length > 0) {
- errorPrefix += ` at "value${printPathArray(path)}": `;
- }
- error.message = errorPrefix + ': ' + error.message;
- throw error;
- }
- function coerceInputValueImpl(
- inputValue: mixed,
- type: GraphQLInputType,
- onError: OnErrorCB,
- path: Path | void,
- ): mixed {
- if (isNonNullType(type)) {
- if (inputValue != null) {
- return coerceInputValueImpl(inputValue, type.ofType, onError, path);
- }
- onError(
- pathToArray(path),
- inputValue,
- new GraphQLError(
- `Expected non-nullable type ${inspect(type)} not to be null.`,
- ),
- );
- return;
- }
- if (inputValue == null) {
-
- return null;
- }
- if (isListType(type)) {
- const itemType = type.ofType;
- if (isCollection(inputValue)) {
- const coercedValue = [];
- forEach((inputValue: any), (itemValue, index) => {
- coercedValue.push(
- coerceInputValueImpl(
- itemValue,
- itemType,
- onError,
- addPath(path, index),
- ),
- );
- });
- return coercedValue;
- }
-
- return [coerceInputValueImpl(inputValue, itemType, onError, path)];
- }
- if (isInputObjectType(type)) {
- if (!isObjectLike(inputValue)) {
- onError(
- pathToArray(path),
- inputValue,
- new GraphQLError(`Expected type ${type.name} to be an object.`),
- );
- return;
- }
- const coercedValue = {};
- const fieldDefs = type.getFields();
- for (const field of objectValues(fieldDefs)) {
- const fieldValue = inputValue[field.name];
- if (fieldValue === undefined) {
- if (field.defaultValue !== undefined) {
- coercedValue[field.name] = field.defaultValue;
- } else if (isNonNullType(field.type)) {
- const typeStr = inspect(field.type);
- onError(
- pathToArray(path),
- inputValue,
- new GraphQLError(
- `Field ${field.name} of required type ${typeStr} was not provided.`,
- ),
- );
- }
- continue;
- }
- coercedValue[field.name] = coerceInputValueImpl(
- fieldValue,
- field.type,
- onError,
- addPath(path, field.name),
- );
- }
-
- for (const fieldName of Object.keys(inputValue)) {
- if (!fieldDefs[fieldName]) {
- const suggestions = suggestionList(
- fieldName,
- Object.keys(type.getFields()),
- );
- onError(
- pathToArray(path),
- inputValue,
- new GraphQLError(
- `Field "${fieldName}" is not defined by type ${type.name}.` +
- didYouMean(suggestions),
- ),
- );
- }
- }
- return coercedValue;
- }
- if (isScalarType(type)) {
- let parseResult;
-
-
-
- try {
- parseResult = type.parseValue(inputValue);
- } catch (error) {
- onError(
- pathToArray(path),
- inputValue,
- new GraphQLError(
- `Expected type ${type.name}. ` + error.message,
- undefined,
- undefined,
- undefined,
- undefined,
- error,
- ),
- );
- return;
- }
- if (parseResult === undefined) {
- onError(
- pathToArray(path),
- inputValue,
- new GraphQLError(`Expected type ${type.name}.`),
- );
- }
- return parseResult;
- }
- if (isEnumType(type)) {
- if (typeof inputValue === 'string') {
- const enumValue = type.getValue(inputValue);
- if (enumValue) {
- return enumValue.value;
- }
- }
- const suggestions = suggestionList(
- String(inputValue),
- type.getValues().map(enumValue => enumValue.name),
- );
- onError(
- pathToArray(path),
- inputValue,
- new GraphQLError(`Expected type ${type.name}.` + didYouMean(suggestions)),
- );
- return;
- }
-
- invariant(false, 'Unexpected input type: ' + inspect((type: empty)));
- }
|