[][src]Trait dgraph_tonic::Mutate

pub trait Mutate: Query {
#[must_use]    fn discard<'async_trait>(
        self
    ) -> Pin<Box<dyn Future<Output = Result<(), DgraphError>> + Send + 'async_trait>>
    where
        Self: 'async_trait
;
#[must_use] fn commit<'async_trait>(
        self
    ) -> Pin<Box<dyn Future<Output = Result<(), DgraphError>> + Send + 'async_trait>>
    where
        Self: 'async_trait
;
#[must_use] fn mutate<'life0, 'async_trait>(
        &'life0 mut self,
        mu: Mutation
    ) -> Pin<Box<dyn Future<Output = Result<MutationResponse, DgraphError>> + Send + 'async_trait>>
    where
        'life0: 'async_trait,
        Self: 'async_trait
;
#[must_use] fn mutate_and_commit_now<'async_trait>(
        self,
        mu: Mutation
    ) -> Pin<Box<dyn Future<Output = Result<MutationResponse, DgraphError>> + Send + 'async_trait>>
    where
        Self: 'async_trait
;
#[must_use] fn upsert<'life0, 'async_trait, Q, M>(
        &'life0 mut self,
        query: Q,
        mu: M
    ) -> Pin<Box<dyn Future<Output = Result<MutationResponse, DgraphError>> + Send + 'async_trait>>
    where
        Q: Into<String> + Send + Sync,
        M: Into<UpsertMutation> + Send + Sync,
        Q: 'async_trait,
        M: 'async_trait,
        'life0: 'async_trait,
        Self: 'async_trait
;
#[must_use] fn upsert_and_commit_now<'async_trait, Q, M>(
        self,
        query: Q,
        mu: M
    ) -> Pin<Box<dyn Future<Output = Result<MutationResponse, DgraphError>> + Send + 'async_trait>>
    where
        Q: Into<String> + Send + Sync,
        M: Into<UpsertMutation> + Send + Sync,
        Q: 'async_trait,
        M: 'async_trait,
        Self: 'async_trait
;
#[must_use] fn upsert_with_vars<'life0, 'async_trait, Q, K, V, M>(
        &'life0 mut self,
        query: Q,
        vars: HashMap<K, V>,
        mu: M
    ) -> Pin<Box<dyn Future<Output = Result<MutationResponse, DgraphError>> + Send + 'async_trait>>
    where
        Q: Into<String> + Send + Sync,
        K: Into<String> + Send + Sync + Eq + Hash,
        V: Into<String> + Send + Sync,
        M: Into<UpsertMutation> + Send + Sync,
        Q: 'async_trait,
        K: 'async_trait,
        V: 'async_trait,
        M: 'async_trait,
        'life0: 'async_trait,
        Self: 'async_trait
;
#[must_use] fn upsert_with_vars_and_commit_now<'async_trait, Q, K, V, M>(
        self,
        query: Q,
        vars: HashMap<K, V>,
        mu: M
    ) -> Pin<Box<dyn Future<Output = Result<MutationResponse, DgraphError>> + Send + 'async_trait>>
    where
        Q: Into<String> + Send + Sync,
        K: Into<String> + Send + Sync + Eq + Hash,
        V: Into<String> + Send + Sync,
        M: Into<UpsertMutation> + Send + Sync,
        Q: 'async_trait,
        K: 'async_trait,
        V: 'async_trait,
        M: 'async_trait,
        Self: 'async_trait
; }

Allowed mutation operation in Dgraph

Required methods

#[must_use]fn discard<'async_trait>(
    self
) -> Pin<Box<dyn Future<Output = Result<(), DgraphError>> + Send + 'async_trait>> where
    Self: 'async_trait, 

Discard transaction

Errors

Return gRPC error.

#[must_use]fn commit<'async_trait>(
    self
) -> Pin<Box<dyn Future<Output = Result<(), DgraphError>> + Send + 'async_trait>> where
    Self: 'async_trait, 

Commit transaction

Errors

Return gRPC error.

#[must_use]fn mutate<'life0, 'async_trait>(
    &'life0 mut self,
    mu: Mutation
) -> Pin<Box<dyn Future<Output = Result<MutationResponse, DgraphError>> + Send + 'async_trait>> where
    'life0: 'async_trait,
    Self: 'async_trait, 

Adding or removing data in Dgraph is called a mutation.

Arguments

  • mu: required mutations

Errors

  • GrpcError: there is error in communication or server does not accept mutation
  • MissingTxnContext: there is error in txn setup

Example

use dgraph_tonic::{Client, Mutation, Mutate};
use serde::Serialize;
#[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(Serialize)]
struct Person {
  uid: String,
  name: String,
}
#[tokio::main]
async fn main() {
   let p = Person {
       uid:  "_:alice".into(),
       name: "Alice".into(),
   };

   let mut mu = Mutation::new();
   mu.set_set_json(&p).expect("JSON");

   let client = client().await;
   let mut txn = client.new_mutated_txn();
   let result = txn.mutate(mu).await.expect("failed to create data");
   txn.commit().await.expect("Txn is not committed");
}

#[must_use]fn mutate_and_commit_now<'async_trait>(
    self,
    mu: Mutation
) -> Pin<Box<dyn Future<Output = Result<MutationResponse, DgraphError>> + Send + 'async_trait>> where
    Self: 'async_trait, 

Adding or removing data in Dgraph is called a mutation.

Sometimes, you only want to commit a mutation, without querying anything further. In such cases, you can use this function to indicate that the mutation must be immediately committed.

Arguments

  • mu: required mutations

Errors

  • GrpcError: there is error in communication or server does not accept mutation
  • MissingTxnContext: there is error in txn setup

Example

use dgraph_tonic::{Client, Mutation, Mutate};
use serde::Serialize;
#[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(Serialize)]
struct Person {
  uid: String,
  name: String,
}

#[tokio::main]
async fn main() {
   let p = Person {
       uid:  "_:alice".into(),
       name: "Alice".into(),
   };

   let mut mu = Mutation::new();
   mu.set_set_json(&p).expect("JSON");

   let client = client().await;
   let txn = client.new_mutated_txn();
   let result = txn.mutate_and_commit_now(mu).await.expect("failed to create data");
}

#[must_use]fn upsert<'life0, 'async_trait, Q, M>(
    &'life0 mut self,
    query: Q,
    mu: M
) -> Pin<Box<dyn Future<Output = Result<MutationResponse, DgraphError>> + Send + 'async_trait>> where
    Q: Into<String> + Send + Sync,
    M: Into<UpsertMutation> + Send + Sync,
    Q: 'async_trait,
    M: 'async_trait,
    'life0: 'async_trait,
    Self: 'async_trait, 

This function allows you to run upserts consisting of one query and one or more mutations.

Arguments

  • q: Dgraph query
  • mu: required mutations

Errors

  • GrpcError: there is error in communication or server does not accept mutation
  • MissingTxnContext: there is error in txn setup

Example

Upsert with one mutation

use dgraph_tonic::{Client, Mutation, Operation, Mutate};
#[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 {
            user as var(func: eq(email, "wrong_email@dgraph.io"))
        }"#;

    let mut mu = Mutation::new();
    mu.set_set_nquads(r#"uid(user) <email> "correct_email@dgraph.io" ."#);

    let client = client().await;
    let op = Operation {
        schema: "email: string @index(exact) .".into(),
        ..Default::default()
    };
    client.alter(op).await.expect("Schema is not updated");
    let mut txn = client.new_mutated_txn();
    // Upsert: If wrong_email found, update the existing data or else perform a new mutation.
    let response = txn.upsert(q, mu).await.expect("failed to upsert data");
    txn.commit().await.expect("Txn is not committed");
}

Upsert with more mutations

use dgraph_tonic::{Client, Mutation, Operation, Mutate};
use std::collections::HashMap;
#[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 {
            user as var(func: eq(email, "wrong_email@dgraph.io"))
        }"#;

    let mut mu_1 = Mutation::new();
    mu_1.set_set_nquads(r#"uid(user) <email> "correct_email@dgraph.io" ."#);
    mu_1.set_cond("@if(eq(len(user), 1))");

    let mut mu_2 = Mutation::new();
    mu_2.set_set_nquads(r#"uid(user) <email> "another_email@dgraph.io" ."#);
    mu_2.set_cond("@if(eq(len(user), 2))");

    let client = client().await;
    let op = Operation {
        schema: "email: string @index(exact) .".into(),
        ..Default::default()
    };
    client.alter(op).await.expect("Schema is not updated");
    let mut txn = client.new_mutated_txn();
    // Upsert: If wrong_email found, update the existing data or else perform a new mutation.
    let response = txn.upsert(q, vec![mu_1, mu_2]).await.expect("failed to upsert data");
    txn.commit().await.expect("Txn is not committed");
}

#[must_use]fn upsert_and_commit_now<'async_trait, Q, M>(
    self,
    query: Q,
    mu: M
) -> Pin<Box<dyn Future<Output = Result<MutationResponse, DgraphError>> + Send + 'async_trait>> where
    Q: Into<String> + Send + Sync,
    M: Into<UpsertMutation> + Send + Sync,
    Q: 'async_trait,
    M: 'async_trait,
    Self: 'async_trait, 

This function allows you to run upserts consisting of one query and one or more mutations.

Transaction is committed.

Arguments

  • q: Dgraph query
  • mu: required mutations

Errors

  • GrpcError: there is error in communication or server does not accept mutation
  • MissingTxnContext: there is error in txn setup

#[must_use]fn upsert_with_vars<'life0, 'async_trait, Q, K, V, M>(
    &'life0 mut self,
    query: Q,
    vars: HashMap<K, V>,
    mu: M
) -> Pin<Box<dyn Future<Output = Result<MutationResponse, DgraphError>> + Send + 'async_trait>> where
    Q: Into<String> + Send + Sync,
    K: Into<String> + Send + Sync + Eq + Hash,
    V: Into<String> + Send + Sync,
    M: Into<UpsertMutation> + Send + Sync,
    Q: 'async_trait,
    K: 'async_trait,
    V: 'async_trait,
    M: 'async_trait,
    'life0: 'async_trait,
    Self: 'async_trait, 

This function allows you to run upserts with query variables consisting of one query and one ore more mutations.

Arguments

  • q: Dgraph query
  • mu: required mutations
  • vars: query variables

Errors

  • GrpcError: there is error in communication or server does not accept mutation
  • MissingTxnContext: there is error in txn setup

Example

Upsert with only one mutation

use dgraph_tonic::{Client, Mutation, Operation, Mutate};
use std::collections::HashMap;
#[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 users($email: string) {
            user as var(func: eq(email, $email))
        }"#;
    let mut vars = HashMap::new();
    vars.insert("$email", "wrong_email@dgraph.io");

    let mut mu = Mutation::new();
    mu.set_set_nquads(r#"uid(user) <email> "correct_email@dgraph.io" ."#);

    let client = client().await;
    let op = Operation {
        schema: "email: string @index(exact) .".into(),
        ..Default::default()
    };
    client.alter(op).await.expect("Schema is not updated");
    let mut txn = client.new_mutated_txn();
    // Upsert: If wrong_email found, update the existing data or else perform a new mutation.
    let response = txn.upsert_with_vars(q, vars, mu).await.expect("failed to upsert data");
    txn.commit().await.expect("Txn is not committed");
}

Upsert with more mutations

use dgraph_tonic::{Client, Mutation, Operation, Mutate};
use std::collections::HashMap;
#[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 users($email: string) {
            user as var(func: eq(email, $email))
        }"#;
    let mut vars = HashMap::new();
    vars.insert("$email","wrong_email@dgraph.io");

    let mut mu_1 = Mutation::new();
    mu_1.set_set_nquads(r#"uid(user) <email> "correct_email@dgraph.io" ."#);
    mu_1.set_cond("@if(eq(len(user), 1))");

    let mut mu_2 = Mutation::new();
    mu_2.set_set_nquads(r#"uid(user) <email> "another_email@dgraph.io" ."#);
    mu_2.set_cond("@if(eq(len(user), 2))");

    let client = client().await;
    let op = Operation {
        schema: "email: string @index(exact) .".into(),
        ..Default::default()
    };
    client.alter(op).await.expect("Schema is not updated");
    let mut txn = client.new_mutated_txn();
    // Upsert: If wrong_email found, update the existing data or else perform a new mutation.
    let response = txn.upsert_with_vars(q, vars, vec![mu_1, mu_2]).await.expect("failed to upsert data");
    txn.commit().await.expect("Txn is not committed");
}

#[must_use]fn upsert_with_vars_and_commit_now<'async_trait, Q, K, V, M>(
    self,
    query: Q,
    vars: HashMap<K, V>,
    mu: M
) -> Pin<Box<dyn Future<Output = Result<MutationResponse, DgraphError>> + Send + 'async_trait>> where
    Q: Into<String> + Send + Sync,
    K: Into<String> + Send + Sync + Eq + Hash,
    V: Into<String> + Send + Sync,
    M: Into<UpsertMutation> + Send + Sync,
    Q: 'async_trait,
    K: 'async_trait,
    V: 'async_trait,
    M: 'async_trait,
    Self: 'async_trait, 

This function allows you to run upserts with query variables consisting of one query and one ore more mutations.

Transaction is committed.

Arguments

  • q: Dgraph query
  • mu: required mutations
  • vars: query variables

Errors

  • GrpcError: there is error in communication or server does not accept mutation
  • MissingTxnContext: there is error in txn setup
Loading content...

Implementors

impl<C: ILazyClient> Mutate for TxnMutatedType<C>[src]

Loading content...