Skip to main content

Module graphql

Module graphql 

Source
Expand description

Reading and writing tenant data.

Two functions, both of which answer with the type you asked for:

#[derive(Deserialize)]
struct Invoice {
    amount: f64,
}

#[derive(Deserialize)]
struct Open {
    invoices: Vec<Invoice>,
}

const OPEN: &str = r#"
  query Open($id: ID!) {
    invoices: crm__invoice(where: {customer_id: {_eq: $id}}, limit: 100) { amount }
  }"#;

let open: Open = simple::graphql::query(OPEN, json!({ "id": "CUS1" }))?;
let total: f64 = open.invoices.iter().map(|invoice| invoice.amount).sum();

assert_eq!(total, 20.0);

§Why the read and the write are separate functions

A read that fails changed nothing; a write that fails may have landed, and that single bit decides whether the call may be repeated. The party that knows which of the two happened is the function that made the call, so each reports its own outcome: query reports QUERY_EXECUTION_FAILED and mutate reports MUTATION_EXECUTION_FAILED. An action author gets the right one by calling the right function.

The same split runs through the rest: a read whose response cannot be decoded is INVALID_QUERY_RESPONSE, and a write whose result cannot be decoded is MUTATION_RESULT_UNREADABLE — a write that ran and whose effect was never established, which is never retried and always reported.

§One function per intent

There is no combined execute. Which function you call is the declaration of intent, so what a failure means is settled at the call site. The document check below stays what it is — a guard against an obvious mix-up, a mutation handed to query or a query handed to mutate — and never the thing that decides whether a write may be repeated.

Functions§

mutate
Write tenant data.
query
Read tenant data.