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