UNPKG

graphql-paper

Version:

A flexible in-memory store based on a GraphQL Schema

48 lines (46 loc) 1.93 kB
'use strict'; var graphql = require('graphql'); var extractListType = require('../../graphql/extract-list-type.js'); var nullDocument = require('../../document/null-document.js'); var fieldReturnTypeMismatch = require('../errors/field-return-type-mismatch.js'); const listFieldValidator = { skipConnectionValue: false, skipNullValue: true, validate({ field, fieldValue, fieldConnections }) { const listType = extractListType.extractListType(field.type); if (graphql.isListType(listType)) { if (fieldValue && !Array.isArray(fieldValue)) { throw new fieldReturnTypeMismatch.FieldReturnTypeMismatch({ field: field, expected: 'Array', actual: typeof fieldValue }); } const isNonNullList = !graphql.isNullableType(listType === null || listType === void 0 ? void 0 : listType.ofType); if (isNonNullList) { var _fieldConnections$fin; const nullishIndex = Array.isArray(fieldValue) ? fieldValue.findIndex(element => element == null) : -1; const nullishConnectionIndex = (_fieldConnections$fin = fieldConnections === null || fieldConnections === void 0 ? void 0 : fieldConnections.findIndex(key => key === nullDocument.key)) !== null && _fieldConnections$fin !== void 0 ? _fieldConnections$fin : -1; if (nullishIndex !== -1) { throw new fieldReturnTypeMismatch.FieldReturnTypeMismatch({ field: field, expected: 'non-null list', actual: `${fieldValue[nullishIndex]} in the array` }); } else if (nullishConnectionIndex !== -1) { throw new fieldReturnTypeMismatch.FieldReturnTypeMismatch({ field: field, expected: 'non-null list', actual: `connected null document` }); } } } } }; exports.listFieldValidator = listFieldValidator; //# sourceMappingURL=list-field.js.map