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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
use std::collections::{HashMap, HashSet};

use async_graphql::{
    dynamic::{
        Enum, Field, FieldFuture, FieldValue, InputObject, InputValue, Object,
        ResolverContext, Scalar, Schema as DynamicSchema,
        SchemaBuilder as DynamicSchemaBuilder, SchemaError, TypeRef,
    },
    Request,
};
use async_graphql_parser::types::{BaseType, Type};
use async_graphql_value::Name;
use fuel_indexer_database::{queries, IndexerConnectionPool};
use fuel_indexer_schema::db::tables::IndexerSchema;
use lazy_static::lazy_static;
use serde_json::Value;

use crate::graphql::{GraphqlError, GraphqlQueryBuilder, GraphqlResult};

lazy_static! {
    /// Scalar types supported by the Fuel indexer. These should always stay up-to-date
    /// with fuel-indexer-schema/src/base.graphql.
    static ref SCALAR_TYPES: HashSet<&'static str> = HashSet::from([
        "Address",
        "AssetId",
        "Boolean",
        "Bytes",
        "Bytes32",
        "Bytes4",
        "Bytes64",
        "Bytes8",
        "ContractId",
        "I128",
        "I16",
        "I32",
        "I64",
        "I8",
        "ID",
        "Identity",
        "Json",
        "U128",
        "U16",
        "U32",
        "U64",
        "U8",
        "UID",
    ]);

    /// Scalar types that are represented by a numeric type. This ensures that the
    /// value type provided for a field filter matches the type of the scalar itself.
    static ref NUMERIC_SCALAR_TYPES: HashSet<&'static str> = HashSet::from([
        "I128",
        "I16",
        "I32",
        "I64",
        "U128",
        "U16",
        "U32",
        "U64",
    ]);

    /// Scalar types that are represented by a string type. This ensures that the
    /// value type provided for a field filter matches the type of the scalar itself.
    static ref STRING_SCALAR_TYPES: HashSet<&'static str> = HashSet::from([
        "Address",
        "AssetId",
        "Bytes",
        "Bytes32",
        "Bytes4",
        "Bytes64",
        "Bytes64",
        "Bytes8",
        "ContractId",
        "ID",
        "Identity",
        "Json",
        "UID",
    ]);

    /// Scalar types that can be sorted.
    static ref SORTABLE_SCALAR_TYPES: HashSet<&'static str> = HashSet::from([
        "Address",
        "AssetId",
        "ContractId",
        "I128",
        "I16",
        "I32",
        "I64",
        "ID",
        "Identity",
        "U128",
        "U16",
        "U32",
        "U64",
        "UID",
    ]);

    /// Entity types that should be ignored when building the dynamic schema,
    /// so that they do not appear in the generated documentation. This is done
    /// to hide internal Fuel indexer entity types.
    static ref IGNORED_ENTITY_TYPES: HashSet<&'static str> =
        HashSet::from(["IndexMetadataEntity"]);

    /// Entity fields that should be ignored when building the dynamic schema,
    /// so that they do not appear in the generated documentation. This is done
    /// to hide internal Fuel indexer entity fields.
    static ref IGNORED_ENTITY_FIELD_TYPES: HashSet<&'static str> =
        HashSet::from(["object"]);
}

/// Execute user query and return results.
pub async fn execute_query(
    dynamic_request: Request,
    dynamic_schema: DynamicSchema,
    user_query: String,
    pool: IndexerConnectionPool,
    schema: IndexerSchema,
) -> GraphqlResult<Value> {
    // Because the schema types from async-graphql expect each field to be resolved
    // separately, it became untenable to use the .execute() method of the dynamic
    // schema itself to resolve queries. Instead, we set it to only resolve
    // introspection queries and then pass any non-introspection queries to our
    // custom query resolver.
    match dynamic_request.operation_name.as_deref() {
        Some("IntrospectionQuery") | Some("introspectionquery") => {
            let introspection_results = dynamic_schema.execute(dynamic_request).await;
            let data = introspection_results.data.into_json()?;

            Ok(data)
        }
        Some(_) | None => {
            let query =
                GraphqlQueryBuilder::new(&schema, user_query.as_str())?.build()?;

            let queries = query.as_sql(&schema, pool.database_type())?.join(";\n");

            let mut conn = match pool.acquire().await {
                Ok(c) => c,
                Err(e) => return Err(GraphqlError::QueryError(e.to_string())),
            };

            match queries::run_query(&mut conn, queries).await {
                Ok(r) => Ok(r),
                Err(e) => Err(GraphqlError::QueryError(e.to_string())),
            }
        }
    }
}

/// Build a dynamic schema. This allows for introspection, which allows for extensive
/// auto-documentation and code suggestions.
pub fn build_dynamic_schema(schema: &IndexerSchema) -> GraphqlResult<DynamicSchema> {
    // Register scalars into dynamic schema so that users are aware of their existence.
    let mut schema_builder: DynamicSchemaBuilder = SCALAR_TYPES.iter().fold(
        DynamicSchema::build("QueryRoot", None, None).introspection_only(),
        |sb, scalar| {
            // These types come pre-included in SchemaBuilder.
            if *scalar == "Boolean" || *scalar == "ID" {
                sb
            } else {
                sb.register(Scalar::new(*scalar))
            }
        },
    );

    let mut input_objects = Vec::new();

    // For some reason, async-graphql does not implement the Hash trait on any of the
    // type that we need for dynamic schemas. So we are essentially making a hash table
    // ourselves for the filter and sort objects.
    let mut filter_object_list = Vec::new();
    let mut filter_tracker = HashMap::new();
    let mut sort_object_list = Vec::new();
    let mut sorter_tracker = HashMap::new();

    // async-graphql requires a root query object so that the base entity
    // fields can be queried against. This QueryRoot does not appear anywhere
    // in the generated documentation nor is it required for the user to create.
    let mut query_root = Object::new("QueryRoot");

    let sort_enum = Enum::new("SortOrder").item("asc").item("desc");

    for (entity_type, field_map) in schema.parsed().object_field_mappings() {
        if IGNORED_ENTITY_TYPES.contains(&entity_type.as_str()) {
            continue;
        }

        // Input values are stored for each field so that we can create
        // a final input object for filtering and sorting a field.
        let mut filter_input_vals = Vec::new();
        let mut sort_input_vals = Vec::new();

        // Field names will be added to this enum in order to allow
        // for filtering on the column itself, i.e. "has" operator.
        let mut object_field_enum = Enum::new(format!("{entity_type}Fields"));

        for (field_name, field_type) in field_map.clone() {
            if IGNORED_ENTITY_FIELD_TYPES.contains(&field_name.as_str()) {
                continue;
            }

            let (field_filter_input_val, mut field_input_objects, sort_input_val) =
                create_input_values_and_objects_for_field(
                    field_name.clone(),
                    field_type,
                    entity_type.clone(),
                    &sort_enum,
                )?;

            filter_input_vals.push(field_filter_input_val);
            input_objects.append(&mut field_input_objects);

            if let Some(input_val) = sort_input_val {
                sort_input_vals.push(input_val);
            }

            object_field_enum = object_field_enum.item(field_name);
        }

        if !filter_input_vals.is_empty() {
            let filter_object = filter_input_vals
                .into_iter()
                .fold(
                    InputObject::new(format!("{entity_type}Filter")),
                    |input_obj, input_val| input_obj.field(input_val),
                )
                .field(InputValue::new(
                    "has",
                    TypeRef::named_nn_list(object_field_enum.type_name()),
                ));

            filter_object_list.push(filter_object);
            filter_tracker.insert(entity_type.to_string(), filter_object_list.len() - 1);
        }

        if !sort_input_vals.is_empty() {
            let sort_object = sort_input_vals.into_iter().fold(
                InputObject::new(format!("{entity_type}Sort")),
                |input_obj, input_val| input_obj.field(input_val),
            );

            sort_object_list.push(sort_object);
            sorter_tracker.insert(entity_type.to_string(), sort_object_list.len() - 1);
        }

        // Additionally, because we cannot refer to the object fields directly and
        // associate the field arguments to them, we iterate through the fields a
        // second time and construct the fields for the dynamic schema and add the
        // field arguments as well.
        let mut fields = Vec::new();
        for (field_name, field_type) in field_map {
            if IGNORED_ENTITY_FIELD_TYPES.contains(&field_name.as_str()) {
                continue;
            }

            if let Some(field_def) = Type::new(field_type) {
                let base_field_type = &field_def.base;
                let nullable = field_def.nullable;

                let field_type = match base_field_type {
                    BaseType::Named(type_name) => {
                        if nullable {
                            // TODO: If we do not check for virtual types,
                            // enums become recursively self-referential and the playground
                            // will report errors related to enum subfields not being
                            // supplied.
                            //
                            //
                            // For now, setting them to a String type does not
                            // cause errors, but we should decide what the final process is.
                            if schema.parsed().is_virtual_typedef(field_type) {
                                TypeRef::named(TypeRef::STRING)
                            } else {
                                TypeRef::named(type_name.to_string())
                            }
                        } else if schema.parsed().is_virtual_typedef(field_type) {
                            TypeRef::named_nn(TypeRef::STRING)
                        } else {
                            TypeRef::named_nn(type_name.to_string())
                        }
                    }
                    BaseType::List(list_type) => {
                        let inner_base_type = list_type.base.to_string();
                        let nullable_inner = list_type.nullable;

                        if nullable && nullable_inner {
                            TypeRef::named_list(inner_base_type)
                        } else if nullable && !nullable_inner {
                            TypeRef::named_nn_list(inner_base_type)
                        } else if !nullable && nullable_inner {
                            TypeRef::named_list_nn(inner_base_type)
                        } else {
                            TypeRef::named_nn_list_nn(inner_base_type)
                        }
                    }
                };

                let field = create_field_with_assoc_args(
                    field_name.to_string(),
                    field_type,
                    base_field_type,
                    &filter_tracker,
                    &filter_object_list,
                    &sorter_tracker,
                    &sort_object_list,
                );

                fields.push(field);
            }
        }

        // Create object using all of the fields that were constructed for the entity
        // and repeat the same process in order to allow for introspection-related
        // functionality at the root query level.
        let obj = fields
            .into_iter()
            .fold(Object::new(entity_type.clone()), |obj, f| obj.field(f));

        // Create field for entity object and add it to root level query object.
        let field = create_field_with_assoc_args(
            entity_type.to_string().to_lowercase(),
            TypeRef::named(obj.type_name()),
            &BaseType::Named(Name::new(obj.type_name())),
            &filter_tracker,
            &filter_object_list,
            &sorter_tracker,
            &sort_object_list,
        );
        if !SCALAR_TYPES.contains(&obj.type_name()) {
            query_root = query_root.field(field);
        }

        schema_builder = schema_builder.register(obj).register(object_field_enum);
    }

    // In order for the schema to successfully use the input objects
    // that make up the filter and sort arguments, the objects have to
    // be registered with the schema.
    for filter_obj in filter_object_list {
        schema_builder = schema_builder.register(filter_obj);
    }

    for sort_obj in sort_object_list {
        schema_builder = schema_builder.register(sort_obj);
    }

    for io in input_objects {
        schema_builder = schema_builder.register(io);
    }

    schema_builder = schema_builder.register(sort_enum);
    schema_builder = schema_builder.register(query_root);

    Ok(schema_builder.finish()?)
}

/// Create input values and objects that are used to build introspection information for a field.
fn create_input_values_and_objects_for_field(
    field_name: String,
    field_type: String,
    entity_type: String,
    sort_enum: &Enum,
) -> GraphqlResult<(InputValue, Vec<InputObject>, Option<InputValue>)> {
    let field_type =
        Type::new(&field_type).ok_or(GraphqlError::DynamicSchemaBuildError(
            SchemaError::from("Could not create type defintion from field type string"),
        ))?;

    match field_type.base {
        BaseType::Named(field_type) => {
            let (field_filter_input_val, field_input_objects) =
                create_filter_val_and_objects_for_field(
                    &field_name,
                    field_type.as_str(),
                    entity_type.as_str(),
                );

            if SORTABLE_SCALAR_TYPES.contains(field_type.as_str()) {
                let sort_input_val =
                    InputValue::new(field_name, TypeRef::named(sort_enum.type_name()));
                return Ok((
                    field_filter_input_val,
                    field_input_objects,
                    Some(sort_input_val),
                ));
            }

            Ok((field_filter_input_val, field_input_objects, None))
        }
        // TODO: Do the same as above, but with list type
        BaseType::List(_) => unimplemented!("List types are not currently supported"),
    }
}

fn create_field_with_assoc_args(
    field_name: String,
    field_type_ref: TypeRef,
    base_field_type: &BaseType,
    filter_tracker: &HashMap<String, usize>,
    filter_object_list: &[InputObject],
    sorter_tracker: &HashMap<String, usize>,
    sort_object_list: &[InputObject],
) -> Field {
    // Because the dynamic schema is set to only resolve introspection
    // queries, we set the resolvers to return a dummy value.
    let mut field =
        Field::new(field_name, field_type_ref, move |_ctx: ResolverContext| {
            return FieldFuture::new(async move { Ok(Some(FieldValue::value(1))) });
        });

    match base_field_type {
        BaseType::Named(field_type) => {
            if !SCALAR_TYPES.contains(field_type.as_str()) {
                if let Some(idx) = filter_tracker.get(&field_type.to_string()) {
                    let object_filter_arg = InputValue::new(
                        "filter",
                        TypeRef::named(filter_object_list[*idx].type_name()),
                    );
                    field = field.argument(object_filter_arg)
                }

                if let Some(idx) = sorter_tracker.get(&field_type.to_string()) {
                    let object_sort_arg = InputValue::new(
                        "order",
                        TypeRef::named(sort_object_list[*idx].type_name()),
                    );
                    field = field.argument(object_sort_arg);
                }

                let offset_arg = InputValue::new("offset", TypeRef::named(TypeRef::INT));

                let limit_arg = InputValue::new("first", TypeRef::named(TypeRef::INT));

                let id_selection_arg =
                    InputValue::new("id", TypeRef::named(TypeRef::STRING));

                field = field
                    .argument(offset_arg)
                    .argument(limit_arg)
                    .argument(id_selection_arg);
            }
        }
        BaseType::List(_) => unimplemented!("List types are not currently supported"),
    }

    field
}

/// Build the filter objects for a particular field. The resultant object
/// will ensure that the correct value type is allowed for the field by
/// passing the input type information in the introspection response.
fn create_filter_val_and_objects_for_field<'a>(
    field_name: &'a str,
    field_type: &'a str,
    obj_name: &'a str,
) -> (InputValue, Vec<InputObject>) {
    let mut input_objs: Vec<InputObject> = Vec::new();

    let filter_arg_type = if NUMERIC_SCALAR_TYPES.contains(field_type) {
        TypeRef::INT
    } else {
        TypeRef::STRING
    };

    // TODO: Add support for logical operators -- https://github.com/FuelLabs/fuel-indexer/issues/917

    let complex_comparison_obj =
        InputObject::new(format!("{obj_name}_{field_name}_ComplexComparisonObject"))
            .field(InputValue::new("min", TypeRef::named_nn(filter_arg_type)))
            .field(InputValue::new("max", TypeRef::named_nn(filter_arg_type)));

    let complete_comparison_obj =
        InputObject::new(format!("{obj_name}{field_name}FilterObject"))
            .field(InputValue::new(
                "between",
                TypeRef::named(complex_comparison_obj.type_name()),
            ))
            .field(InputValue::new("equals", TypeRef::named(filter_arg_type)))
            .field(InputValue::new("gt", TypeRef::named(filter_arg_type)))
            .field(InputValue::new("gte", TypeRef::named(filter_arg_type)))
            .field(InputValue::new("lt", TypeRef::named(filter_arg_type)))
            .field(InputValue::new("lte", TypeRef::named(filter_arg_type)))
            .field(InputValue::new(
                "in",
                TypeRef::named_nn_list(filter_arg_type),
            ));

    let input_val_for_field = InputValue::new(
        field_name,
        TypeRef::named(complete_comparison_obj.type_name()),
    );

    input_objs.append(&mut vec![complex_comparison_obj, complete_comparison_obj]);
    (input_val_for_field, input_objs)
}