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

  1. Define the from variable with 'as' before/alongside the shortest-path query in the same block
  2. Check variable name spelling between emitter and shortest(from: var(...))
  3. Ensure the emitting query clause actually matches at least one node
  4. 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

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


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/689a038f1652c89b. Report an issue: GitHub.