dgraph icon indicating copy to clipboard operation
dgraph copied to clipboard

Predicate name support in GraphQL variables

Open llonchj opened this issue 6 years ago • 5 comments

Given I have a node:

<_:1> <type:emailAccount> "" .
<_:1> <name> "[email protected]" .

I want to be able to retrieve the uid given the name and has(<type:emailAccount>).

I am trying to build the query using QueryWithVars and the golang api.

The query works with a hardcoded $type variable.

        variables := map[string]string{
    		"$name": Name,
    		// "$type": Type,
    	}
    
    	q := `
    	query uid($name: string)
    	{
    		uid(func:eq(name, $name)) @filter(has(<type:emailAccount>)) {
    		  uid
    		}
    	  }
    	`
    	resp, err := txn.QueryWithVars(ctx, q, variables)
    	if err != nil {
    		return "", err
    	}

If i try to use $type as variable, returns the error: rpc error: code = Unknown desc = Got empty attr for function: [has].

    	q := `
    	query uid($name: string)
    	{
    		uid(func:eq(name, $name)) @filter(has($type)) {
    		  uid
    		}
    	  }
    	`

What should be the query in order to work using $type to call has($type)?

Thanks

llonchj avatar Jan 17 '18 05:01 llonchj

Wow, this is an old issue and doesn't seem like it should have been left open for so long. Sorry about that, @llonchj! I'll add to the possible list of improvements to the query language but I can't promise it will make it into our roadmap any time soon.

Thanks!

campoy avatar May 31 '19 19:05 campoy

Cap problema @campoy! Thank you!

llonchj avatar May 31 '19 19:05 llonchj

I have moved this issue to Dgraph as this is a bug in Dgraph's query parser. So, it is not related to Dgo.

Reproducible with

query uid($name: string="[email protected]", $type: string="<type:emailAccount>"){
  uid(func:eq(name, $name)) @filter(has($type)) {
    uid
    name
  }
}

MichelDiz avatar Mar 25 '20 06:03 MichelDiz

Github issues have been deprecated. This issue has been moved to discuss. You can follow the conversation there and also subscribe to updates by changing your notification preferences.

drawing

minhaj-shakeel avatar Jul 20 '20 19:07 minhaj-shakeel

+1 for this feature.

My usecase is reported in https://discuss.dgraph.io/t/passing-sorting-criteria-to-custom-dql/17777/3

In short, I'd like to pass custom filtering or sorting criteria to a DQL query.

POST http://localhost:8080/query
Content-Type: application/dql

query q($p : string = "Person.firstName", $terms: string = "Harry", $orderby: string = "Person.lastName") {
      queryPersonCustom(func: anyofterms($p, $terms), orderasc: $orderby) {
        id : uid
        firstName : Person.firstName
        lastName : Person.lastName
      }
}

xpomul avatar Sep 15 '22 07:09 xpomul