Query

Trait Query 

Source
pub trait Query: Send + Sync {
    // Required methods
    fn query<'life0, 'async_trait, Q>(
        &'life0 mut self,
        query: Q,
    ) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>
       where Q: Into<String> + Send + Sync + 'async_trait,
             Self: 'async_trait,
             'life0: 'async_trait;
    fn query_rdf<'life0, 'async_trait, Q>(
        &'life0 mut self,
        query: Q,
    ) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>
       where Q: Into<String> + Send + Sync + 'async_trait,
             Self: 'async_trait,
             'life0: 'async_trait;
    fn query_with_vars<'life0, 'async_trait, Q, K, V>(
        &'life0 mut self,
        query: Q,
        vars: HashMap<K, V>,
    ) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>
       where Q: Into<String> + Send + Sync + 'async_trait,
             K: Into<String> + Send + Sync + Eq + Hash + 'async_trait,
             V: Into<String> + Send + Sync + 'async_trait,
             Self: 'async_trait,
             'life0: 'async_trait;
    fn query_rdf_with_vars<'life0, 'async_trait, Q, K, V>(
        &'life0 mut self,
        query: Q,
        vars: HashMap<K, V>,
    ) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>
       where Q: Into<String> + Send + Sync + 'async_trait,
             K: Into<String> + Send + Sync + Eq + Hash + 'async_trait,
             V: Into<String> + Send + Sync + 'async_trait,
             Self: 'async_trait,
             'life0: 'async_trait;
}
Expand description

All Dgaph transaction types can performe a queries

Required Methods§

Source

fn query<'life0, 'async_trait, Q>( &'life0 mut self, query: Q, ) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>
where Q: Into<String> + Send + Sync + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

You can run a query by calling txn.query(q).

§Arguments
  • query: GraphQL+- query
§Errors

If transaction is not initialized properly, return EmptyTxn error.

gRPC errors can be returned also.

§Example
use std::collections::HashMap;
use dgraph_tonic::{Client, Response, Query};
use serde::Deserialize;
#[cfg(feature = "acl")]
use dgraph_tonic::{AclClientType, LazyChannel};

#[cfg(not(feature = "acl"))]
async fn client() -> Client {
    Client::new("http://127.0.0.1:19080").expect("Dgraph client")
}

#[cfg(feature = "acl")]
async fn client() -> AclClientType<LazyChannel> {
    let default = Client::new("http://127.0.0.1:19080").unwrap();
    default.login("groot", "password").await.expect("Acl client")
}

#[derive(Deserialize, Debug)]
struct Person {
  uid: String,
  name: String,
}

#[derive(Deserialize, Debug)]
struct Persons {
  all: Vec<Person>
}

#[tokio::main]
async fn main() {
    let q = r#"query all($a: string) {
    all(func: eq(name, "Alice")) {
      uid
      name
    }
  }"#;

  let client = client().await;
  let mut txn = client.new_read_only_txn();
  let resp: Response = txn.query(q).await.expect("Query response");
  let persons: Persons = resp.try_into().expect("Persons");
}
Source

fn query_rdf<'life0, 'async_trait, Q>( &'life0 mut self, query: Q, ) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>
where Q: Into<String> + Send + Sync + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

You can run a query with rdf response by calling txn.query_rdf(q).

§Arguments
  • query: GraphQL+- query
§Errors

If transaction is not initialized properly, return EmptyTxn error.

gRPC errors can be returned also.

§Example
use std::collections::HashMap;
use dgraph_tonic::{Client, Response, Query};
use serde::Deserialize;
#[cfg(feature = "acl")]
use dgraph_tonic::{AclClientType, LazyChannel};

#[cfg(not(feature = "acl"))]
async fn client() -> Client {
    Client::new("http://127.0.0.1:19080").expect("Dgraph client")
}

#[cfg(feature = "acl")]
async fn client() -> AclClientType<LazyChannel> {
    let default = Client::new("http://127.0.0.1:19080").unwrap();
    default.login("groot", "password").await.expect("Acl client")
}


#[tokio::main]
async fn main() {
    let q = r#"query all($a: string) {
    all(func: eq(name, "Alice")) {
      uid
      name
    }
  }"#;

  let client = client().await;
  let mut txn = client.new_read_only_txn();
  let resp: Response = txn.query_rdf(q).await.expect("Query response");
  println!("{}",String::from_utf8(resp.rdf).unwrap());
}
Source

fn query_with_vars<'life0, 'async_trait, Q, K, V>( &'life0 mut self, query: Q, vars: HashMap<K, V>, ) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>
where Q: Into<String> + Send + Sync + 'async_trait, K: Into<String> + Send + Sync + Eq + Hash + 'async_trait, V: Into<String> + Send + Sync + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

You can run a query with defined variables by calling txn.query_with_vars(q, vars).

§Arguments
  • query: GraphQL+- query
  • vars: map of variables
§Errors

If transaction is not initialized properly, return EmptyTxn error.

gRPC errors can be returned also.

§Example
use std::collections::HashMap;
use dgraph_tonic::{Client, Response, Query};
use serde::Deserialize;
#[cfg(feature = "acl")]
use dgraph_tonic::{AclClientType, LazyChannel};

#[cfg(not(feature = "acl"))]
async fn client() -> Client {
    Client::new("http://127.0.0.1:19080").expect("Dgraph client")
}

#[cfg(feature = "acl")]
async fn client() -> AclClientType<LazyChannel> {
    let default = Client::new("http://127.0.0.1:19080").unwrap();
    default.login("groot", "password").await.expect("Acl client")
}

#[derive(Deserialize, Debug)]
struct Person {
    uid: String,
    name: String,
}

#[derive(Deserialize, Debug)]
struct Persons {
    all: Vec<Person>
}

#[tokio::main]
async fn main() {
    let q = r#"query all($a: string) {
        all(func: eq(name, $a)) {
        uid
        name
        }
    }"#;

    let mut vars = HashMap::new();
    vars.insert("$a", "Alice");

    let client = client().await;
    let mut txn = client.new_read_only_txn();
    let resp: Response = txn.query_with_vars(q, vars).await.expect("query response");
    let persons: Persons = resp.try_into().expect("Persons");
}
Source

fn query_rdf_with_vars<'life0, 'async_trait, Q, K, V>( &'life0 mut self, query: Q, vars: HashMap<K, V>, ) -> Pin<Box<dyn Future<Output = Result<Response>> + Send + 'async_trait>>
where Q: Into<String> + Send + Sync + 'async_trait, K: Into<String> + Send + Sync + Eq + Hash + 'async_trait, V: Into<String> + Send + Sync + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

You can run a query with defined variables and rdf response by calling txn.query_rdf_with_vars(q, vars).

§Arguments
  • query: GraphQL+- query
  • vars: map of variables
§Errors

If transaction is not initialized properly, return EmptyTxn error.

gRPC errors can be returned also.

§Example
use std::collections::HashMap;
use dgraph_tonic::{Client, Response, Query};
use serde::Deserialize;
#[cfg(feature = "acl")]
use dgraph_tonic::{AclClientType, LazyChannel};

#[cfg(not(feature = "acl"))]
async fn client() -> Client {
    Client::new("http://127.0.0.1:19080").expect("Dgraph client")
}

#[cfg(feature = "acl")]
async fn client() -> AclClientType<LazyChannel> {
    let default = Client::new("http://127.0.0.1:19080").unwrap();
    default.login("groot", "password").await.expect("Acl client")
}


#[tokio::main]
async fn main() {
    let q = r#"query all($a: string) {
        all(func: eq(name, $a)) {
        uid
        name
        }
    }"#;

    let mut vars = HashMap::new();
    vars.insert("$a", "Alice");

    let client = client().await;
    let mut txn = client.new_read_only_txn();
    let resp: Response = txn.query_rdf_with_vars(q, vars).await.expect("query response");
    println!("{}",String::from_utf8(resp.rdf).unwrap());
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<S: IState, C: ILazyClient> Query for TxnVariant<S, C>