File size: 26,980 Bytes
b110593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
//  CONTACT: [email protected]
//

package get

import (
	"context"
	"fmt"
	"regexp"
	"strings"

	"github.com/tailor-inc/graphql"
	"github.com/tailor-inc/graphql/language/ast"
	"github.com/weaviate/weaviate/adapters/handlers/graphql/descriptions"
	"github.com/weaviate/weaviate/adapters/handlers/graphql/local/common_filters"
	"github.com/weaviate/weaviate/entities/additional"
	"github.com/weaviate/weaviate/entities/dto"
	enterrors "github.com/weaviate/weaviate/entities/errors"
	"github.com/weaviate/weaviate/entities/filters"
	"github.com/weaviate/weaviate/entities/models"
	"github.com/weaviate/weaviate/entities/modulecapabilities"
	"github.com/weaviate/weaviate/entities/schema"
	"github.com/weaviate/weaviate/entities/search"
	"github.com/weaviate/weaviate/entities/searchparams"
)

func (b *classBuilder) primitiveField(propertyType schema.PropertyDataType,
	property *models.Property, className string,
) *graphql.Field {
	switch propertyType.AsPrimitive() {
	case schema.DataTypeText:
		return &graphql.Field{
			Description: property.Description,
			Name:        property.Name,
			Type:        graphql.String,
		}
	case schema.DataTypeInt:
		return &graphql.Field{
			Description: property.Description,
			Name:        property.Name,
			Type:        graphql.Int,
		}
	case schema.DataTypeNumber:
		return &graphql.Field{
			Description: property.Description,
			Name:        property.Name,
			Type:        graphql.Float,
		}
	case schema.DataTypeBoolean:
		return &graphql.Field{
			Description: property.Description,
			Name:        property.Name,
			Type:        graphql.Boolean,
		}
	case schema.DataTypeDate:
		return &graphql.Field{
			Description: property.Description,
			Name:        property.Name,
			Type:        graphql.String, // String since no graphql date datatype exists
		}
	case schema.DataTypeGeoCoordinates:
		obj := newGeoCoordinatesObject(className, property.Name)

		return &graphql.Field{
			Description: property.Description,
			Name:        property.Name,
			Type:        obj,
			Resolve:     resolveGeoCoordinates,
		}
	case schema.DataTypePhoneNumber:
		obj := newPhoneNumberObject(className, property.Name)

		return &graphql.Field{
			Description: property.Description,
			Name:        property.Name,
			Type:        obj,
			Resolve:     resolvePhoneNumber,
		}
	case schema.DataTypeBlob:
		return &graphql.Field{
			Description: property.Description,
			Name:        property.Name,
			Type:        graphql.String,
		}
	case schema.DataTypeTextArray:
		return &graphql.Field{
			Description: property.Description,
			Name:        property.Name,
			Type:        graphql.NewList(graphql.String),
		}
	case schema.DataTypeIntArray:
		return &graphql.Field{
			Description: property.Description,
			Name:        property.Name,
			Type:        graphql.NewList(graphql.Int),
		}
	case schema.DataTypeNumberArray:
		return &graphql.Field{
			Description: property.Description,
			Name:        property.Name,
			Type:        graphql.NewList(graphql.Float),
		}
	case schema.DataTypeBooleanArray:
		return &graphql.Field{
			Description: property.Description,
			Name:        property.Name,
			Type:        graphql.NewList(graphql.Boolean),
		}
	case schema.DataTypeDateArray:
		return &graphql.Field{
			Description: property.Description,
			Name:        property.Name,
			Type:        graphql.NewList(graphql.String), // String since no graphql date datatype exists
		}
	case schema.DataTypeUUIDArray:
		return &graphql.Field{
			Description: property.Description,
			Name:        property.Name,
			Type:        graphql.NewList(graphql.String), // Always return UUID as string representation to the user
		}
	case schema.DataTypeUUID:
		return &graphql.Field{
			Description: property.Description,
			Name:        property.Name,
			Type:        graphql.String, // Always return UUID as string representation to the user
		}
	default:
		panic(fmt.Sprintf("buildGetClass: unknown primitive type for %s.%s; %s",
			className, property.Name, propertyType.AsPrimitive()))
	}
}

func newGeoCoordinatesObject(className string, propertyName string) *graphql.Object {
	return graphql.NewObject(graphql.ObjectConfig{
		Description: "GeoCoordinates as latitude and longitude in decimal form",
		Name:        fmt.Sprintf("%s%sGeoCoordinatesObj", className, propertyName),
		Fields: graphql.Fields{
			"latitude": &graphql.Field{
				Name:        "Latitude",
				Description: "The Latitude of the point in decimal form.",
				Type:        graphql.Float,
			},
			"longitude": &graphql.Field{
				Name:        "Longitude",
				Description: "The Longitude of the point in decimal form.",
				Type:        graphql.Float,
			},
		},
	})
}

func newPhoneNumberObject(className string, propertyName string) *graphql.Object {
	return graphql.NewObject(graphql.ObjectConfig{
		Description: "PhoneNumber in various parsed formats",
		Name:        fmt.Sprintf("%s%sPhoneNumberObj", className, propertyName),
		Fields: graphql.Fields{
			"input": &graphql.Field{
				Name:        "Input",
				Description: "The raw phone number as put in by the user prior to parsing",
				Type:        graphql.String,
			},
			"internationalFormatted": &graphql.Field{
				Name:        "Input",
				Description: "The parsed phone number in the international format",
				Type:        graphql.String,
			},
			"nationalFormatted": &graphql.Field{
				Name:        "Input",
				Description: "The parsed phone number in the national format",
				Type:        graphql.String,
			},
			"national": &graphql.Field{
				Name:        "Input",
				Description: "The parsed phone number in the national format",
				Type:        graphql.Int,
			},
			"valid": &graphql.Field{
				Name:        "Input",
				Description: "Whether the phone number could be successfully parsed and was considered valid by the parser",
				Type:        graphql.Boolean,
			},
			"countryCode": &graphql.Field{
				Name:        "Input",
				Description: "The parsed country code, i.e. the leading numbers identifing the country in an international format",
				Type:        graphql.Int,
			},
			"defaultCountry": &graphql.Field{
				Name:        "Input",
				Description: "The defaultCountry as put in by the user. (This is used to help parse national numbers into an international format)",
				Type:        graphql.String,
			},
		},
	})
}

func buildGetClassField(classObject *graphql.Object,

	class *models.Class, modulesProvider ModulesProvider, fusionEnum *graphql.Enum,

) graphql.Field {
	field := graphql.Field{
		Type:        graphql.NewList(classObject),
		Description: class.Description,
		Args: graphql.FieldConfigArgument{
			"after": &graphql.ArgumentConfig{
				Description: descriptions.AfterID,
				Type:        graphql.String,
			},
			"limit": &graphql.ArgumentConfig{
				Description: descriptions.Limit,
				Type:        graphql.Int,
			},
			"offset": &graphql.ArgumentConfig{
				Description: descriptions.After,
				Type:        graphql.Int,
			},
			"autocut": &graphql.ArgumentConfig{
				Description: "Cut off number of results after the Nth extrema. Off by default, negative numbers mean off.",
				Type:        graphql.Int,
			},

			"sort":       sortArgument(class.Class),
			"nearVector": nearVectorArgument(class.Class),
			"nearObject": nearObjectArgument(class.Class),
			"where":      whereArgument(class.Class),
			"group":      groupArgument(class.Class),
			"groupBy":    groupByArgument(class.Class),
		},
		Resolve: newResolver(modulesProvider).makeResolveGetClass(class.Class),
	}

	field.Args["bm25"] = bm25Argument(class.Class)
	field.Args["hybrid"] = hybridArgument(classObject, class, modulesProvider, fusionEnum)

	if modulesProvider != nil {
		for name, argument := range modulesProvider.GetArguments(class) {
			field.Args[name] = argument
		}
	}

	if replicationEnabled(class) {
		field.Args["consistencyLevel"] = consistencyLevelArgument(class)
	}

	if schema.MultiTenancyEnabled(class) {
		field.Args["tenant"] = tenantArgument()
	}

	return field
}

func resolveGeoCoordinates(p graphql.ResolveParams) (interface{}, error) {
	field := p.Source.(map[string]interface{})[p.Info.FieldName]
	if field == nil {
		return nil, nil
	}

	geo, ok := field.(*models.GeoCoordinates)
	if !ok {
		return nil, fmt.Errorf("expected a *models.GeoCoordinates, but got: %T", field)
	}

	return map[string]interface{}{
		"latitude":  geo.Latitude,
		"longitude": geo.Longitude,
	}, nil
}

func resolvePhoneNumber(p graphql.ResolveParams) (interface{}, error) {
	field := p.Source.(map[string]interface{})[p.Info.FieldName]
	if field == nil {
		return nil, nil
	}

	phone, ok := field.(*models.PhoneNumber)
	if !ok {
		return nil, fmt.Errorf("expected a *models.PhoneNumber, but got: %T", field)
	}

	return map[string]interface{}{
		"input":                  phone.Input,
		"internationalFormatted": phone.InternationalFormatted,
		"nationalFormatted":      phone.NationalFormatted,
		"national":               phone.National,
		"valid":                  phone.Valid,
		"countryCode":            phone.CountryCode,
		"defaultCountry":         phone.DefaultCountry,
	}, nil
}

func whereArgument(className string) *graphql.ArgumentConfig {
	return &graphql.ArgumentConfig{
		Description: descriptions.GetWhere,
		Type: graphql.NewInputObject(
			graphql.InputObjectConfig{
				Name:        fmt.Sprintf("GetObjects%sWhereInpObj", className),
				Fields:      common_filters.BuildNew(fmt.Sprintf("GetObjects%s", className)),
				Description: descriptions.GetWhereInpObj,
			},
		),
	}
}

type resolver struct {
	modulesProvider ModulesProvider
}

func newResolver(modulesProvider ModulesProvider) *resolver {
	return &resolver{modulesProvider}
}

func (r *resolver) makeResolveGetClass(className string) graphql.FieldResolveFn {
	return func(p graphql.ResolveParams) (interface{}, error) {
		result, err := r.resolveGet(p, className)
		if err != nil {
			return result, enterrors.NewErrGraphQLUser(err, "Get", className)
		}
		return result, nil
	}
}

func (r *resolver) resolveGet(p graphql.ResolveParams, className string) (interface{}, error) {
	source, ok := p.Source.(map[string]interface{})
	if !ok {
		return nil, fmt.Errorf("expected graphql root to be a map, but was %T", p.Source)
	}

	resolver, ok := source["Resolver"].(Resolver)
	if !ok {
		return nil, fmt.Errorf("expected source map to have a usable Resolver, but got %#v", source["Resolver"])
	}

	pagination, err := filters.ExtractPaginationFromArgs(p.Args)
	if err != nil {
		return nil, err
	}

	cursor, err := filters.ExtractCursorFromArgs(p.Args)
	if err != nil {
		return nil, err
	}

	// There can only be exactly one ast.Field; it is the class name.
	if len(p.Info.FieldASTs) != 1 {
		panic("Only one Field expected here")
	}

	selectionsOfClass := p.Info.FieldASTs[0].SelectionSet

	properties, addlProps, err := extractProperties(
		className, selectionsOfClass, p.Info.Fragments, r.modulesProvider)
	if err != nil {
		return nil, err
	}

	var sort []filters.Sort
	if sortArg, ok := p.Args["sort"]; ok {
		sort = filters.ExtractSortFromArgs(sortArg.([]interface{}))
	}

	filters, err := common_filters.ExtractFilters(p.Args, p.Info.FieldName)
	if err != nil {
		return nil, fmt.Errorf("could not extract filters: %s", err)
	}

	var nearVectorParams *searchparams.NearVector
	if nearVector, ok := p.Args["nearVector"]; ok {
		p, err := common_filters.ExtractNearVector(nearVector.(map[string]interface{}))
		if err != nil {
			return nil, fmt.Errorf("failed to extract nearVector params: %s", err)
		}
		nearVectorParams = &p
	}

	var nearObjectParams *searchparams.NearObject
	if nearObject, ok := p.Args["nearObject"]; ok {
		p, err := common_filters.ExtractNearObject(nearObject.(map[string]interface{}))
		if err != nil {
			return nil, fmt.Errorf("failed to extract nearObject params: %s", err)
		}
		nearObjectParams = &p
	}

	var moduleParams map[string]interface{}
	if r.modulesProvider != nil {
		extractedParams := r.modulesProvider.ExtractSearchParams(p.Args, className)
		if len(extractedParams) > 0 {
			moduleParams = extractedParams
		}
	}

	// extracts bm25 (sparseSearch) from the query
	var keywordRankingParams *searchparams.KeywordRanking
	if bm25, ok := p.Args["bm25"]; ok {
		if len(sort) > 0 {
			return nil, fmt.Errorf("bm25 search is not compatible with sort")
		}
		p := common_filters.ExtractBM25(bm25.(map[string]interface{}), addlProps.ExplainScore)
		keywordRankingParams = &p
	}

	// Extract hybrid search params from the processed query
	// Everything hybrid can go in another namespace AFTER modulesprovider is
	// refactored
	var hybridParams *searchparams.HybridSearch
	if hybrid, ok := p.Args["hybrid"]; ok {
		if len(sort) > 0 {
			return nil, fmt.Errorf("hybrid search is not compatible with sort")
		}
		p, err := common_filters.ExtractHybridSearch(hybrid.(map[string]interface{}), addlProps.ExplainScore)
		if err != nil {
			return nil, fmt.Errorf("failed to extract hybrid params: %w", err)
		}
		hybridParams = p
	}

	var replProps *additional.ReplicationProperties
	if cl, ok := p.Args["consistencyLevel"]; ok {
		replProps = &additional.ReplicationProperties{
			ConsistencyLevel: cl.(string),
		}
	}

	group := extractGroup(p.Args)

	var groupByParams *searchparams.GroupBy
	if groupBy, ok := p.Args["groupBy"]; ok {
		p := common_filters.ExtractGroupBy(groupBy.(map[string]interface{}))
		groupByParams = &p
	}

	var tenant string
	if tk, ok := p.Args["tenant"]; ok {
		tenant = tk.(string)
	}

	params := dto.GetParams{
		Filters:               filters,
		ClassName:             className,
		Pagination:            pagination,
		Cursor:                cursor,
		Properties:            properties,
		Sort:                  sort,
		NearVector:            nearVectorParams,
		NearObject:            nearObjectParams,
		Group:                 group,
		ModuleParams:          moduleParams,
		AdditionalProperties:  addlProps,
		KeywordRanking:        keywordRankingParams,
		HybridSearch:          hybridParams,
		ReplicationProperties: replProps,
		GroupBy:               groupByParams,
		Tenant:                tenant,
	}

	// need to perform vector search by distance
	// under certain conditions
	setLimitBasedOnVectorSearchParams(&params)

	return func() (interface{}, error) {
		result, err := resolver.GetClass(p.Context, principalFromContext(p.Context), params)
		if err != nil {
			return result, enterrors.NewErrGraphQLUser(err, "Get", params.ClassName)
		}
		return result, nil
	}, nil
}

// the limit needs to be set according to the vector search parameters.
// for example, if a certainty is provided by any of the near* options,
// and no limit was provided, weaviate will want to execute a vector
// search by distance. it knows to do this by watching for a limit
// flag, specifically filters.LimitFlagSearchByDistance
func setLimitBasedOnVectorSearchParams(params *dto.GetParams) {
	setLimit := func(params *dto.GetParams) {
		if params.Pagination == nil {
			// limit was omitted entirely, implicitly
			// indicating to do unlimited search
			params.Pagination = &filters.Pagination{
				Limit: filters.LimitFlagSearchByDist,
			}
		} else if params.Pagination.Limit < 0 {
			// a negative limit was set, explicitly
			// indicating to do unlimited search
			params.Pagination.Limit = filters.LimitFlagSearchByDist
		}
	}

	if params.NearVector != nil &&
		(params.NearVector.Certainty != 0 || params.NearVector.WithDistance) {
		setLimit(params)
		return
	}

	if params.NearObject != nil &&
		(params.NearObject.Certainty != 0 || params.NearObject.WithDistance) {
		setLimit(params)
		return
	}

	for _, param := range params.ModuleParams {
		nearParam, ok := param.(modulecapabilities.NearParam)
		if ok && nearParam.SimilarityMetricProvided() {
			setLimit(params)
			return
		}
	}
}

func extractGroup(args map[string]interface{}) *dto.GroupParams {
	group, ok := args["group"]
	if !ok {
		return nil
	}

	asMap := group.(map[string]interface{}) // guaranteed by graphql
	strategy := asMap["type"].(string)
	force := asMap["force"].(float64)
	return &dto.GroupParams{
		Strategy: strategy,
		Force:    float32(force),
	}
}

func principalFromContext(ctx context.Context) *models.Principal {
	principal := ctx.Value("principal")
	if principal == nil {
		return nil
	}

	return principal.(*models.Principal)
}

func isPrimitive(selectionSet *ast.SelectionSet) bool {
	if selectionSet == nil {
		return true
	}

	// if there is a selection set it could either be a cross-ref or a map-type
	// field like GeoCoordinates or PhoneNumber
	for _, subSelection := range selectionSet.Selections {
		if subsectionField, ok := subSelection.(*ast.Field); ok {
			if fieldNameIsOfObjectButNonReferenceType(subsectionField.Name.Value) {
				return true
			}
		}
	}

	// must be a ref field
	return false
}

type additionalCheck struct {
	modulesProvider ModulesProvider
}

func (ac *additionalCheck) isAdditional(parentName, name string) bool {
	if parentName == "_additional" {
		if name == "classification" || name == "certainty" ||
			name == "distance" || name == "id" || name == "vector" ||
			name == "creationTimeUnix" || name == "lastUpdateTimeUnix" ||
			name == "score" || name == "explainScore" || name == "isConsistent" ||
			name == "group" {
			return true
		}
		if ac.isModuleAdditional(name) {
			return true
		}
	}
	return false
}

func (ac *additionalCheck) isModuleAdditional(name string) bool {
	if ac.modulesProvider != nil {
		if len(ac.modulesProvider.GraphQLAdditionalFieldNames()) > 0 {
			for _, moduleAdditionalProperty := range ac.modulesProvider.GraphQLAdditionalFieldNames() {
				if name == moduleAdditionalProperty {
					return true
				}
			}
		}
	}
	return false
}

func fieldNameIsOfObjectButNonReferenceType(field string) bool {
	switch field {
	case "latitude", "longitude":
		// must be a geo prop
		return true
	case "input", "internationalFormatted", "nationalFormatted", "national",
		"valid", "countryCode", "defaultCountry":
		// must be a phone number
		return true
	default:
		return false
	}
}

func extractProperties(className string, selections *ast.SelectionSet,

	fragments map[string]ast.Definition,

	modulesProvider ModulesProvider,

) ([]search.SelectProperty, additional.Properties, error) {
	var properties []search.SelectProperty
	var additionalProps additional.Properties
	additionalCheck := &additionalCheck{modulesProvider}

	for _, selection := range selections.Selections {
		field := selection.(*ast.Field)
		name := field.Name.Value
		property := search.SelectProperty{Name: name}

		property.IsPrimitive = isPrimitive(field.SelectionSet)
		if !property.IsPrimitive {
			// We can interpret this property in different ways
			for _, subSelection := range field.SelectionSet.Selections {
				switch s := subSelection.(type) {
				case *ast.Field:
					// Is it a field with the name __typename?
					if s.Name.Value == "__typename" {
						property.IncludeTypeName = true
						continue
					} else if additionalCheck.isAdditional(name, s.Name.Value) {
						additionalProperty := s.Name.Value
						if additionalProperty == "classification" {
							additionalProps.Classification = true
							continue
						}
						if additionalProperty == "certainty" {
							additionalProps.Certainty = true
							continue
						}
						if additionalProperty == "distance" {
							additionalProps.Distance = true
							continue
						}
						if additionalProperty == "id" {
							additionalProps.ID = true
							continue
						}
						if additionalProperty == "vector" {
							additionalProps.Vector = true
							continue
						}
						if additionalProperty == "creationTimeUnix" {
							additionalProps.CreationTimeUnix = true
							continue
						}
						if additionalProperty == "score" {
							additionalProps.Score = true
							continue
						}
						if additionalProperty == "explainScore" {
							additionalProps.ExplainScore = true
							continue
						}
						if additionalProperty == "lastUpdateTimeUnix" {
							additionalProps.LastUpdateTimeUnix = true
							continue
						}
						if additionalProperty == "isConsistent" {
							additionalProps.IsConsistent = true
							continue
						}
						if additionalProperty == "group" {
							additionalProps.Group = true
							additionalGroupHitProperties, err := extractGroupHitProperties(className, additionalProps, subSelection, fragments, modulesProvider)
							if err != nil {
								return nil, additionalProps, err
							}
							properties = append(properties, additionalGroupHitProperties...)
							continue
						}
						if modulesProvider != nil {
							if additionalCheck.isModuleAdditional(additionalProperty) {
								additionalProps.ModuleParams = getModuleParams(additionalProps.ModuleParams)
								additionalProps.ModuleParams[additionalProperty] = modulesProvider.ExtractAdditionalField(className, additionalProperty, s.Arguments)
								continue
							}
						}
					} else {
						// It's an object / object array property
						continue
					}

				case *ast.FragmentSpread:
					ref, err := extractFragmentSpread(className, s, fragments, modulesProvider)
					if err != nil {
						return nil, additionalProps, err
					}

					property.Refs = append(property.Refs, ref)

				case *ast.InlineFragment:
					ref, err := extractInlineFragment(className, s, fragments, modulesProvider)
					if err != nil {
						return nil, additionalProps, err
					}

					property.Refs = append(property.Refs, ref)

				default:
					return nil, additionalProps, fmt.Errorf("unrecoginzed type in subs-selection: %T", subSelection)
				}
			}
		}

		if name == "_additional" {
			continue
		}

		properties = append(properties, property)
	}

	return properties, additionalProps, nil
}

func extractGroupHitProperties(

	className string,

	additionalProps additional.Properties,

	subSelection ast.Selection,

	fragments map[string]ast.Definition,

	modulesProvider ModulesProvider,

) ([]search.SelectProperty, error) {
	additionalGroupProperties := []search.SelectProperty{}
	if subSelection != nil {
		if selectionSet := subSelection.GetSelectionSet(); selectionSet != nil {
			for _, groupSubSelection := range selectionSet.Selections {
				if groupSubSelection != nil {
					if groupSubSelectionField, ok := groupSubSelection.(*ast.Field); ok {
						if groupSubSelectionField.Name.Value == "hits" && groupSubSelectionField.SelectionSet != nil {
							for _, groupHitsSubSelection := range groupSubSelectionField.SelectionSet.Selections {
								if hf, ok := groupHitsSubSelection.(*ast.Field); ok {
									if hf.SelectionSet != nil {
										for _, ss := range hf.SelectionSet.Selections {
											if inlineFrag, ok := ss.(*ast.InlineFragment); ok {
												ref, err := extractInlineFragment(className, inlineFrag, fragments, modulesProvider)
												if err != nil {
													return nil, err
												}

												additionalGroupHitProp := search.SelectProperty{Name: fmt.Sprintf("_additional:group:hits:%v", hf.Name.Value)}
												additionalGroupHitProp.Refs = append(additionalGroupHitProp.Refs, ref)
												additionalGroupProperties = append(additionalGroupProperties, additionalGroupHitProp)
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	return additionalGroupProperties, nil
}

func getModuleParams(moduleParams map[string]interface{}) map[string]interface{} {
	if moduleParams == nil {
		return map[string]interface{}{}
	}
	return moduleParams
}

func extractInlineFragment(class string, fragment *ast.InlineFragment,

	fragments map[string]ast.Definition,

	modulesProvider ModulesProvider,

) (search.SelectClass, error) {
	var className schema.ClassName
	var err error
	var result search.SelectClass

	if strings.Contains(fragment.TypeCondition.Name.Value, "__") {
		// is a helper type for a network ref
		// don't validate anything as of now
		className = schema.ClassName(fragment.TypeCondition.Name.Value)
	} else {
		className, err = schema.ValidateClassName(fragment.TypeCondition.Name.Value)
		if err != nil {
			return result, fmt.Errorf("the inline fragment type name '%s' is not a valid class name", fragment.TypeCondition.Name.Value)
		}
	}

	if className == "Beacon" {
		return result, fmt.Errorf("retrieving cross-refs by beacon is not supported yet - coming soon!")
	}

	subProperties, additionalProperties, err := extractProperties(class, fragment.SelectionSet, fragments, modulesProvider)
	if err != nil {
		return result, err
	}

	result.ClassName = string(className)
	result.RefProperties = subProperties
	result.AdditionalProperties = additionalProperties
	return result, nil
}

func extractFragmentSpread(class string, spread *ast.FragmentSpread,

	fragments map[string]ast.Definition,

	modulesProvider ModulesProvider,

) (search.SelectClass, error) {
	var result search.SelectClass
	name := spread.Name.Value

	def, ok := fragments[name]
	if !ok {
		return result, fmt.Errorf("spread fragment '%s' refers to unknown fragment", name)
	}

	className, err := hackyWorkaroundToExtractClassName(def, name)
	if err != nil {
		return result, err
	}

	subProperties, additionalProperties, err := extractProperties(class, def.GetSelectionSet(), fragments, modulesProvider)
	if err != nil {
		return result, err
	}

	result.ClassName = string(className)
	result.RefProperties = subProperties
	result.AdditionalProperties = additionalProperties
	return result, nil
}

// It seems there's no proper way to extract this info unfortunately:
// https://github.com/tailor-inc/graphql/issues/455
func hackyWorkaroundToExtractClassName(def ast.Definition, name string) (string, error) {
	loc := def.GetLoc()
	raw := loc.Source.Body[loc.Start:loc.End]
	r := regexp.MustCompile(fmt.Sprintf(`fragment\s*%s\s*on\s*(\w*)\s*{`, name))
	matches := r.FindSubmatch(raw)
	if len(matches) < 2 {
		return "", fmt.Errorf("could not extract a className from fragment")
	}

	return string(matches[1]), nil
}