[][src]Type Definition dgraph_tonic::MutatedTxn

type MutatedTxn = TxnVariant<Mutated>;

Transaction variant with mutations support.

Methods

impl MutatedTxn[src]

pub async fn discard(__arg0: Self) -> Result<(), DgraphError>[src]

Discard transaction

Errors

Return gRPC error.

pub async fn commit(self) -> Result<(), DgraphError>[src]

Commit transaction

Errors

Return gRPC error.

pub async fn mutate<'_>(
    &'_ mut self,
    mu: Mutation
) -> Result<Response, DgraphError>
[src]

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};
use serde::Serialize;

#[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::new(vec!["http://127.0.0.1:19080"]).await.expect("Connected to dGraph");
   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 commited");
}

pub async fn mutate_and_commit_now(
    __arg0: Self,
    mu: Mutation
) -> Result<Response, DgraphError>
[src]

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};
use serde::Serialize;

#[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::new(vec!["http://127.0.0.1:19080"]).await.expect("Connected to dGraph");
   let txn = client.new_mutated_txn();
   let result = txn.mutate_and_commit_now(mu).await.expect("failed to create data");
}

pub async fn upsert<Q, M>(
    __arg0: Self,
    query: Q,
    mu: M
) -> Result<Response, DgraphError> where
    Q: Into<String> + Send + Sync,
    M: Into<UpsertMutation>, 
[src]

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

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};

#[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::new(vec!["http://127.0.0.1:19080"]).await.expect("Connected to dGraph");
    let op = Operation {
        schema: "email: string @index(exact) .".into(),
        ..Default::default()
    };
    client.alter(op).await.expect("Schema is not updated");    
    let 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");
}

Upsert with more mutations

use dgraph_tonic::{Client, Mutation, Operation};
use std::collections::HashMap;

#[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::new(vec!["http://127.0.0.1:19080"]).await.expect("Connected to dGraph");
    let op = Operation {
        schema: "email: string @index(exact) .".into(),
        ..Default::default()
    };
    client.alter(op).await.expect("Schema is not updated");
    let 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");
}

pub async fn upsert_with_vars<Q, K, V, M>(
    __arg0: Self,
    query: Q,
    vars: HashMap<K, V>,
    mu: M
) -> Result<Response, DgraphError> where
    Q: Into<String> + Send + Sync,
    K: Into<String> + Send + Sync + Eq + Hash,
    V: Into<String> + Send + Sync,
    M: Into<UpsertMutation>, 
[src]

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};
use std::collections::HashMap;

#[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::new(vec!["http://127.0.0.1:19080"]).await.expect("Connected to dGraph");
    let op = Operation {
        schema: "email: string @index(exact) .".into(),
        ..Default::default()
    };
    client.alter(op).await.expect("Schema is not updated");
    let 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");
}

Upsert with more mutations

use dgraph_tonic::{Client, Mutation, Operation};
use std::collections::HashMap;

#[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::new(vec!["http://127.0.0.1:19080"]).await.expect("Connected to dGraph");
    let op = Operation {
        schema: "email: string @index(exact) .".into(),
        ..Default::default()
    };
    client.alter(op).await.expect("Schema is not updated");    
    let 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");
}