UNPKG

graphql-paper

Version:

A flexible in-memory store based on a GraphQL Schema

47 lines (46 loc) 1.83 kB
import { isListType, isNullableType } from 'graphql'; import { extractListType } from '../../graphql/extract-list-type.mjs'; import { key } from '../../document/null-document.mjs'; import { FieldReturnTypeMismatch } from '../errors/field-return-type-mismatch.mjs'; var listFieldValidator = { skipConnectionValue: false, skipNullValue: true, validate(_ref) { var { field, fieldValue, fieldConnections } = _ref; var listType = extractListType(field.type); if (isListType(listType)) { if (fieldValue && !Array.isArray(fieldValue)) { throw new FieldReturnTypeMismatch({ field: field, expected: 'Array', actual: typeof fieldValue }); } var isNonNullList = !isNullableType(listType === null || listType === void 0 ? void 0 : listType.ofType); if (isNonNullList) { var _fieldConnections$fin; var nullishIndex = Array.isArray(fieldValue) ? fieldValue.findIndex(element => element == null) : -1; var nullishConnectionIndex = (_fieldConnections$fin = fieldConnections === null || fieldConnections === void 0 ? void 0 : fieldConnections.findIndex(key$1 => key$1 === key)) !== null && _fieldConnections$fin !== void 0 ? _fieldConnections$fin : -1; if (nullishIndex !== -1) { throw new FieldReturnTypeMismatch({ field: field, expected: 'non-null list', actual: "".concat(fieldValue[nullishIndex], " in the array") }); } else if (nullishConnectionIndex !== -1) { throw new FieldReturnTypeMismatch({ field: field, expected: 'non-null list', actual: "connected null document" }); } } } } }; export { listFieldValidator }; //# sourceMappingURL=list-field.mjs.map