From d1c6732df13b3bbd5922bac8a6f89473bb200fa2 Mon Sep 17 00:00:00 2001
From: Manish R Jain <manish@dgraph.io>
Date: Wed, 22 Jun 2016 16:19:20 +1000
Subject: [PATCH] Fix a bug which causes us to return no results in JSON

---
 query/query.go      | 20 ++++++++++++--------
 query/query_test.go |  7 +++++--
 2 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/query/query.go b/query/query.go
index f1b0d970..d1470131 100644
--- a/query/query.go
+++ b/query/query.go
@@ -247,15 +247,19 @@ func (g *SubGraph) ToJson(l *Latency) (js []byte, rerr error) {
 		log.Fatal("We don't currently support more than 1 uid at root.")
 	}
 
-	ival := r[0]
-	var m map[string]interface{}
-	if ival != nil {
-		m = ival.(map[string]interface{})
-	} else {
-		m = make(map[string]interface{})
+	// r is a map, and we don't know it's key. So iterate over it, even though it only has 1 result.
+	for _, ival := range r {
+		var m map[string]interface{}
+		if ival != nil {
+			m = ival.(map[string]interface{})
+		} else {
+			m = make(map[string]interface{})
+		}
+		m["server_latency"] = l.ToMap()
+		return json.Marshal(m)
 	}
-	m["server_latency"] = l.ToMap()
-	return json.Marshal(m)
+	log.Fatal("Runtime should never reach here.")
+	return []byte(""), fmt.Errorf("Runtime should never reach here.")
 }
 
 // This function performs a binary search on the uids slice and returns the
diff --git a/query/query_test.go b/query/query_test.go
index 2e3474d1..23b12604 100644
--- a/query/query_test.go
+++ b/query/query_test.go
@@ -19,9 +19,9 @@ package query
 import (
 	"bytes"
 	"encoding/gob"
-	"fmt"
 	"io/ioutil"
 	"os"
+	"strings"
 	"testing"
 	"time"
 
@@ -324,7 +324,10 @@ func TestToJson(t *testing.T) {
 	if err != nil {
 		t.Error(err)
 	}
-	fmt.Printf(string(js))
+	s := string(js)
+	if !strings.Contains(s, "Michonne") {
+		t.Errorf("Unable to find Michonne in this result: %v", s)
+	}
 }
 
 func getProperty(properties []*graph.Property, prop string) []byte {
-- 
GitLab