@@ -548,8 +548,12 @@ void SchemaLoader::visitObjectTypeDefinition(const peg::ast_node& objectTypeDefi
548
548
std::string_view description;
549
549
550
550
peg::on_first_child<peg::object_name>(objectTypeDefinition,
551
- [&name](const peg::ast_node& child) {
551
+ [isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
552
552
name = child.string_view ();
553
+ if (!isIntrospection)
554
+ {
555
+ blockReservedName (name, child.begin ());
556
+ }
553
557
});
554
558
555
559
peg::on_first_child<peg::description>(objectTypeDefinition,
@@ -575,9 +579,14 @@ void SchemaLoader::visitObjectTypeExtension(const peg::ast_node& objectTypeExten
575
579
{
576
580
std::string_view name;
577
581
578
- peg::on_first_child<peg::object_name>(objectTypeExtension, [&name](const peg::ast_node& child) {
579
- name = child.string_view ();
580
- });
582
+ peg::on_first_child<peg::object_name>(objectTypeExtension,
583
+ [isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
584
+ name = child.string_view ();
585
+ if (!isIntrospection)
586
+ {
587
+ blockReservedName (name, child.begin ());
588
+ }
589
+ });
581
590
582
591
const auto itrType = _objectNames.find (name);
583
592
@@ -609,8 +618,12 @@ void SchemaLoader::visitInterfaceTypeDefinition(const peg::ast_node& interfaceTy
609
618
std::string_view description;
610
619
611
620
peg::on_first_child<peg::interface_name>(interfaceTypeDefinition,
612
- [&name](const peg::ast_node& child) {
621
+ [isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
613
622
name = child.string_view ();
623
+ if (!isIntrospection)
624
+ {
625
+ blockReservedName (name, child.begin ());
626
+ }
614
627
});
615
628
616
629
peg::on_first_child<peg::description>(interfaceTypeDefinition,
@@ -637,8 +650,12 @@ void SchemaLoader::visitInterfaceTypeExtension(const peg::ast_node& interfaceTyp
637
650
std::string_view name;
638
651
639
652
peg::on_first_child<peg::interface_name>(interfaceTypeExtension,
640
- [&name](const peg::ast_node& child) {
653
+ [isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
641
654
name = child.string_view ();
655
+ if (!isIntrospection)
656
+ {
657
+ blockReservedName (name, child.begin ());
658
+ }
642
659
});
643
660
644
661
const auto itrType = _interfaceNames.find (name);
@@ -666,8 +683,12 @@ void SchemaLoader::visitInputObjectTypeDefinition(const peg::ast_node& inputObje
666
683
std::string_view description;
667
684
668
685
peg::on_first_child<peg::object_name>(inputObjectTypeDefinition,
669
- [&name](const peg::ast_node& child) {
686
+ [isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
670
687
name = child.string_view ();
688
+ if (!isIntrospection)
689
+ {
690
+ blockReservedName (name, child.begin ());
691
+ }
671
692
});
672
693
673
694
peg::on_first_child<peg::description>(inputObjectTypeDefinition,
@@ -694,8 +715,12 @@ void SchemaLoader::visitInputObjectTypeExtension(const peg::ast_node& inputObjec
694
715
std::string_view name;
695
716
696
717
peg::on_first_child<peg::object_name>(inputObjectTypeExtension,
697
- [&name](const peg::ast_node& child) {
718
+ [isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
698
719
name = child.string_view ();
720
+ if (!isIntrospection)
721
+ {
722
+ blockReservedName (name, child.begin ());
723
+ }
699
724
});
700
725
701
726
const auto itrType = _inputNames.find (name);
@@ -722,9 +747,14 @@ void SchemaLoader::visitEnumTypeDefinition(const peg::ast_node& enumTypeDefiniti
722
747
std::string_view name;
723
748
std::string_view description;
724
749
725
- peg::on_first_child<peg::enum_name>(enumTypeDefinition, [&name](const peg::ast_node& child) {
726
- name = child.string_view ();
727
- });
750
+ peg::on_first_child<peg::enum_name>(enumTypeDefinition,
751
+ [isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
752
+ name = child.string_view ();
753
+ if (!isIntrospection)
754
+ {
755
+ blockReservedName (name, child.begin ());
756
+ }
757
+ });
728
758
729
759
peg::on_first_child<peg::description>(enumTypeDefinition,
730
760
[&description](const peg::ast_node& child) {
@@ -749,9 +779,14 @@ void SchemaLoader::visitEnumTypeExtension(const peg::ast_node& enumTypeExtension
749
779
{
750
780
std::string_view name;
751
781
752
- peg::on_first_child<peg::enum_name>(enumTypeExtension, [&name](const peg::ast_node& child) {
753
- name = child.string_view ();
754
- });
782
+ peg::on_first_child<peg::enum_name>(enumTypeExtension,
783
+ [isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
784
+ name = child.string_view ();
785
+ if (!isIntrospection)
786
+ {
787
+ blockReservedName (name, child.begin ());
788
+ }
789
+ });
755
790
756
791
const auto itrType = _enumNames.find (name);
757
792
@@ -827,8 +862,12 @@ void SchemaLoader::visitScalarTypeDefinition(const peg::ast_node& scalarTypeDefi
827
862
std::string_view description;
828
863
829
864
peg::on_first_child<peg::scalar_name>(scalarTypeDefinition,
830
- [&name](const peg::ast_node& child) {
865
+ [isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
831
866
name = child.string_view ();
867
+ if (!isIntrospection)
868
+ {
869
+ blockReservedName (name, child.begin ());
870
+ }
832
871
});
833
872
834
873
peg::on_first_child<peg::description>(scalarTypeDefinition,
@@ -851,9 +890,14 @@ void SchemaLoader::visitScalarTypeExtension(const peg::ast_node& scalarTypeExten
851
890
{
852
891
std::string_view name;
853
892
854
- peg::on_first_child<peg::scalar_name>(scalarTypeExtension, [&name](const peg::ast_node& child) {
855
- name = child.string_view ();
856
- });
893
+ peg::on_first_child<peg::scalar_name>(scalarTypeExtension,
894
+ [isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
895
+ name = child.string_view ();
896
+ if (!isIntrospection)
897
+ {
898
+ blockReservedName (name, child.begin ());
899
+ }
900
+ });
857
901
858
902
const auto itrType = _scalarNames.find (name);
859
903
@@ -909,9 +953,14 @@ void SchemaLoader::visitUnionTypeDefinition(const peg::ast_node& unionTypeDefini
909
953
std::string_view name;
910
954
std::string_view description;
911
955
912
- peg::on_first_child<peg::union_name>(unionTypeDefinition, [&name](const peg::ast_node& child) {
913
- name = child.string_view ();
914
- });
956
+ peg::on_first_child<peg::union_name>(unionTypeDefinition,
957
+ [isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
958
+ name = child.string_view ();
959
+ if (!isIntrospection)
960
+ {
961
+ blockReservedName (name, child.begin ());
962
+ }
963
+ });
915
964
916
965
peg::on_first_child<peg::description>(unionTypeDefinition,
917
966
[&description](const peg::ast_node& child) {
@@ -936,9 +985,14 @@ void SchemaLoader::visitUnionTypeExtension(const peg::ast_node& unionTypeExtensi
936
985
{
937
986
std::string_view name;
938
987
939
- peg::on_first_child<peg::union_name>(unionTypeExtension, [&name](const peg::ast_node& child) {
940
- name = child.string_view ();
941
- });
988
+ peg::on_first_child<peg::union_name>(unionTypeExtension,
989
+ [isIntrospection = _isIntrospection, &name](const peg::ast_node& child) {
990
+ name = child.string_view ();
991
+ if (!isIntrospection)
992
+ {
993
+ blockReservedName (name, child.begin ());
994
+ }
995
+ });
942
996
943
997
const auto itrType = _unionNames.find (name);
944
998
@@ -958,8 +1012,12 @@ void SchemaLoader::visitDirectiveDefinition(const peg::ast_node& directiveDefini
958
1012
Directive directive;
959
1013
960
1014
peg::on_first_child<peg::directive_name>(directiveDefinition,
961
- [&directive](const peg::ast_node& child) {
1015
+ [isIntrospection = _isIntrospection, &directive](const peg::ast_node& child) {
962
1016
directive.name = child.string_view ();
1017
+ if (!isIntrospection)
1018
+ {
1019
+ blockReservedName (directive.name , child.begin ());
1020
+ }
963
1021
});
964
1022
965
1023
peg::on_first_child<peg::description>(directiveDefinition,
@@ -1030,6 +1088,25 @@ std::string_view SchemaLoader::getSafeCppName(std::string_view type) noexcept
1030
1088
return (safeNames.cend () == itr) ? type : itr->second ->second ;
1031
1089
}
1032
1090
1091
+ void SchemaLoader::blockReservedName (
1092
+ std::string_view name, std::optional<tao::graphqlpeg::position> position)
1093
+ {
1094
+ // https://spec.graphql.org/October2021/#sec-Names.Reserved-Names
1095
+ if (name.size () > 1 && name.substr (0 , 2 ) == R"gql( __)gql" sv)
1096
+ {
1097
+ std::ostringstream error;
1098
+
1099
+ error << " Names starting with __ are reserved: " << name;
1100
+
1101
+ if (position)
1102
+ {
1103
+ error << " line: " << position->line << " column: " << position->column ;
1104
+ }
1105
+
1106
+ throw std::runtime_error (error.str ());
1107
+ }
1108
+ }
1109
+
1033
1110
OutputFieldList SchemaLoader::getOutputFields (const peg::ast_node::children_t & fields)
1034
1111
{
1035
1112
OutputFieldList outputFields;
0 commit comments