{"record":{"id":"110b980721f2842a","repo":"facebook/relay","slug":"when-assigning-an-array-of-items-none-of-the-item","errorCode":null,"errorMessage":"When assigning an array of items, none of the items should be null or undefined.","messagePattern":"When assigning an array of items, none of the items should be null or undefined\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/relay-runtime/mutations/createUpdatableProxy.js","lineNumber":217,"sourceCode":"  }\n}\n\nfunction createSetterForPluralLinkedField(\n  selection: ReaderLinkedField,\n  variables: Variables,\n  updatableProxyRootRecord: RecordProxy,\n  recordSourceProxy: RecordSourceProxy,\n) {\n  return function set(newValue: ReadonlyArray<{__id: string, ...}>) {\n    const newVariables = getArgumentValues(selection.args ?? [], variables);\n    if (newValue == null) {\n      throw new Error(\n        'Do not assign null to plural linked fields; assign an empty array instead.',\n      );\n    } else {\n      const recordProxies = newValue.map((item): ?RecordProxy => {\n        if (item == null) {\n          throw new Error(\n            'When assigning an array of items, none of the items should be null or undefined.',\n          );\n        }\n        const {__id} = item;\n        if (__id == null) {\n          throw new Error(\n            'The __id field must be present on each item passed to the setter. This indicates a bug in Relay.',\n          );\n        }\n        const newValueRecord = recordSourceProxy.get(__id);\n        if (newValueRecord == null) {\n          throw new Error(\n            `Did not find item with data id ${__id} in the store.`,\n          );\n        }\n        return newValueRecord;\n      });\n      updatableProxyRootRecord.setLinkedRecords(","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/facebook/relay/blob/668b1b85e06261aa3b58dabfc51f8b5524a70955/packages/relay-runtime/mutations/createUpdatableProxy.js#L199-L235","documentation":"Thrown when an array assigned to a plural linked field through an updatable proxy contains null or undefined items. Relay cannot link a null entry in the store for a plural linked field, so every element of the assigned array must be a valid record object.","triggerScenarios":"Calling `pluralField.set(items)` where `items` is an array that includes null or undefined elements, e.g. `set([a, null, c])` or an array built from a sparse or nullable-mapped source.","commonSituations":"Mapping over server data that contains null list items (GraphQL lists are nullable by default); spreading arrays with holes; filtering logic that leaves undefined entries.","solutions":["Filter out null/undefined before assigning: `field.set(items.filter(Boolean))`","Use `items.filter(item => item != null)` to be explicit about both null and undefined","Fix the server/fragment so list items are non-null (e.g. `[Type!]!]`) if nulls should never appear"],"exampleFix":"// before\nupdatable.viewer.friends.set(friends);\n// after\nupdatable.viewer.friends.set(friends.filter(f => f != null));","handlingStrategy":"validation","validationCode":"function assertNoNullItems(items) {\n  if (items.some(item => item == null)) {\n    throw new TypeError('Array assigned to plural linked field contains null/undefined items');\n  }\n}","typeGuard":"function hasNoNullItems<T>(items: ReadonlyArray<T | null | undefined>): items is ReadonlyArray<T> {\n  return items.every((item): item is T => item != null);\n}","tryCatchPattern":"try {\n  field.set(items);\n} catch (e) {\n  if (e.message.includes('none of the items should be null')) field.set(items.filter(i => i != null));\n  else throw e;\n}","preventionTips":["Filter nulls from GraphQL list results (lists are nullable by default): items.filter(i => i != null)","Request non-null list elements in the schema ([Type!]!]) when possible","Avoid sparse arrays and unfiltered .map results","Validate arrays at the boundary before passing into updaters"],"tags":["relay","updater","graphql","linked-fields"],"backgroundTag":"invalid-null-assignment","analyzedSha":"668b1b85e06261aa3b58dabfc51f8b5524a70955","analyzedAt":"2026-09-02T19:57:20.783Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}