[][src]Trait dgraph_tonic::Query

pub trait Query: Send + Sync {
#[must_use]    fn query<'life0, 'async_trait, Q>(
        &'life0 mut self,
        query: Q
    ) -> Pin<Box<dyn Future<Output = Result<Response, DgraphError>> + Send + 'async_trait>>
    where
        Q: Into<String> + Send + Sync,
        Q: 'async_trait,
        'life0: 'async_trait,
        Self: 'async_trait
;
#[must_use] 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, DgraphError>> + Send + 'async_trait>>
    where
        Q: Into<String> + Send + Sync,
        K: Into<String> + Send + Sync + Eq + Hash,
        V: Into<String> + Send + Sync,
        Q: 'async_trait,
        K: 'async_trait,
        V: 'async_trait,
        'life0: 'async_trait,
        Self: 'async_trait
; }

All Dgaph transaction types can performe a queries

Required methods

#[must_use]fn query<'life0, 'async_trait, Q>(
    &'life0 mut self,
    query: Q
) -> Pin<Box<dyn Future<Output = Result<Response, DgraphError>> + Send + 'async_trait>> where
    Q: Into<String> + Send + Sync,
    Q: 'async_trait,
    'life0: 'async_trait,
    Self: '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");
}

#[must_use]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, DgraphError>> + Send + 'async_trait>> where
    Q: Into<String> + Send + Sync,
    K: Into<String> + Send + Sync + Eq + Hash,
    V: Into<String> + Send + Sync,
    Q: 'async_trait,
    K: 'async_trait,
    V: 'async_trait,
    'life0: 'async_trait,
    Self: '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");
}
Loading content...

Implementors

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

Loading content...