-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathValidation.cpp
More file actions
2136 lines (1687 loc) · 56.2 KB
/
Validation.cpp
File metadata and controls
2136 lines (1687 loc) · 56.2 KB
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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "Validation.h"
#include "graphqlservice/internal/Base64.h"
#include "graphqlservice/internal/Grammar.h"
#include "graphqlservice/introspection/IntrospectionSchema.h"
#include <algorithm>
#include <iostream>
#include <iterator>
#include <stdexcept>
using namespace std::literals;
namespace graphql::service {
SharedType getSharedType(const ValidateType& type) noexcept
{
return type ? type->get().shared_from_this() : SharedType {};
}
ValidateType getValidateType(const SharedType& type) noexcept
{
return type ? std::make_optional(std::cref(*type)) : std::nullopt;
}
bool operator==(const ValidateType& lhs, const ValidateType& rhs) noexcept
{
// Equal if they're either both std::nullopt or they are both not empty and the addresses of the
// references match.
return (lhs ? (rhs && &lhs->get() == &rhs->get()) : !rhs);
}
bool ValidateArgumentVariable::operator==(const ValidateArgumentVariable& other) const
{
return name == other.name;
}
bool ValidateArgumentEnumValue::operator==(const ValidateArgumentEnumValue& other) const
{
return value == other.value;
}
bool ValidateArgumentValuePtr::operator==(const ValidateArgumentValuePtr& other) const
{
return (!value ? !other.value : (other.value && value->data == other.value->data));
}
bool ValidateArgumentList::operator==(const ValidateArgumentList& other) const
{
return values == other.values;
}
bool ValidateArgumentMap::operator==(const ValidateArgumentMap& other) const
{
return values == other.values;
}
ValidateArgumentValue::ValidateArgumentValue(ValidateArgumentVariable&& value)
: data(std::move(value))
{
}
ValidateArgumentValue::ValidateArgumentValue(int value)
: data(value)
{
}
ValidateArgumentValue::ValidateArgumentValue(double value)
: data(value)
{
}
ValidateArgumentValue::ValidateArgumentValue(std::string_view value)
: data(std::move(value))
{
}
ValidateArgumentValue::ValidateArgumentValue(bool value)
: data(value)
{
}
ValidateArgumentValue::ValidateArgumentValue(ValidateArgumentEnumValue&& value)
: data(std::move(value))
{
}
ValidateArgumentValue::ValidateArgumentValue(ValidateArgumentList&& value)
: data(std::move(value))
{
}
ValidateArgumentValue::ValidateArgumentValue(ValidateArgumentMap&& value)
: data(std::move(value))
{
}
ValidateArgumentValueVisitor::ValidateArgumentValueVisitor(std::list<schema_error>& errors)
: _errors(errors)
{
}
ValidateArgumentValuePtr ValidateArgumentValueVisitor::getArgumentValue()
{
auto result = std::move(_argumentValue);
return result;
}
void ValidateArgumentValueVisitor::visit(const peg::ast_node& value)
{
if (value.is_type<peg::variable_value>())
{
visitVariable(value);
}
else if (value.is_type<peg::integer_value>())
{
visitIntValue(value);
}
else if (value.is_type<peg::float_value>())
{
visitFloatValue(value);
}
else if (value.is_type<peg::string_value>())
{
visitStringValue(value);
}
else if (value.is_type<peg::true_keyword>() || value.is_type<peg::false_keyword>())
{
visitBooleanValue(value);
}
else if (value.is_type<peg::null_keyword>())
{
visitNullValue(value);
}
else if (value.is_type<peg::enum_value>())
{
visitEnumValue(value);
}
else if (value.is_type<peg::list_value>())
{
visitListValue(value);
}
else if (value.is_type<peg::object_value>())
{
visitObjectValue(value);
}
}
void ValidateArgumentValueVisitor::visitVariable(const peg::ast_node& variable)
{
ValidateArgumentVariable value { variable.string_view().substr(1) };
auto position = variable.begin();
_argumentValue.value = std::make_unique<ValidateArgumentValue>(std::move(value));
_argumentValue.position = { position.line, position.column };
}
void ValidateArgumentValueVisitor::visitIntValue(const peg::ast_node& intValue)
{
int value { std::atoi(intValue.string().c_str()) };
auto position = intValue.begin();
_argumentValue.value = std::make_unique<ValidateArgumentValue>(value);
_argumentValue.position = { position.line, position.column };
}
void ValidateArgumentValueVisitor::visitFloatValue(const peg::ast_node& floatValue)
{
double value { std::atof(floatValue.string().c_str()) };
auto position = floatValue.begin();
_argumentValue.value = std::make_unique<ValidateArgumentValue>(value);
_argumentValue.position = { position.line, position.column };
}
void ValidateArgumentValueVisitor::visitStringValue(const peg::ast_node& stringValue)
{
std::string_view value { stringValue.unescaped_view() };
auto position = stringValue.begin();
_argumentValue.value = std::make_unique<ValidateArgumentValue>(value);
_argumentValue.position = { position.line, position.column };
}
void ValidateArgumentValueVisitor::visitBooleanValue(const peg::ast_node& booleanValue)
{
bool value { booleanValue.is_type<peg::true_keyword>() };
auto position = booleanValue.begin();
_argumentValue.value = std::make_unique<ValidateArgumentValue>(value);
_argumentValue.position = { position.line, position.column };
}
void ValidateArgumentValueVisitor::visitNullValue(const peg::ast_node& nullValue)
{
auto position = nullValue.begin();
_argumentValue.value.reset();
_argumentValue.position = { position.line, position.column };
}
void ValidateArgumentValueVisitor::visitEnumValue(const peg::ast_node& enumValue)
{
ValidateArgumentEnumValue value { enumValue.string_view() };
auto position = enumValue.begin();
_argumentValue.value = std::make_unique<ValidateArgumentValue>(std::move(value));
_argumentValue.position = { position.line, position.column };
}
void ValidateArgumentValueVisitor::visitListValue(const peg::ast_node& listValue)
{
ValidateArgumentList value;
auto position = listValue.begin();
value.values.reserve(listValue.children.size());
for (const auto& child : listValue.children)
{
ValidateArgumentValueVisitor visitor(_errors);
visitor.visit(*child);
value.values.emplace_back(visitor.getArgumentValue());
}
_argumentValue.value = std::make_unique<ValidateArgumentValue>(std::move(value));
_argumentValue.position = { position.line, position.column };
}
void ValidateArgumentValueVisitor::visitObjectValue(const peg::ast_node& objectValue)
{
ValidateArgumentMap value;
auto position = objectValue.begin();
for (const auto& field : objectValue.children)
{
auto name = field->children.front()->string_view();
if (value.values.find(name) != value.values.end())
{
// https://spec.graphql.org/October2021/#sec-Input-Object-Field-Uniqueness
auto fieldPosition = field->begin();
std::ostringstream message;
message << "Conflicting input field name: " << name;
_errors.push_back({ message.str(), { fieldPosition.line, fieldPosition.column } });
continue;
}
ValidateArgumentValueVisitor visitor(_errors);
visitor.visit(*field->children.back());
value.values[std::move(name)] = visitor.getArgumentValue();
}
_argumentValue.value = std::make_unique<ValidateArgumentValue>(std::move(value));
_argumentValue.position = { position.line, position.column };
}
ValidateField::ValidateField(ValidateType&& returnType, ValidateType&& objectType,
std::string_view fieldName, ValidateFieldArguments&& arguments)
: returnType(std::move(returnType))
, objectType(std::move(objectType))
, fieldName(fieldName)
, arguments(std::move(arguments))
{
}
bool ValidateField::operator==(const ValidateField& other) const
{
return (returnType == other.returnType)
&& ((objectType && other.objectType && &objectType->get() != &other.objectType->get())
|| (fieldName == other.fieldName && arguments == other.arguments));
}
ValidateVariableTypeVisitor::ValidateVariableTypeVisitor(
const std::shared_ptr<schema::Schema>& schema, const ValidateTypes& types)
: _schema(schema)
, _types(types)
{
}
void ValidateVariableTypeVisitor::visit(const peg::ast_node& typeName)
{
if (typeName.is_type<peg::nonnull_type>())
{
visitNonNullType(typeName);
}
else if (typeName.is_type<peg::list_type>())
{
visitListType(typeName);
}
else if (typeName.is_type<peg::named_type>())
{
visitNamedType(typeName);
}
}
void ValidateVariableTypeVisitor::visitNamedType(const peg::ast_node& namedType)
{
auto name = namedType.string_view();
auto itrType = _types.find(name);
if (itrType == _types.end())
{
return;
}
switch (itrType->second->get().kind())
{
case introspection::TypeKind::SCALAR:
case introspection::TypeKind::ENUM:
case introspection::TypeKind::INPUT_OBJECT:
_isInputType = true;
_variableType = getValidateType(_schema->LookupType(name));
break;
default:
break;
}
}
void ValidateVariableTypeVisitor::visitListType(const peg::ast_node& listType)
{
ValidateVariableTypeVisitor visitor(_schema, _types);
visitor.visit(*listType.children.front());
_isInputType = visitor.isInputType();
_variableType = getValidateType(
_schema->WrapType(introspection::TypeKind::LIST, getSharedType(visitor.getType())));
}
void ValidateVariableTypeVisitor::visitNonNullType(const peg::ast_node& nonNullType)
{
ValidateVariableTypeVisitor visitor(_schema, _types);
visitor.visit(*nonNullType.children.front());
_isInputType = visitor.isInputType();
_variableType = getValidateType(
_schema->WrapType(introspection::TypeKind::NON_NULL, getSharedType(visitor.getType())));
}
bool ValidateVariableTypeVisitor::isInputType() const
{
return _isInputType;
}
ValidateType ValidateVariableTypeVisitor::getType()
{
auto result = std::move(_variableType);
return result;
}
ValidateExecutableVisitor::ValidateExecutableVisitor(std::shared_ptr<schema::Schema> schema)
: _schema(schema)
{
const auto& queryType = _schema->queryType();
const auto& mutationType = _schema->mutationType();
const auto& subscriptionType = _schema->subscriptionType();
_operationTypes.reserve(3);
if (mutationType)
{
_operationTypes[strMutation] = getValidateType(mutationType);
}
if (queryType)
{
_operationTypes[strQuery] = getValidateType(queryType);
}
if (subscriptionType)
{
_operationTypes[strSubscription] = getValidateType(subscriptionType);
}
const auto& types = _schema->types();
_types.reserve(types.size());
for (const auto& entry : types)
{
const auto name = entry.first;
const auto kind = entry.second->kind();
if (!isScalarType(kind))
{
auto matchingTypes = std::move(_matchingTypes[name]);
if (kind == introspection::TypeKind::OBJECT)
{
matchingTypes.emplace(name);
}
else
{
const auto& possibleTypes = entry.second->possibleTypes();
if (kind == introspection::TypeKind::INTERFACE)
{
matchingTypes.reserve(possibleTypes.size() + 1);
matchingTypes.emplace(name);
}
else
{
matchingTypes.reserve(possibleTypes.size());
}
for (const auto& possibleType : possibleTypes)
{
const auto spType = possibleType.lock();
if (spType)
{
matchingTypes.emplace(spType->name());
}
}
}
if (!matchingTypes.empty())
{
_matchingTypes[name] = std::move(matchingTypes);
}
}
else if (kind == introspection::TypeKind::ENUM)
{
const auto& enumValues = entry.second->enumValues();
internal::string_view_set values;
values.reserve(enumValues.size());
for (const auto& value : enumValues)
{
if (value)
{
values.emplace(value->name());
}
}
if (!enumValues.empty())
{
_enumValues[name] = std::move(values);
}
}
else if (kind == introspection::TypeKind::SCALAR)
{
_scalarTypes.emplace(name);
}
_types[name] = getValidateType(entry.second);
}
const auto& directives = _schema->directives();
_directives.reserve(directives.size());
for (const auto& directive : directives)
{
const auto name = directive->name();
const auto& locations = directive->locations();
const auto& args = directive->args();
ValidateDirective validateDirective;
validateDirective.isRepeatable = directive->isRepeatable();
for (const auto location : locations)
{
validateDirective.locations.emplace(location);
}
validateDirective.arguments = getArguments(args);
_directives[name] = std::move(validateDirective);
}
}
void ValidateExecutableVisitor::visit(const peg::ast_node& root)
{
// Visit all of the fragment definitions and check for duplicates.
peg::for_each_child<peg::fragment_definition>(root,
[this](const peg::ast_node& fragmentDefinition) {
const auto& fragmentName = fragmentDefinition.children.front();
const auto inserted =
_fragmentDefinitions.emplace(fragmentName->string_view(), fragmentDefinition);
if (!inserted.second)
{
// https://spec.graphql.org/October2021/#sec-Fragment-Name-Uniqueness
auto position = fragmentDefinition.begin();
std::ostringstream error;
error << "Duplicate fragment name: " << inserted.first->first;
_errors.push_back({ error.str(), { position.line, position.column } });
}
});
// Visit all of the operation definitions and check for duplicates.
peg::for_each_child<peg::operation_definition>(root,
[this](const peg::ast_node& operationDefinition) {
std::string_view operationName;
peg::on_first_child<peg::operation_name>(operationDefinition,
[&operationName](const peg::ast_node& child) {
operationName = child.string_view();
});
const auto inserted = _operationDefinitions.emplace(operationName, operationDefinition);
if (!inserted.second)
{
// https://spec.graphql.org/October2021/#sec-Operation-Name-Uniqueness
auto position = operationDefinition.begin();
std::ostringstream error;
error << "Duplicate operation name: " << inserted.first->first;
_errors.push_back({ error.str(), { position.line, position.column } });
}
});
// Check for lone anonymous operations.
if (_operationDefinitions.size() > 1)
{
auto itr = std::find_if(_operationDefinitions.begin(),
_operationDefinitions.end(),
[](const auto& entry) noexcept {
return entry.first.empty();
});
if (itr != _operationDefinitions.end())
{
// https://spec.graphql.org/October2021/#sec-Lone-Anonymous-Operation
auto position = itr->second.get().begin();
_errors.push_back(
{ "Anonymous operation not alone", { position.line, position.column } });
}
}
// Visit the executable definitions recursively.
for (const auto& child : root.children)
{
if (child->is_type<peg::fragment_definition>())
{
visitFragmentDefinition(*child);
}
else if (child->is_type<peg::operation_definition>())
{
visitOperationDefinition(*child);
}
else
{
// https://spec.graphql.org/October2021/#sec-Executable-Definitions
auto position = child->begin();
_errors.push_back({ "Unexpected type definition", { position.line, position.column } });
}
}
if (!_fragmentDefinitions.empty())
{
// https://spec.graphql.org/October2021/#sec-Fragments-Must-Be-Used
auto unreferencedFragments = std::move(_fragmentDefinitions);
for (const auto& name : _referencedFragments)
{
unreferencedFragments.erase(name);
}
std::transform(unreferencedFragments.begin(),
unreferencedFragments.end(),
std::back_inserter(_errors),
[](const auto& fragmentDefinition) noexcept {
auto position = fragmentDefinition.second.get().begin();
std::ostringstream message;
message << "Unused fragment definition name: " << fragmentDefinition.first;
return schema_error { message.str(), { position.line, position.column } };
});
}
}
std::list<schema_error> ValidateExecutableVisitor::getStructuredErrors()
{
auto errors = std::move(_errors);
// Reset all of the state for this query, but keep the Introspection schema information.
_fragmentDefinitions.clear();
_operationDefinitions.clear();
_referencedFragments.clear();
_fragmentCycles.clear();
return errors;
}
void ValidateExecutableVisitor::visitFragmentDefinition(const peg::ast_node& fragmentDefinition)
{
peg::on_first_child<peg::directives>(fragmentDefinition, [this](const peg::ast_node& child) {
visitDirectives(introspection::DirectiveLocation::FRAGMENT_DEFINITION, child);
});
const auto name = fragmentDefinition.children.front()->string_view();
const auto& selection = *fragmentDefinition.children.back();
const auto& typeCondition = fragmentDefinition.children[1];
auto innerType = typeCondition->children.front()->string_view();
auto itrType = _types.find(innerType);
if (itrType == _types.end() || isScalarType(itrType->second->get().kind()))
{
// https://spec.graphql.org/October2021/#sec-Fragment-Spread-Type-Existence
// https://spec.graphql.org/October2021/#sec-Fragments-On-Composite-Types
auto position = typeCondition->begin();
std::ostringstream message;
message << (itrType == _types.end() ? "Undefined target type on fragment definition: "
: "Scalar target type on fragment definition: ")
<< name << " name: " << innerType;
_errors.push_back({ message.str(), { position.line, position.column } });
return;
}
_fragmentStack.emplace(name);
_scopedType = itrType->second;
visitSelection(selection);
_scopedType.reset();
_fragmentStack.clear();
_selectionFields.clear();
}
void ValidateExecutableVisitor::visitOperationDefinition(const peg::ast_node& operationDefinition)
{
auto operationType = strQuery;
peg::on_first_child<peg::operation_type>(operationDefinition,
[&operationType](const peg::ast_node& child) {
operationType = child.string_view();
});
std::string_view operationName;
peg::on_first_child<peg::operation_name>(operationDefinition,
[&operationName](const peg::ast_node& child) {
operationName = child.string_view();
});
_operationVariables = std::make_optional<VariableTypes>();
peg::for_each_child<peg::variable>(operationDefinition,
[this, operationName](const peg::ast_node& variable) {
std::string_view variableName;
ValidateArgument variableArgument;
for (const auto& child : variable.children)
{
if (child->is_type<peg::variable_name>())
{
// Skip the $ prefix
variableName = child->string_view().substr(1);
if (_operationVariables->find(variableName) != _operationVariables->end())
{
// https://spec.graphql.org/October2021/#sec-Variable-Uniqueness
auto position = child->begin();
std::ostringstream message;
message << "Conflicting variable";
if (!operationName.empty())
{
message << " operation: " << operationName;
}
message << " name: " << variableName;
_errors.push_back({ message.str(), { position.line, position.column } });
return;
}
}
else if (child->is_type<peg::named_type>() || child->is_type<peg::list_type>()
|| child->is_type<peg::nonnull_type>())
{
ValidateVariableTypeVisitor visitor(_schema, _types);
visitor.visit(*child);
if (!visitor.isInputType())
{
// https://spec.graphql.org/October2021/#sec-Variables-Are-Input-Types
auto position = child->begin();
std::ostringstream message;
message << "Invalid variable type";
if (!operationName.empty())
{
message << " operation: " << operationName;
}
message << " name: " << variableName;
_errors.push_back({ message.str(), { position.line, position.column } });
return;
}
variableArgument.type = visitor.getType();
}
else if (child->is_type<peg::default_value>())
{
ValidateArgumentValueVisitor visitor(_errors);
visitor.visit(*child->children.back());
auto argument = visitor.getArgumentValue();
if (!validateInputValue(false, argument, variableArgument.type))
{
// https://spec.graphql.org/October2021/#sec-Values-of-Correct-Type
auto position = child->begin();
std::ostringstream message;
message << "Incompatible variable default value";
if (!operationName.empty())
{
message << " operation: " << operationName;
}
message << " name: " << variableName;
_errors.push_back({ message.str(), { position.line, position.column } });
return;
}
variableArgument.defaultValue = true;
variableArgument.nonNullDefaultValue = argument.value != nullptr;
}
}
_variableDefinitions.emplace(variableName, variable);
_operationVariables->emplace(variableName, std::move(variableArgument));
});
peg::on_first_child<peg::directives>(operationDefinition,
[this, &operationType](const peg::ast_node& child) {
auto location = introspection::DirectiveLocation::QUERY;
if (operationType == strMutation)
{
location = introspection::DirectiveLocation::MUTATION;
}
else if (operationType == strSubscription)
{
location = introspection::DirectiveLocation::SUBSCRIPTION;
}
visitDirectives(location, child);
});
auto itrType = _operationTypes.find(operationType);
if (itrType == _operationTypes.end())
{
auto position = operationDefinition.begin();
std::ostringstream error;
error << "Unsupported operation type: " << operationType;
_errors.push_back({ error.str(), { position.line, position.column } });
return;
}
_scopedType = itrType->second;
_introspectionFieldCount = 0;
_fieldCount = 0;
const auto& selection = *operationDefinition.children.back();
visitSelection(selection);
if (operationType == strSubscription)
{
if (_fieldCount > 1)
{
// https://spec.graphql.org/October2021/#sec-Single-root-field
auto position = operationDefinition.begin();
std::ostringstream error;
error << "Subscription with more than one root field";
if (!operationName.empty())
{
error << " name: " << operationName;
}
_errors.push_back({ error.str(), { position.line, position.column } });
}
if (_introspectionFieldCount != 0)
{
// https://spec.graphql.org/October2021/#sec-Single-root-field
auto position = operationDefinition.begin();
std::ostringstream error;
error << "Subscription with Introspection root field";
if (!operationName.empty())
{
error << " name: " << operationName;
}
_errors.push_back({ error.str(), { position.line, position.column } });
}
}
_scopedType.reset();
_fragmentStack.clear();
_selectionFields.clear();
for (const auto& variable : _variableDefinitions)
{
if (_referencedVariables.find(variable.first) == _referencedVariables.end())
{
// https://spec.graphql.org/October2021/#sec-All-Variables-Used
auto position = variable.second.get().begin();
std::ostringstream error;
error << "Unused variable name: " << variable.first;
_errors.push_back({ error.str(), { position.line, position.column } });
}
}
_operationVariables.reset();
_variableDefinitions.clear();
_referencedVariables.clear();
}
void ValidateExecutableVisitor::visitSelection(const peg::ast_node& selection)
{
for (const auto& child : selection.children)
{
if (child->is_type<peg::field>())
{
visitField(*child);
}
else if (child->is_type<peg::fragment_spread>())
{
visitFragmentSpread(*child);
}
else if (child->is_type<peg::inline_fragment>())
{
visitInlineFragment(*child);
}
}
}
ValidateTypeFieldArguments ValidateExecutableVisitor::getArguments(
const std::vector<std::shared_ptr<const schema::InputValue>>& args)
{
ValidateTypeFieldArguments result;
for (const auto& arg : args)
{
if (!arg)
{
continue;
}
ValidateArgument argument;
argument.defaultValue = !arg->defaultValue().empty();
argument.nonNullDefaultValue =
argument.defaultValue && arg->defaultValue() != R"gql(null)gql"sv;
argument.type = getValidateType(arg->type().lock());
result[arg->name()] = std::move(argument);
}
return result;
}
constexpr bool ValidateExecutableVisitor::isScalarType(introspection::TypeKind kind)
{
switch (kind)
{
case introspection::TypeKind::OBJECT:
case introspection::TypeKind::INTERFACE:
case introspection::TypeKind::UNION:
return false;
default:
return true;
}
}
bool ValidateExecutableVisitor::matchesScopedType(std::string_view name) const
{
if (name == _scopedType->get().name())
{
return true;
}
const auto itrScoped = _matchingTypes.find(_scopedType->get().name());
const auto itrNamed = _matchingTypes.find(name);
if (itrScoped != _matchingTypes.end() && itrNamed != _matchingTypes.end())
{
const auto itrMatch = std::find_if(itrScoped->second.begin(),
itrScoped->second.end(),
[itrNamed](std::string_view matchingType) noexcept {
return itrNamed->second.find(matchingType) != itrNamed->second.end();
});
return itrMatch != itrScoped->second.end();
}
return false;
}
bool ValidateExecutableVisitor::validateInputValue(
bool hasNonNullDefaultValue, const ValidateArgumentValuePtr& argument, const ValidateType& type)
{
if (!type)
{
_errors.push_back({ "Unknown input type", argument.position });
return false;
}
if (argument.value && std::holds_alternative<ValidateArgumentVariable>(argument.value->data))
{
if (_operationVariables)
{
const auto& variable = std::get<ValidateArgumentVariable>(argument.value->data);
auto itrVariable = _operationVariables->find(variable.name);
if (itrVariable == _operationVariables->end())
{
// https://spec.graphql.org/October2021/#sec-All-Variable-Uses-Defined
std::ostringstream message;
message << "Undefined variable name: " << variable.name;
_errors.push_back({ message.str(), argument.position });
return false;
}
_referencedVariables.emplace(variable.name);
return validateVariableType(
hasNonNullDefaultValue || itrVariable->second.nonNullDefaultValue,
itrVariable->second.type,
argument.position,
type);
}
else
{
// In fragment definitions, variables can hold any type. It's only when we are
// transitively visiting them through an operation definition that they are assigned a
// type, and the type may not be exactly the same in all operations definitions which
// reference the fragment.
return true;
}
}
const auto kind = type->get().kind();
if (!argument.value)
{
// The null literal matches any nullable type and does not match a non-nullable type.
if (kind == introspection::TypeKind::NON_NULL && !hasNonNullDefaultValue)
{
_errors.push_back({ "Expected Non-Null value", argument.position });
return false;
}
return true;
}
switch (kind)
{
case introspection::TypeKind::NON_NULL:
{
// Unwrap and check the next one.
const auto ofType = getValidateType(type->get().ofType().lock());
if (!ofType)
{
_errors.push_back({ "Unknown Non-Null type", argument.position });