{"record":{"id":"c77c6ca48eac690b","repo":"marmelab/react-admin","slug":"empty-sparse-fields-specify-at-least-one-field-or","errorCode":null,"errorMessage":"Empty sparse fields. Specify at least one field or remove the 'sparseFields' param","messagePattern":"Empty sparse fields\\. Specify at least one field or remove the 'sparseFields' param","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/ra-data-graphql-simple/src/buildGqlQuery.ts","lineNumber":40,"sourceCode":"} from 'graphql';\nimport * as gqlTypes from 'graphql-ast-types-browser';\n\nimport getFinalType from './getFinalType';\nimport { getGqlType } from './getGqlType';\n\ntype SparseField = string | { [k: string]: SparseField[] };\ntype ExpandedSparseField = { linkedType?: string; fields: SparseField[] };\ntype ProcessedFields = {\n    resourceFields: IntrospectionField[];\n    linkedSparseFields: ExpandedSparseField[];\n};\n\nfunction processSparseFields(\n    resourceFields: readonly IntrospectionField[],\n    sparseFields: SparseField[]\n): ProcessedFields & { resourceFields: readonly IntrospectionField[] } {\n    if (!sparseFields || sparseFields.length === 0)\n        throw new Error(\n            \"Empty sparse fields. Specify at least one field or remove the 'sparseFields' param\"\n        );\n\n    const permittedSparseFields: ProcessedFields = sparseFields.reduce(\n        (permitted: ProcessedFields, sparseField: SparseField) => {\n            let expandedSparseField: ExpandedSparseField;\n            if (typeof sparseField == 'string')\n                expandedSparseField = { fields: [sparseField] };\n            else {\n                const [linkedType, linkedSparseFields] =\n                    Object.entries(sparseField)[0];\n                expandedSparseField = {\n                    linkedType,\n                    fields: linkedSparseFields,\n                };\n            }\n\n            const availableField = resourceFields.find(","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/marmelab/react-admin/blob/051f511bb0afb5ea565c2d3728bf4dab0a6fa5e0/packages/ra-data-graphql-simple/src/buildGqlQuery.ts#L22-L58","documentation":"In ra-data-graphql-simple, the sparseFields option restricts a GraphQL query to a subset of fields. processSparseFields validates the supplied list up front and throws if it is empty or falsy, because an empty selection set would produce an invalid or meaningless query.","triggerScenarios":"Passing sparseFields: [] or sparseFields: undefined/null to a buildQuery factory (or data provider options) while explicitly opting into sparse field selection.","commonSituations":"Building the fields array conditionally from user config or feature flags and ending up with an empty array; JSON config files with an empty 'sparseFields' list.","solutions":["Provide at least one field in the sparseFields array.","Omit the sparseFields param entirely when you want all fields.","Guard before calling: only pass sparseFields when the array has entries."],"exampleFix":"// before\nbuildQuery('GET_LIST', 'Post', { sparseFields: [] });\n// after\nbuildQuery('GET_LIST', 'Post', { sparseFields: ['id', 'title'] }); // or omit sparseFields","handlingStrategy":"validation","validationCode":"const options = sparseFields?.length ? { sparseFields } : {};\nbuildQuery(fetchType, resource, { ...params, ...options });","typeGuard":"const hasSparseFields = (f: unknown): f is string[] =>\n  Array.isArray(f) && f.length > 0;","tryCatchPattern":"try {\n  return await dataProvider.getList(resource, params);\n} catch (e) {\n  if (e.message.includes('Empty sparse fields')) {\n    return await dataProvider.getList(resource, { ...params, sparseFields: undefined });\n  }\n  throw e;\n}","preventionTips":["Only set sparseFields in config when the array is non-empty.","Centralize provider options construction so empty arrays are stripped.","Validate config files at startup."],"tags":["graphql","validation","config","ra-data-graphql-simple"],"backgroundTag":"empty-required-param","analyzedSha":"051f511bb0afb5ea565c2d3728bf4dab0a6fa5e0","analyzedAt":"2026-08-30T02:28:14.926Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}