Skip to main content

query

Function query 

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

Read tenant data.

Refuses a document that is empty or that begins mutation, before anything is sent.

#[derive(Deserialize)]
struct Answer {
    rows: Vec<Value>,
}

let answer: Answer = simple::graphql::query("query Q { rows: app__thing { id } }", json!({}))?;

assert!(answer.rows.is_empty());
Examples found in repository?
examples/load_knowledge.rs (line 142)
138fn handler(request: Request<Input>) -> Result<Output, Error> {
139    let ids = wanted_ids(request.data.ids)?;
140
141    let loaded: Loaded =
142        simple::graphql::query(LOAD_KNOWLEDGE, json!({ "ids": ids, "limit": ids.len() }))?;
143
144    let mut found: HashMap<String, Item> = loaded
145        .knowledge
146        .into_iter()
147        .filter_map(|article| {
148            let id = trimmed(&article.id);
149
150            if id.is_empty() {
151                return None;
152            }
153
154            Some((id.clone(), item(id, article)))
155        })
156        .collect();
157
158    let missing: Vec<&String> = ids.iter().filter(|id| !found.contains_key(*id)).collect();
159
160    if !missing.is_empty() {
161        return Err(Error::invalid(format!(
162            "Knowledge article(s) not found: {}.",
163            join(&missing)
164        ))
165        .details(json!({ "missing": missing }))
166        .hint("Search for the article before loading it, or drop the id that does not exist."));
167    }
168
169    Ok(Output {
170        items: ids
171            .iter()
172            .filter_map(|id| found.remove(id))
173            .collect::<Vec<Item>>(),
174    })
175}
More examples
Hide additional examples
examples/close_duplicate_lead.rs (line 107)
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}