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
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//! This module contains data structures to build a generic graphql interface to
//! filter entities. The main entry point is the [`Filter`](struct.Filter.html) struct

use crate::diesel_ext::BoxableFilter;
use crate::juniper_ext::{FromLookAheadValue, NameBuilder, Nameable};
use crate::scalar::WundergraphScalarValue;
use diesel::backend::Backend;
use diesel::query_builder::{BoxedSelectStatement, QueryFragment};
use diesel::sql_types::Bool;
use diesel::AppearsOnTable;
use diesel::QueryDsl;
use diesel::Table;
use indexmap::IndexMap;
use juniper::meta::{Argument, MetaType};
use juniper::FromInputValue;
use juniper::GraphQLType;
use juniper::InputValue;
use juniper::LookAheadValue;
use juniper::Registry;
use juniper::ToInputValue;
use std::marker::PhantomData;

pub(crate) mod build_filter;
pub mod collector;
mod common_filter;
pub(crate) mod filter_helper;
pub(crate) mod filter_value;
pub(crate) mod inner_filter;
mod not;
mod nullable_filter;
mod reference_filter;
mod string_filter;

use self::collector::{AndCollector, FilterCollector, OrCollector};
use self::not::Not;

#[doc(inline)]
pub use self::build_filter::BuildFilter;
#[doc(inline)]
pub use self::common_filter::FilterOption;
#[doc(inline)]
pub use self::filter_helper::AsColumnFilter;
#[doc(inline)]
pub use self::filter_helper::AsNonColumnFilter;
#[doc(inline)]
pub use self::filter_helper::BuildFilterHelper;
#[doc(inline)]
pub use self::filter_helper::CreateFilter;
#[doc(inline)]
pub use self::filter_value::FilterValue;
#[doc(inline)]
pub use self::inner_filter::InnerFilter;

#[doc(hidden)]
pub use self::filter_helper::*;

/// Main filter struct
///
/// This struct is the main entry point to wundergraphs filter api
/// The exact field specfic filters are given by a subtype (`inner`)
#[derive(Debug)]
pub struct Filter<F, T> {
    and: Option<Vec<Filter<F, T>>>,
    or: Option<Vec<Filter<F, T>>>,
    not: Option<Box<Not<Filter<F, T>>>>,
    inner: F,
    p: PhantomData<T>,
}

impl<F, T> Nameable for Filter<F, T>
where
    F: Nameable,
{
    fn name() -> String {
        F::name()
    }
}

impl<F, T> FromInputValue<WundergraphScalarValue> for Filter<F, T>
where
    F: InnerFilter,
{
    fn from_input_value(v: &InputValue<WundergraphScalarValue>) -> Option<Self> {
        if let Some(obj) = v.to_object_value() {
            let and = obj.get("and").map_or_else(
                || Option::from_input_value(&InputValue::Null),
                |v| Option::from_input_value(*v),
            )?;
            let or = obj.get("or").map_or_else(
                || Option::from_input_value(&InputValue::Null),
                |v| Option::from_input_value(*v),
            )?;
            let not = obj.get("not").map_or_else(
                || Option::from_input_value(&InputValue::Null),
                |v| Option::from_input_value(*v),
            )?;
            let inner = F::from_inner_input_value(obj)?;
            Some(Self {
                and,
                or,
                not,
                inner,
                p: PhantomData,
            })
        } else {
            None
        }
    }
}

impl<F, T> ToInputValue<WundergraphScalarValue> for Filter<F, T>
where
    F: InnerFilter,
{
    fn to_input_value(&self) -> InputValue<WundergraphScalarValue> {
        let mut map = IndexMap::with_capacity(Self::FIELD_COUNT);
        self.to_inner_input_value(&mut map);
        InputValue::object(map)
    }
}

impl<F, T> GraphQLType<WundergraphScalarValue> for Filter<F, T>
where
    F: InnerFilter,
{
    type Context = F::Context;
    type TypeInfo = NameBuilder<Self>;

    fn name(info: &Self::TypeInfo) -> Option<&str> {
        Some(info.name())
    }

    fn meta<'r>(
        info: &Self::TypeInfo,
        registry: &mut Registry<'r, WundergraphScalarValue>,
    ) -> MetaType<'r, WundergraphScalarValue>
    where
        WundergraphScalarValue: 'r,
    {
        let fields = Self::register_fields(info, registry);
        registry
            .build_input_object_type::<Self>(info, &fields)
            .into_meta()
    }
}

impl<F, T> FromLookAheadValue for Filter<F, T>
where
    F: InnerFilter,
{
    fn from_look_ahead(v: &LookAheadValue<'_, WundergraphScalarValue>) -> Option<Self> {
        if let LookAheadValue::Object(ref obj) = *v {
            Some(Self::from_inner_look_ahead(obj))
        } else {
            None
        }
    }
}

impl<F, DB, T> BuildFilter<DB> for Filter<F, T>
where
    DB: Backend + 'static,
    F: InnerFilter + BuildFilter<DB> + 'static,
    T: 'static,
    F::Ret: AppearsOnTable<T>,
{
    type Ret = Box<dyn BoxableFilter<T, DB, SqlType = Bool>>;

    fn into_filter(self) -> Option<Self::Ret>
where {
        let Self { and, or, inner, .. } = self;
        let mut and = and
            .map(|a| {
                a.into_iter().fold(AndCollector::default(), |mut a, f| {
                    a.append_filter(f);
                    a
                })
            })
            .unwrap_or_default();
        let or = or
            .map(|a| {
                a.into_iter().fold(OrCollector::default(), |mut o, f| {
                    o.append_filter(f);
                    o
                })
            })
            .unwrap_or_default();
        and.append_filter(self.not.map(|not| *not));
        and.append_filter(or);
        and.append_filter(inner);
        and.into_filter()
    }
}

impl<F, T> Filter<F, T>
where
    F: InnerFilter,
{
    /// Apply the filter to a given select statement
    ///
    /// This function will extend the where clause with the given filter expression
    /// In case there is already an existing filter the new filter will by connected
    /// connected by and
    pub fn apply_filter<'a, ST, DB>(
        self,
        mut q: BoxedSelectStatement<'a, ST, T, DB>,
    ) -> BoxedSelectStatement<'a, ST, T, DB>
    where
        T: Table + 'a,
        DB: Backend + 'a,
        Self: BuildFilter<DB>,
        <Self as BuildFilter<DB>>::Ret: AppearsOnTable<T> + QueryFragment<DB> + 'a,
    {
        if let Some(f) = self.into_filter() {
            q = <BoxedSelectStatement<'_, _, _, _> as QueryDsl>::filter(q, f);
        }
        q
    }
}

impl<F, T> InnerFilter for Filter<F, T>
where
    F: InnerFilter,
{
    type Context = F::Context;

    const FIELD_COUNT: usize = F::FIELD_COUNT + 3;

    fn from_inner_input_value(
        obj: IndexMap<&str, &InputValue<WundergraphScalarValue>>,
    ) -> Option<Self> {
        let and = obj.get("and").map_or_else(
            || Option::from_input_value(&InputValue::Null),
            |v| Option::from_input_value(*v),
        )?;
        let or = obj.get("or").map_or_else(
            || Option::from_input_value(&InputValue::Null),
            |v| Option::from_input_value(*v),
        )?;
        let not = obj.get("not").map_or_else(
            || Option::from_input_value(&InputValue::Null),
            |v| Option::from_input_value(*v),
        )?;
        let inner = F::from_inner_input_value(obj)?;
        Some(Self {
            and,
            or,
            not,
            inner,
            p: PhantomData,
        })
    }

    fn from_inner_look_ahead(objs: &[(&str, LookAheadValue<'_, WundergraphScalarValue>)]) -> Self {
        let and = objs
            .iter()
            .find(|o| o.0 == "and")
            .and_then(|o| Vec::from_look_ahead(&o.1));

        let or = objs
            .iter()
            .find(|o| o.0 == "or")
            .and_then(|o| Vec::from_look_ahead(&o.1));

        let not = objs
            .iter()
            .find(|o| o.0 == "not")
            .and_then(|o| Box::from_look_ahead(&o.1));

        let inner = F::from_inner_look_ahead(objs);

        Self {
            and,
            or,
            not,
            inner,
            p: PhantomData,
        }
    }

    fn to_inner_input_value(&self, map: &mut IndexMap<&str, InputValue<WundergraphScalarValue>>) {
        map.insert("and", self.and.to_input_value());
        map.insert("or", self.or.to_input_value());
        map.insert("not", self.not.to_input_value());
        self.inner.to_inner_input_value(map);
    }

    fn register_fields<'r>(
        info: &NameBuilder<Self>,
        registry: &mut Registry<'r, WundergraphScalarValue>,
    ) -> Vec<Argument<'r, WundergraphScalarValue>> {
        let and = registry.arg_with_default::<Option<Vec<Self>>>("and", &None, info);
        let or = registry.arg_with_default::<Option<Vec<Self>>>("or", &None, info);
        let not = registry.arg_with_default::<Option<Box<Not<Self>>>>(
            "not",
            &None,
            &NameBuilder::default(),
        );
        let mut fields = vec![and, or, not];
        fields.extend(F::register_fields(&NameBuilder::default(), registry));
        fields
    }
}