Got upsert error: rpc error: code = Unknown desc = while lexing query

Hi!

I’m using dgraph version 1.1.0 and trying to run the following test code

package main

import (
	"context"
	"fmt"
	"log"
	"strings"
	"time"

	dgo "github.com/dgraph-io/dgo/v2"
	"github.com/dgraph-io/dgo/v2/protos/api"
	"google.golang.org/grpc"
)

const (
	uidGetByName = `
						query {
							me(func: eq(name, "%NAME%")) {
    							uidValue as uid
							}
						}
    `
	mutationMsisdn = `
		  				uid(uidValue) <name> "%MSISDN%" .
		  				uid(uidValue) <msisdn> "%MSISDN%" .
		  				uid(uidValue) <dateAdd> "%DATETIME%" .
		  				uid(uidValue) <dgraph.type> "msisdn" .
	`
)

var (
	msisdns = []string{"1", "2", "3", "4", "5"}
)

func main() {

	upserts := []*api.Request{}

	for _, val := range msisdns {
		mu := &api.Mutation{SetNquads: []byte(strings.Replace(strings.Replace(uidGetByName, "%MSISDN%", val, -1), "%DATETIME%", time.Now().UTC().Format(time.RFC3339), -1))}
		upserts = append(
			upserts,
			&api.Request{
				Query:     strings.Replace(uidGetByName, "%NAME%", val, -1),
				Mutations: []*api.Mutation{mu},
			},
		)
	}

	ctx, toCancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer toCancel()

	conn, err := grpc.Dial("ip:port", grpc.WithInsecure())
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	dg := dgo.NewDgraphClient(api.NewDgraphClient(conn))

	txn := dg.NewTxn()
	defer txn.Discard(ctx)

	for _, curUpsert := range upserts {
		res, err := txn.Do(ctx, curUpsert)
		if err != nil {
			log.Fatal(err)
		}
		log.Println("mutationNodesCommon answer", res.String())
		fmt.Println()
	}

	txn.Commit(ctx)
}

but I end up getting the error:

rpc error: code = Unknown desc = while lexing query { at line 1 column 0: Invalid input: q at lexText

I’ve tried to remove “query” word and “{}” but it doesn’t help me.

In your code two things looks wrong:

	uidGetByName = `
						query {
							me(func: eq(name, "%NAME%")) {
    							uidValue as uid
							}
						}
    `

should be

	uidGetByName = `
						{
							me(func: eq(name, "%NAME%")) {
    							uidValue as uid
							}
						}
    `

But this change wont resolve the error.

Cause in the line this JSON query is used as a Nquad:

mu := &api.Mutation{SetNquads: []byte(strings.Replace(strings.Replace(uidGetByName, "%MSISDN%", val, -1), "%DATETIME%", time.Now().UTC().Format(time.RFC3339), -1))}

That is the cause of the error I think. uidGetByName is a JSON query not a Nquad tripple.

If you want this to do as a upsert in one query, you can define a upsert block in JSON as described here:
https://docs.dgraph.io/mutations/#upsert-block

I wonder if this is really needed in your case. It might be much simpler and easier to understand your code, if you handle the query and the mutation separate in two calls within a transaction.

Thank you for explanation! it was my own stupid error :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.