1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use query_structure::Model;
use schema::QuerySchema;

use super::*;
use crate::ParsedField;

pub(crate) fn find_first(
    field: ParsedField<'_>,
    model: Model,
    query_schema: &QuerySchema,
) -> QueryGraphBuilderResult<ReadQuery> {
    let many_query = many::find_many(field, model, query_schema)?;
    try_limit_to_one(many_query)
}

pub(crate) fn find_first_or_throw(
    field: ParsedField<'_>,
    model: Model,
    query_schema: &QuerySchema,
) -> QueryGraphBuilderResult<ReadQuery> {
    let many_query = many::find_many_or_throw(field, model, query_schema)?;
    try_limit_to_one(many_query)
}

#[inline]
fn try_limit_to_one(mut query: ReadQuery) -> QueryGraphBuilderResult<ReadQuery> {
    Ok(match query {
        ReadQuery::ManyRecordsQuery(ref mut m) if m.args.take.is_none() => {
            m.args.take = Some(1);
            query
        }
        _ => query,
    })
}