Skip to main content

mutate

Function mutate 

Source
pub fn mutate<T: DeserializeOwned>(
    document: &str,
    variables: Value,
) -> Result<T, Error>
Expand description

Write tenant data.

Refuses a document that is empty or that does not begin mutation, before anything is sent.

Examples found in repository?
examples/close_duplicate_lead.rs (line 150)
98fn handler(request: Request<Input>) -> Result<Output, Error> {
99    let duplicate = request.data.lead_id.trim();
100    let survivor = request.data.duplicate_of.trim();
101
102    if duplicate == survivor {
103        return Err(Error::invalid("A lead cannot be a duplicate of itself.")
104            .hint("Pass two different lead ids."));
105    }
106
107    let found: Found = simple::graphql::query(FIND_LEADS, json!({ "ids": [duplicate, survivor] }))?;
108
109    let duplicate_lead = found
110        .leads
111        .iter()
112        .find(|lead| lead.id == duplicate)
113        .ok_or_else(|| {
114            Error::invalid(format!("Lead {duplicate} does not exist."))
115                .details(json!({ "missing": duplicate }))
116                .hint("Search for the lead before closing it.")
117        })?;
118
119    let survivor_lead = found
120        .leads
121        .iter()
122        .find(|lead| lead.id == survivor)
123        .ok_or_else(|| {
124            Error::invalid(format!("Lead {survivor} does not exist."))
125                .details(json!({ "missing": survivor }))
126                .hint("Search for the lead before merging into it.")
127        })?;
128
129    if duplicate_lead.status == "closed" {
130        return Err(Error::domain(
131            "LEAD_ALREADY_CLOSED",
132            format!("Lead {duplicate} is already closed."),
133        )
134        .hint("Nothing to do."));
135    }
136
137    if duplicate_lead.email != survivor_lead.email {
138        return Err(Error::domain(
139            "LEAD_EMAIL_MISMATCH",
140            "The two leads do not share an email address.",
141        )
142        .details(json!({
143            "duplicate": duplicate_lead.email,
144            "survivor": survivor_lead.email,
145        }))
146        .hint("Confirm these are the same person before merging them."));
147    }
148
149    let closed: Closed =
150        simple::graphql::mutate(CLOSE_LEAD, json!({ "id": duplicate, "merged": survivor }))?;
151
152    Ok(Output {
153        closed: duplicate.to_string(),
154        merged_into: survivor.to_string(),
155        rows_changed: closed.result.affected_rows,
156    })
157}
More examples
Hide additional examples
examples/file_expense_receipt.rs (lines 172-180)
106fn handler(request: Request<Input>) -> Result<Output, Error> {
107    let employee = request.data.employee_id.trim();
108    let receipt_url = request.data.receipt_url.trim();
109
110    if employee.is_empty() {
111        return Err(Error::invalid("An expense is filed against an employee.")
112            .hint("Pass the id of the employee the receipt belongs to."));
113    }
114
115    if receipt_url.is_empty() {
116        return Err(Error::invalid("An expense is filed from a receipt.")
117            .hint("Pass the address the receipt is read from."));
118    }
119
120    let policy: Policy = simple::settings::get(APP_ID, &["approval_limit", "currency"])?;
121
122    let receipt = simple::storage::upload_external(
123        Source::url(receipt_url),
124        Target::new(APP_ID, "expense", "receipt"),
125    )?;
126
127    let read: Execution<Receipt> = simple::ai::extract(
128        simple::serde_json::to_value(&receipt)?,
129        "Read the merchant, the currency and the total from this receipt.",
130        json!({
131            "type": "object",
132            "properties": {
133                "merchant": { "type": "string" },
134                "currency": { "type": "string" },
135                "total": { "type": "number" }
136            },
137            "required": ["merchant", "currency", "total"]
138        }),
139        Options::default(),
140    )?;
141
142    let claim = read.data;
143
144    if claim.currency != policy.currency {
145        return Err(Error::domain(
146            "RECEIPT_CURRENCY_MISMATCH",
147            format!(
148                "The receipt is in {} and this app files expenses in {}.",
149                claim.currency, policy.currency
150            ),
151        )
152        .details(json!({ "expected": policy.currency, "receipt": claim.currency }))
153        .hint("File it against an app configured for that currency. The file is stored; the expense is not."));
154    }
155
156    if claim.total > policy.approval_limit {
157        return Err(Error::domain(
158            "EXPENSE_OVER_LIMIT",
159            format!(
160                "{} {} is over the {} approval limit.",
161                claim.total, claim.currency, policy.approval_limit
162            ),
163        )
164        .details(json!({
165            "limit": policy.approval_limit,
166            "merchant": claim.merchant,
167            "total": claim.total,
168        }))
169        .hint("Send it for approval instead. The file is stored; the expense is not."));
170    }
171
172    let filed: Filed = simple::graphql::mutate(
173        FILE_EXPENSE,
174        json!({
175            "employee": employee,
176            "merchant": &claim.merchant,
177            "receipt": &receipt,
178            "total": claim.total,
179        }),
180    )?;
181
182    Ok(Output {
183        expense_id: filed.expense.id,
184        merchant: claim.merchant,
185        total: claim.total,
186        receipt,
187    })
188}