@@ -1344,7 +1344,7 @@ func (s *Server) doQuery(ctx context.Context, req *Request) (resp *api.Response,
13441344 graphql : isGraphQL ,
13451345 gqlField : req .gqlField ,
13461346 }
1347- if rerr = parseRequest (qc ); rerr != nil {
1347+ if rerr = parseRequest (ctx , qc ); rerr != nil {
13481348 return
13491349 }
13501350
@@ -1565,7 +1565,7 @@ func processQuery(ctx context.Context, qc *queryContext) (*api.Response, error)
15651565}
15661566
15671567// parseRequest parses the incoming request
1568- func parseRequest (qc * queryContext ) error {
1568+ func parseRequest (ctx context. Context , qc * queryContext ) error {
15691569 start := time .Now ()
15701570 defer func () {
15711571 qc .latency .Parsing = time .Since (start )
@@ -1585,7 +1585,7 @@ func parseRequest(qc *queryContext) error {
15851585 qc .gmuList = append (qc .gmuList , gmu )
15861586 }
15871587
1588- if err := addQueryIfUnique (qc ); err != nil {
1588+ if err := addQueryIfUnique (ctx , qc ); err != nil {
15891589 return err
15901590 }
15911591
@@ -1698,19 +1698,38 @@ func verifyUnique(qc *queryContext, qr query.Request) error {
16981698}
16991699
17001700// addQueryIfUnique adds dummy queries in the request for checking whether predicate is unique in the db
1701- func addQueryIfUnique (qc * queryContext ) error {
1701+ func addQueryIfUnique (qctx context. Context , qc * queryContext ) error {
17021702 if len (qc .gmuList ) == 0 {
17031703 return nil
17041704 }
17051705
1706- ctx := context .WithValue (context .Background (), schema .IsWrite , false )
1706+ ctx := context .WithValue (qctx , schema .IsWrite , false )
1707+ namespace , err := x .ExtractNamespace (ctx )
1708+ if err != nil {
1709+ // It's okay to ignore this here. If namespace is not set, it could mean either there is no
1710+ // authorization or it's trying to be bypassed. So the namespace is either 0 or the mutation would fail.
1711+ glog .Errorf ("Error while extracting namespace, assuming default %s" , err )
1712+ namespace = 0
1713+ }
1714+ isGalaxyQuery := x .IsGalaxyOperation (ctx )
1715+
17071716 qc .uniqueVars = map [uint64 ]uniquePredMeta {}
17081717 var buildQuery strings.Builder
17091718 for gmuIndex , gmu := range qc .gmuList {
17101719 for rdfIndex , pred := range gmu .Set {
1711- predSchema , _ := schema .State ().Get (ctx , x .NamespaceAttr (pred .Namespace , pred .Predicate ))
1712- if ! predSchema .Unique {
1713- continue
1720+ if isGalaxyQuery {
1721+ // The caller should make sure that the directed edges contain the namespace we want
1722+ // to insert into.
1723+ namespace = pred .Namespace
1724+ }
1725+ if pred .Predicate != "dgraph.xid" {
1726+ // [TODO] Don't check if it's dgraph.xid. It's a bug as this node might not be aware
1727+ // of the schema for the given predicate. This is a bug issue for dgraph.xid hence
1728+ // we are bypassing it manually until the bug is fixed.
1729+ predSchema , ok := schema .State ().Get (ctx , x .NamespaceAttr (namespace , pred .Predicate ))
1730+ if ! ok || ! predSchema .Unique {
1731+ continue
1732+ }
17141733 }
17151734 var predicateName string
17161735 if pred .Lang != "" {
0 commit comments