{"record":{"id":"aae6848a3cc7418f","repo":"dgraph-io/dgraph","slug":"duplicate-xid-found-s","errorCode":null,"errorMessage":"duplicate XID found: %s","messagePattern":"duplicate XID found: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"graphql/resolve/mutation_rewriter.go","lineNumber":1893,"sourceCode":"\t\t\t\t// There are two cases:\n\t\t\t\t// Case 1: We are at top level:\n\t\t\t\t// \t       We return an error if the same node is referenced twice at top level.\n\t\t\t\t// Case 2: We are not at top level:\n\t\t\t\t//         We don't return an error if one of the occurrences of XID is a reference\n\t\t\t\t//         and other is definition.\n\t\t\t\t//         We return an error if both occurrences contain values other than XID and are\n\t\t\t\t//         not equal.\n\t\t\t\tif xidMetadata.variableObjMap[variable] != nil {\n\t\t\t\t\t// if we already encountered an object with same xid earlier, and this object is\n\t\t\t\t\t// considered a duplicate of the existing object, then return error.\n\n\t\t\t\t\tif xidMetadata.isDuplicateXid(atTopLevel, variable, obj, srcField) {\n\t\t\t\t\t\t// TODO(Jatin): Add this error for inherited @id field with interface arg.\n\t\t\t\t\t\t//  Currently we don't return this error for the nested case when\n\t\t\t\t\t\t//  at both root and nested level we have same value of @id fields\n\t\t\t\t\t\t//  which have interface arg set and are inherited from same interface\n\t\t\t\t\t\t//  but are in different implementing type, we currently treat that as reference.\n\t\t\t\t\t\terr := errors.Errorf(\"duplicate XID found: %s\", xidString)\n\t\t\t\t\t\tretErrors = append(retErrors, err)\n\t\t\t\t\t\treturn nil, nil, retErrors\n\t\t\t\t\t}\n\t\t\t\t\t// In the other case it is not duplicate, we update variableObjMap in case the new\n\t\t\t\t\t// occurrence of XID is its description and the old occurrence was a reference.\n\t\t\t\t\t// Example:\n\t\t\t\t\t// obj = { \"id\": \"1\", \"name\": \"name1\"}\n\t\t\t\t\t// xidMetadata.variableObjMap[variable] = { \"id\": \"1\" }\n\t\t\t\t\t// In this case, as obj is the correct definition of the object, we update variableObjMap\n\t\t\t\t\toldObj := xidMetadata.variableObjMap[variable]\n\t\t\t\t\t// TODO(Jatin): This condition also needs to change in accordance with multiple xids.\n\t\t\t\t\t//  Also consider the case when @id fields can be nullable.\n\t\t\t\t\tif len(oldObj) == 1 && len(obj) > 1 {\n\t\t\t\t\t\t// Continue execution to perform dfs in this case. There may be more nodes\n\t\t\t\t\t\t// in the subtree of this node.\n\t\t\t\t\t\txidMetadata.variableObjMap[variable] = obj\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// This is just a node reference. No need to proceed further.","sourceCodeStart":1875,"sourceCodeEnd":1911,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/graphql/resolve/mutation_rewriter.go#L1875-L1911","documentation":"The same external ID (XID / @id field) value appears more than once within a single mutation request. Dgraph tracks XID occurrences while rewriting the mutation; a duplicate would create or reference the same logical node ambiguously, so it fails with this error. It notes the duplicated XID string.","triggerScenarios":"A single add/update mutation whose input contains two or more objects sharing the same @id field value (e.g. two posts referencing author { username: \"alice\" } where alice's description appears twice), detected by isDuplicateXid during rewriting.","commonSituations":"Bulk-import payloads with repeated entities, list inputs generated from loops that re-emit the same parent object, or batch upserts combining set and nested references of the same XID.","solutions":["Deduplicate the input: send the object with that XID only once and reference it elsewhere by ID/XID.","Split the mutation into multiple requests, each containing a single occurrence of the XID.","Fix payload generators so nested objects are emitted once and reused as references.","Check for inherited @id fields from interfaces causing the same value to appear in different implementing types."],"exampleFix":"// before\naddAuthor(input: [{ username: \"alice\", name: \"A\" }, { username: \"alice\", name: \"A2\" }])\n// after\naddAuthor(input: [{ username: \"alice\", name: \"A\" }]) // one occurrence only","handlingStrategy":"validation","validationCode":"function assertNoDuplicateXids(objects, xidField) {\n  const seen = new Set();\n  for (const o of objects) {\n    const key = o && o[xidField];\n    if (key != null && key !== '') {\n      if (seen.has(key)) throw new Error(`duplicate XID found: ${key}`);\n      seen.add(key);\n    }\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  await client.mutate({ mutation: ADD_AUTHOR, variables: { input: authors } });\n} catch (e) {\n  if (/duplicate XID found/.test(e.message)) {\n    const xid = e.message.match(/duplicate XID found: (.*)$/)[1];\n    // dedupe authors by xid and resend\n  } else throw e;\n}","preventionTips":["Deduplicate bulk payloads by @id field before sending.","Emit shared entities once and reference them by ID/XID in nested spots.","Add unit tests on payload generators to assert XID uniqueness.","Be careful with interface-inherited @id fields across implementing types."],"tags":["graphql","duplicate","external-id"],"backgroundTag":"duplicate-xid","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}