dgraph-io/dgraph · error
value of from var(%s) should have already been populated
Error message
value of from var(%s) should have already been populated
What it means
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.
Source
Thrown at query/query.go:1776
if err != nil {
return err
}
}
return nil
}
// fillShortestPathVars reads value of the uid variable from mp map and fills it into From and To
// parameters.
func (sg *SubGraph) fillShortestPathVars(mp map[string]varValue) error {
// The uidVar.Uids can be nil or have an empty uid list if the variable didn't
// return any uids. This would mean sg.Params.From or sg.Params.To is 0 and the
// query would return an empty result.
if sg.Params.ShortestPathArgs.From != nil && len(sg.Params.ShortestPathArgs.From.NeedsVar) > 0 {
fromVar := sg.Params.ShortestPathArgs.From.NeedsVar[0].Name
uidVar, ok := mp[fromVar]
if !ok {
return errors.Errorf("value of from var(%s) should have already been populated",
fromVar)
}
if uidVar.Uids != nil && len(uidVar.Uids.Uids) > 0 {
if len(uidVar.Uids.Uids) > 1 {
return errors.Errorf("from variable(%s) should only expand to 1 uid", fromVar)
}
sg.Params.From = uidVar.Uids.Uids[0]
}
}
if sg.Params.ShortestPathArgs.To != nil && len(sg.Params.ShortestPathArgs.To.NeedsVar) > 0 {
toVar := sg.Params.ShortestPathArgs.To.NeedsVar[0].Name
uidVar, ok := mp[toVar]
if !ok {
return errors.Errorf("value of to var(%s) should have already been populated",
toVar)
}
if uidVar.Uids != nil && len(uidVar.Uids.Uids) > 0 {View on GitHub (pinned to 759e242be6)
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
Example fix
// before: undefined var
{
p(func: uid(0x1)) {
shortest(from: var(start), to: 0x2) { uid }
}
}
// after
{
start as var(func: eq(name, "alice"))
p(func: uid(0x1)) {
shortest(from: var(start), to: 0x2) { uid }
}
} Defensive patterns
Strategy: validation
Validate before calling
// Ensure every var referenced in shortest(from:...) is defined earlier in the query
for _, v := range referencedVars(query, "shortest", "from") {
if !definedBefore(query, v) {
return fmt.Errorf("from-variable %s must be defined before shortest()", v)
}
} Try / catch
if err != nil && strings.Contains(err.Error(), "value of from var") {
return fmt.Errorf("from-variable missing or empty; check its definition: %w", err)
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- from variable(%s) should only expand to 1 uid
- value of to var(%s) should have already been populated
- to variable(%s) should only expand to 1 uid
- from/to can't be nil for shortest path
- Invalid variable aggregation. Check the levels.
AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01).
Data as JSON: /api/errors/689a038f1652c89b.
Report an issue: GitHub.