wundergraph 0.1.2

A GraphQL ORM build on top of diesel
Documentation
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! This module contains all functionality that is needed to implement a
//! wundergraph entity
//!
//! In general wundergraph entities should be implemented using the provided
//! custom derive. Only for cases where the default implementation does not
//! your requirements a manual implementation should be done.
//!
//! # Deriving
//! See the documentation of `WundergraphEntity` for possible options.
//!
//! # Manual implementation
//! **Double check that a manual implementation is really required and could not
//! be replaced by using the provided derive + `QueryModifier`**
//!
//! For a manual implementation of a wundergraph entity it is required to
//! implement at least the following traits for your type:
//!
//! * [`LoadingHandler`](trait.LoadingHandler.html)
//! * [`WundergraphGraphqlMapper`](../..//graphql_type/trait.WundergraphGraphqlMapper.html)
//! * For each foreign key field [`WundergraphBelongsTo`](fields/trait.WundergraphBelongsTo.html)
//! * [`BuildFilterHelper`](filter/trait.BuildFilterHelper.html) if you use your
//!   entity somewhere as part of a filter
//!
//! See the documentation of the corresponding traits on details about the
//! actual implementation
use crate::context::WundergraphContext;
use crate::error::{Result, WundergraphError};
use crate::helper::tuple::IsPrimaryKeyIndex;
use crate::helper::{PrimaryKeyArgument, UnRef};
use crate::juniper_ext::FromLookAheadValue;
use crate::query_builder::selection::order::BuildOrder;
use crate::query_builder::selection::select::BuildSelect;
use crate::scalar::WundergraphScalarValue;
use diesel::associations::HasTable;
use diesel::backend::Backend;
use diesel::dsl::SqlTypeOf;
use diesel::expression::NonAggregate;
use diesel::query_builder::{BoxedSelectStatement, QueryFragment};
use diesel::query_dsl::methods::BoxedDsl;
use diesel::query_dsl::methods::FilterDsl;
use diesel::query_dsl::methods::{LimitDsl, SelectDsl};
use diesel::sql_types::HasSqlType;
use diesel::BoxableExpression;
use diesel::EqAll;
use diesel::Identifiable;
use diesel::QuerySource;
use diesel::{AppearsOnTable, Connection, QueryDsl, Table};
use juniper::LookAheadValue;
use juniper::{Executor, LookAheadArgument, LookAheadSelection, Selection};

pub mod fields;
pub mod filter;
#[doc(hidden)]
pub mod offset;
#[doc(hidden)]
pub mod order;
pub(crate) mod query_modifier;
#[doc(hidden)]
pub mod query_resolver;
#[doc(hidden)]
pub mod select;

use self::fields::WundergraphFieldList;
use self::filter::build_filter::BuildFilter;
use self::filter::inner_filter::InnerFilter;
use self::filter::Filter;
use self::offset::ApplyOffset;

#[doc(inline)]
pub use self::query_resolver::SqlTypeOfPlaceholder;

#[doc(inline)]
pub use self::query_modifier::QueryModifier;

#[doc(inline)]
pub use wundergraph_derive::WundergraphEntity;

/// A helper type to simplify the select statement query type
/// for a given loading handler.
///
/// # Type parameters
/// * `L`: A type implementing `LoadingHandler`
/// * `DB`: A diesel backend type (`diesel::pg::Pg` or `diesel::sqlite::Sqlite`)
/// * `Ctx`: Used context type, should implement `WundergraphContext`
pub type BoxedQuery<'a, L, DB, Ctx> = BoxedSelectStatement<
    'a,
    SqlTypeOfPlaceholder<
        <L as LoadingHandler<DB, Ctx>>::FieldList,
        DB,
        <L as LoadingHandler<DB, Ctx>>::PrimaryKeyIndex,
        <L as HasTable>::Table,
        Ctx,
    >,
    <L as HasTable>::Table,
    DB,
>;

/// Main entry point for loading database entities as GraphQL objects
///
///
/// # Deriving
///
/// This trait could be derived by using the
/// [`#[derive(WundergraphEntity)]`](derive.WundergraphEntity.html)
/// custom derive
///
/// # Manual implementation
///
/// ## Generic paramaters
/// * `DB` Diesel backend type, should be a concrete type
/// * `Ctx` Wundergraph context type. It's possible to use
///   a generic type parameter here
///
/// ```
/// # #[macro_use] extern crate diesel;
/// # #[cfg(feature = "postgres")]
/// # use diesel::pg::Pg;
/// # use diesel::Connection;
/// use wundergraph::helper::TupleIndex0;
/// use wundergraph::query_builder::selection::LoadingHandler;
/// use wundergraph::WundergraphContext;
///
/// table! {
///     heros {
///         id -> Integer,
///         name -> Text,
///     }
/// }
///
/// #[derive(Identifiable)]
/// struct Hero {
///     id: i32,
///     name: String,
/// }
///
/// # #[cfg(feature = "postgres")]
/// impl<Ctx> LoadingHandler<Pg, Ctx> for Hero
/// where
///     Ctx: WundergraphContext,
///     <Ctx as WundergraphContext>::Connection: Connection<Backend = Pg>,
/// {
///     type Columns = (heros::id, heros::name);
///     type FieldList = (i32, String);
///     type PrimaryKeyIndex = TupleIndex0;
///     type Filter = ();
///
///     const FIELD_NAMES: &'static [&'static str] = &["id", "name"];
///     const TYPE_NAME: &'static str = "Hero";
/// }
/// # fn main() {}
/// ```
pub trait LoadingHandler<DB, Ctx>: HasTable + Sized
where
    DB: Backend + ApplyOffset + 'static,
{
    /// List of columns to load
    ///
    /// This list is given as tuple of diesel column types.
    type Columns: BuildOrder<Self::Table, DB>
        + BuildSelect<
            Self::Table,
            DB,
            SqlTypeOfPlaceholder<Self::FieldList, DB, Self::PrimaryKeyIndex, Self::Table, Ctx>,
        >;

    /// List of all field types
    ///
    /// This list is given as tuple of rust types compatible to the sql type
    /// of the corresponding column. The order of those types is assumed to match
    /// the field order provided to `Columns`
    type FieldList: WundergraphFieldList<DB, Self::PrimaryKeyIndex, Self::Table, Ctx>;

    /// Index of the primary key as index into the `Columns` and `FieldList` tuples
    type PrimaryKeyIndex: Default + IsPrimaryKeyIndex;

    /// The filter type for this entity
    type Filter: InnerFilter + BuildFilter<DB> + 'static;

    /// List of graphql field names
    ///
    /// Those names are assumed to have the same order then the fields in `FieldList`
    const FIELD_NAMES: &'static [&'static str];
    /// The graphql name of the current type
    const TYPE_NAME: &'static str;
    /// The graphql description of the current type
    const TYPE_DESCRIPTION: Option<&'static str> = None;

    /// Main entry point to loading something from the database
    ///
    /// The default implementation passes the final query to the
    /// `QueryModifier`
    fn load<'a>(
        select: &LookAheadSelection<'_, WundergraphScalarValue>,
        selection: Option<&'_ [Selection<'_, WundergraphScalarValue>]>,
        executor: &Executor<'_, Ctx, WundergraphScalarValue>,
        query: BoxedQuery<'a, Self, DB, Ctx>,
    ) -> Result<Vec<juniper::Value<WundergraphScalarValue>>>
    where
        DB: HasSqlType<
            SqlTypeOfPlaceholder<Self::FieldList, DB, Self::PrimaryKeyIndex, Self::Table, Ctx>,
        >,
        Ctx: WundergraphContext + QueryModifier<Self, DB>,
        Ctx::Connection: Connection<Backend = DB>,
        DB::QueryBuilder: Default,
        <Self::Table as QuerySource>::FromClause: QueryFragment<DB>,
    {
        use diesel::RunQueryDsl;
        use juniper::LookAheadMethods;

        let ctx = executor.context();
        let conn = ctx.get_connection();
        let query = ctx.modify_query(select, query)?;
        #[cfg(feature = "debug")]
        {
            log::debug!("{:?}", diesel::debug_query(&query));
        }
        let placeholder = <_ as RunQueryDsl<_>>::load(query, conn)?;
        Ok(Self::FieldList::resolve(
            placeholder,
            select.arguments(),
            select,
            selection,
            Self::FIELD_NAMES,
            executor,
        )?)
    }

    /// Load a single entity by a given primary key
    ///
    /// The default implementation calls `load` internally
    fn load_by_primary_key<'a>(
        select: &LookAheadSelection<'_, WundergraphScalarValue>,
        selection: Option<&'_ [Selection<'_, WundergraphScalarValue>]>,
        executor: &Executor<'_, Ctx, WundergraphScalarValue>,
        mut query: BoxedQuery<'a, Self, DB, Ctx>,
    ) -> Result<Option<juniper::Value<WundergraphScalarValue>>>
    where
        Self: 'static,
        &'static Self: Identifiable,
        Ctx: WundergraphContext + QueryModifier<Self, DB>,
        Ctx::Connection: Connection<Backend = DB>,
        <&'static Self as Identifiable>::Id: UnRef<'static>,
        <Self::Table as Table>::PrimaryKey:
            EqAll<<<&'static Self as Identifiable>::Id as UnRef<'static>>::UnRefed> + Default,
        <<Self::Table as Table>::PrimaryKey as EqAll<
            <<&'static Self as Identifiable>::Id as UnRef<'static>>::UnRefed,
        >>::Output: AppearsOnTable<Self::Table> + NonAggregate + QueryFragment<DB>,
        PrimaryKeyArgument<'static, Self::Table, (), <&'static Self as Identifiable>::Id>:
            FromLookAheadValue,
        DB: HasSqlType<
            SqlTypeOfPlaceholder<Self::FieldList, DB, Self::PrimaryKeyIndex, Self::Table, Ctx>,
        >,
        DB::QueryBuilder: Default,
        <Self::Table as QuerySource>::FromClause: QueryFragment<DB>,
    {
        use juniper::LookAheadMethods;
        let v = select
            .argument("primaryKey")
            .ok_or(WundergraphError::NoPrimaryKeyArgumentFound)?;
        let key = PrimaryKeyArgument::<
            Self::Table,
            _,
            <&'static Self as Identifiable>::Id,
            >::from_look_ahead(v.value())
            .ok_or(WundergraphError::NoPrimaryKeyArgumentFound)?;
        query = <_ as QueryDsl>::filter(
            query,
            <Self::Table as Table>::PrimaryKey::default().eq_all(key.values),
        );
        query = <_ as QueryDsl>::limit(query, 1);
        let res = Self::load(select, selection, executor, query)?;
        Ok(res.into_iter().next())
    }

    /// Build a sql query to load this entity from a given graphql request
    ///
    /// The default implementation calls `get_select`, `apply_filter`,
    /// `apply_limit`, `apply_offset` and `apply_order` to construct the final
    /// query
    fn build_query<'a>(
        _global_arguments: &[LookAheadArgument<WundergraphScalarValue>],
        select: &LookAheadSelection<'_, WundergraphScalarValue>,
    ) -> Result<BoxedQuery<'a, Self, DB, Ctx>>
    where
        Self::Table: BoxedDsl<
                'a,
                DB,
                Output = BoxedSelectStatement<
                    'a,
                    SqlTypeOf<<Self::Table as Table>::AllColumns>,
                    Self::Table,
                    DB,
                >,
            > + 'static,
        <Self::Filter as BuildFilter<DB>>::Ret: AppearsOnTable<Self::Table>,
    {
        let mut query =
            <_ as SelectDsl<_>>::select(Self::table().into_boxed(), Self::get_select(select)?);

        query = Self::apply_filter(query, select)?;
        query = Self::apply_limit(query, select)?;
        query = Self::apply_offset(query, select)?;
        query = Self::apply_order(query, select)?;

        Ok(query)
    }

    /// Construct a select clause for the current entity from a given graphql request
    fn get_select(
        select: &LookAheadSelection<'_, WundergraphScalarValue>,
    ) -> Result<
        Box<
            dyn BoxableExpression<
                Self::Table,
                DB,
                SqlType = SqlTypeOfPlaceholder<
                    Self::FieldList,
                    DB,
                    Self::PrimaryKeyIndex,
                    Self::Table,
                    Ctx,
                >,
            >,
        >,
    > {
        use juniper::LookAheadMethods;
        <Self::Columns as BuildSelect<Self::Table, DB, _>>::build_select(
            select,
            |local_index| {
                Self::FieldList::map_table_field(local_index, |global| Self::FIELD_NAMES[global])
                    .expect("Field is there")
            },
            Self::PrimaryKeyIndex::is_index,
            (0..Self::FieldList::NON_TABLE_FIELD_COUNT).any(|i| {
                Self::FieldList::map_non_table_field(i, |global| {
                    select.has_child(Self::FIELD_NAMES[global])
                })
                .unwrap_or(false)
            }),
        )
    }

    /// Construct a where clause from a given graphql request
    fn apply_filter<'a>(
        query: BoxedQuery<'a, Self, DB, Ctx>,
        select: &LookAheadSelection<'_, WundergraphScalarValue>,
    ) -> Result<BoxedQuery<'a, Self, DB, Ctx>>
    where
        Self::Table: 'static,
        <Self::Filter as BuildFilter<DB>>::Ret: AppearsOnTable<Self::Table>,
    {
        use juniper::LookAheadMethods;
        if let Some(filter) = select.argument("filter") {
            if let Some(filter) =
                <Filter<Self::Filter, Self::Table> as FromLookAheadValue>::from_look_ahead(
                    filter.value(),
                )
                .and_then(<_ as BuildFilter<DB>>::into_filter)
            {
                Ok(<_ as FilterDsl<_>>::filter(query, filter))
            } else {
                Ok(query)
            }
        } else {
            Ok(query)
        }
    }

    /// Construct a order clause from a given graphql request
    fn apply_order<'a>(
        mut query: BoxedQuery<'a, Self, DB, Ctx>,
        select: &LookAheadSelection<'_, WundergraphScalarValue>,
    ) -> Result<BoxedQuery<'a, Self, DB, Ctx>>
    where
        Self::Table: 'static,
    {
        use juniper::LookAheadMethods;
        if let Some(LookAheadValue::List(order)) =
            select.argument("order").map(LookAheadArgument::value)
        {
            let order_stmts = <Self::Columns as BuildOrder<Self::Table, DB>>::build_order(
                order,
                |local_index| {
                    Self::FieldList::map_table_field(local_index, |global| {
                        Self::FIELD_NAMES[global]
                    })
                    .expect("Field is there")
                },
            )?;
            for s in order_stmts {
                query = query.then_order_by(s);
            }
            Ok(query)
        } else {
            Ok(query)
        }
    }

    /// Construct a limit clause from a given graphql request
    fn apply_limit<'a>(
        query: BoxedQuery<'a, Self, DB, Ctx>,
        select: &LookAheadSelection<'_, WundergraphScalarValue>,
    ) -> Result<BoxedQuery<'a, Self, DB, Ctx>> {
        use juniper::LookAheadMethods;
        if let Some(limit) = select.argument("limit") {
            Ok(<_ as LimitDsl>::limit(
                query,
                i64::from_look_ahead(limit.value())
                    .ok_or(WundergraphError::CouldNotBuildFilterArgument)?,
            ))
        } else {
            Ok(query)
        }
    }

    /// Construct a offset clause from a given grahpql request
    fn apply_offset<'a>(
        query: BoxedQuery<'a, Self, DB, Ctx>,
        select: &LookAheadSelection<'_, WundergraphScalarValue>,
    ) -> Result<BoxedQuery<'a, Self, DB, Ctx>> {
        <DB as ApplyOffset>::apply_offset::<Self, Ctx>(query, select)
    }

    #[doc(hidden)]
    fn field_description(_idx: usize) -> Option<&'static str> {
        None
    }

    #[doc(hidden)]
    #[allow(clippy::option_option)]
    fn field_deprecation(_idx: usize) -> Option<Option<&'static str>> {
        None
    }
}