{"record":{"id":"689a038f1652c89b","repo":"dgraph-io/dgraph","slug":"value-of-from-var-s-should-have-already-been-pop","errorCode":null,"errorMessage":"value of from var(%s) should have already been populated","messagePattern":"value of from var\\((.+?)\\) should have already been populated","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"query/query.go","lineNumber":1776,"sourceCode":"\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\treturn nil\n}\n\n// fillShortestPathVars reads value of the uid variable from mp map and fills it into From and To\n// parameters.\nfunc (sg *SubGraph) fillShortestPathVars(mp map[string]varValue) error {\n\t// The uidVar.Uids can be nil or have an empty uid list if the variable didn't\n\t// return any uids. This would mean sg.Params.From or sg.Params.To is 0 and the\n\t// query would return an empty result.\n\n\tif sg.Params.ShortestPathArgs.From != nil && len(sg.Params.ShortestPathArgs.From.NeedsVar) > 0 {\n\t\tfromVar := sg.Params.ShortestPathArgs.From.NeedsVar[0].Name\n\t\tuidVar, ok := mp[fromVar]\n\t\tif !ok {\n\t\t\treturn errors.Errorf(\"value of from var(%s) should have already been populated\",\n\t\t\t\tfromVar)\n\t\t}\n\t\tif uidVar.Uids != nil && len(uidVar.Uids.Uids) > 0 {\n\t\t\tif len(uidVar.Uids.Uids) > 1 {\n\t\t\t\treturn errors.Errorf(\"from variable(%s) should only expand to 1 uid\", fromVar)\n\t\t\t}\n\t\t\tsg.Params.From = uidVar.Uids.Uids[0]\n\t\t}\n\t}\n\n\tif sg.Params.ShortestPathArgs.To != nil && len(sg.Params.ShortestPathArgs.To.NeedsVar) > 0 {\n\t\ttoVar := sg.Params.ShortestPathArgs.To.NeedsVar[0].Name\n\t\tuidVar, ok := mp[toVar]\n\t\tif !ok {\n\t\t\treturn errors.Errorf(\"value of to var(%s) should have already been populated\",\n\t\t\t\ttoVar)\n\t\t}\n\t\tif uidVar.Uids != nil && len(uidVar.Uids.Uids) > 0 {","sourceCodeStart":1758,"sourceCodeEnd":1794,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/query/query.go#L1758-L1794","documentation":"For shortest-path queries, the 'from' endpoint may be supplied via a variable. Dgraph expects that variable's value to already be computed and present in the map of done variables before the shortest-path subgraph runs. If the named variable is absent from mp, this error is thrown, meaning the from-variable was never populated upstream.","triggerScenarios":"Running shortest(from: var(f), to: ...) where f was never defined with 'as' in the query, or defined in a branch that returned no results / was not executed before the shortest-path block.","commonSituations":"Typo in variable name in shortest(); variable defined after the shortest-path query in the same block; filter on the variable-emitting node excluded all nodes so the var was never materialized.","solutions":["Define the from variable with 'as' before/alongside the shortest-path query in the same block","Check variable name spelling between emitter and shortest(from: var(...))","Ensure the emitting query clause actually matches at least one node","Verify variable ordering — the shortest-path args depend on vars available at that point"],"exampleFix":"// before: undefined var\n{\n  p(func: uid(0x1)) {\n    shortest(from: var(start), to: 0x2) { uid }\n  }\n}\n// after\n{\n  start as var(func: eq(name, \"alice\"))\n  p(func: uid(0x1)) {\n    shortest(from: var(start), to: 0x2) { uid }\n  }\n}","handlingStrategy":"validation","validationCode":"// Ensure every var referenced in shortest(from:...) is defined earlier in the query\nfor _, v := range referencedVars(query, \"shortest\", \"from\") {\n    if !definedBefore(query, v) {\n        return fmt.Errorf(\"from-variable %s must be defined before shortest()\", v)\n    }\n}","typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"value of from var\") {\n    return fmt.Errorf(\"from-variable missing or empty; check its definition: %w\", err)\n}","preventionTips":["Define from/to variables with 'as' before the shortest-path clause","Test variable-emitting clauses in isolation to confirm they match nodes","Use consistent variable naming conventions to avoid typos"],"tags":["dgraph","query","shortest-path","variables"],"backgroundTag":"shortest-path-variable-not-populated","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}