{"record":{"id":"754fc62c18dad6f4","repo":"dgraph-io/dgraph","slug":"encountered-an-empty-value-for-id-field-s","errorCode":null,"errorMessage":"encountered an empty value for @id field `%s`","messagePattern":"encountered an empty value for @id field `(.+?)`","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"graphql/resolve/mutation_rewriter.go","lineNumber":2006,"sourceCode":"\t\t\t\t\t\t\tctx, typ, fieldDef, varGen, object, xidMetadata, i)\n\t\t\t\t\t} else {\n\t\t\t\t\t\tfieldQueries, fieldTypes, err = existenceQueries(\n\t\t\t\t\t\t\tctx, fieldDef.Type(), fieldDef, varGen, object, xidMetadata)\n\t\t\t\t\t}\n\t\t\t\t\tretErrors = append(retErrors, err...)\n\t\t\t\t\tret = append(ret, fieldQueries...)\n\t\t\t\t\tretTypes = append(retTypes, fieldTypes...)\n\t\t\t\tdefault:\n\t\t\t\t\t// This is a scalar list. So, it won't contain any XID.\n\t\t\t\t\t// Don't do anything.\n\t\t\t\t}\n\n\t\t\t}\n\t\tdefault:\n\t\t\t// This field is either a scalar value or a null.\n\t\t\t// Fields with ID directive cannot have empty values. Checking it here.\n\t\t\tif fieldDef.HasIDDirective() && val == \"\" {\n\t\t\t\terr := fmt.Errorf(\"encountered an empty value for @id field `%s`\", fieldName)\n\t\t\t\tretErrors = append(retErrors, err)\n\t\t\t\treturn nil, nil, retErrors\n\t\t\t}\n\t\t}\n\t}\n\n\treturn ret, retTypes, retErrors\n}\n\nfunc existenceQueriesUnion(\n\tctx context.Context,\n\tparentTyp schema.Type,\n\tsrcField schema.FieldDefinition,\n\tvarGen *VariableGenerator,\n\tobj map[string]interface{},\n\txidMetadata *xidMetadata,\n\tlistIndex int) ([]*dql.GraphQuery, []string, []error) {\n","sourceCodeStart":1988,"sourceCodeEnd":2024,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/graphql/resolve/mutation_rewriter.go#L1988-L2024","documentation":"A field decorated with @id in the GraphQL schema received an empty string value in a mutation while being processed as a scalar or null value. @id fields uniquely identify nodes externally, so Dgraph forbids empty strings for them. The error names the offending field.","triggerScenarios":"Sending an add/update mutation where an @id field (e.g. `email`, `username`) is explicitly set to \"\" and the value reaches the default (scalar/null) branch of the value rewriting loop.","commonSituations":"HTML/JSON forms submitting empty strings, ORM or mapper layers converting missing values to \"\", or spreadsheets/CSV bulk imports with blank cells mapped to @id columns.","solutions":["Provide a real value for the @id field, or remove the field from the mutation input entirely.","Sanitize payloads client-side: convert \"\" to null or drop the key before sending.","If the field is genuinely optional, remove the @id directive from the schema.","For bulk imports, pre-process rows to skip or fill blank @id cells."],"exampleFix":"// before\naddUser(input: { email: \"\", name: \"Bob\" })\n// after\naddUser(input: { email: \"bob@example.com\", name: \"Bob\" })","handlingStrategy":"validation","validationCode":"const idFields = [\"email\", \"username\"];\nfunction sanitizeInput(obj) {\n  for (const k of idFields) {\n    if (obj[k] === \"\") delete obj[k];\n  }\n  return obj;\n}","typeGuard":"function isValidIdValue(v) {\n  return v === undefined || v === null || (typeof v === 'string' && v.trim() !== '');\n}","tryCatchPattern":"try {\n  await client.mutate({ mutation: ADD_USER, variables: { input } });\n} catch (e) {\n  if (/empty value for @id field/.test(e.message)) {\n    const f = e.message.match(/@id field `(\\w+)`/)[1];\n    delete input[f]; // resend without the empty @id\n  } else throw e;\n}","preventionTips":["Drop or null out empty strings for @id fields before mutating.","Treat blank CSV/spreadsheet cells as missing values in importers.","Validate @id fields against a non-empty-string rule in forms.","Keep the schema's @id directive only on truly required fields."],"tags":["graphql","id-directive","empty-value"],"backgroundTag":"empty-required-field","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}