Skip to content

Commit 21ec432

Browse files
committed
Store the unwrapped types with a TypeModifierStack
1 parent e3033c4 commit 21ec432

File tree

2 files changed

+63
-22
lines changed

2 files changed

+63
-22
lines changed

include/RequestLoader.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,26 @@ using ResponseFieldChildren = std::variant<ResponseFieldList, ResponseUnionOptio
3232
struct ResponseField
3333
{
3434
std::shared_ptr<const schema::BaseType> type;
35+
TypeModifierStack modifiers;
36+
std::string_view name;
37+
std::string_view cppName;
38+
std::optional<tao::graphqlpeg::position> position;
39+
std::optional<ResponseFieldChildren> children;
40+
};
41+
42+
struct RequestVariable
43+
{
44+
std::shared_ptr<const schema::BaseType> type;
45+
TypeModifierStack modifiers;
3546
std::string_view name;
3647
std::string_view cppName;
3748
std::string_view defaultValueString;
3849
response::Value defaultValue;
3950
std::optional<tao::graphqlpeg::position> position;
40-
std::optional<ResponseFieldChildren> children;
4151
};
4252

53+
using RequestVariableList = std::vector<RequestVariable>;
54+
4355
struct RequestOptions
4456
{
4557
const std::string requestFilename;
@@ -60,7 +72,7 @@ class RequestLoader
6072
std::string_view getOperationType() const noexcept;
6173
std::string_view getRequestText() const noexcept;
6274

63-
const ResponseFieldList& getVariables() const noexcept;
75+
const RequestVariableList& getVariables() const noexcept;
6476
const ResponseType& getResponseType() const noexcept;
6577

6678
private:
@@ -113,7 +125,7 @@ class RequestLoader
113125
const peg::ast_node* _operation = nullptr;
114126
std::string_view _operationName;
115127
std::string_view _operationType;
116-
ResponseFieldList _variables;
128+
RequestVariableList _variables;
117129
ResponseType _responseType;
118130

119131
FragmentDefinitionMap _fragments;

src/RequestLoader.cpp

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ std::string_view RequestLoader::getRequestText() const noexcept
9999
return trimWhitespace(_requestText);
100100
}
101101

102-
const ResponseFieldList& RequestLoader::getVariables() const noexcept
102+
const RequestVariableList& RequestLoader::getVariables() const noexcept
103103
{
104104
return _variables;
105105
}
@@ -581,7 +581,7 @@ void RequestLoader::collectVariables() noexcept
581581
{
582582
peg::for_each_child<peg::variable>(*_operation,
583583
[this](const peg::ast_node& variableDefinition) {
584-
ResponseField variable;
584+
RequestVariable variable;
585585
TypeVisitor variableType;
586586
service::schema_location defaultValueLocation;
587587

@@ -613,22 +613,24 @@ void RequestLoader::collectVariables() noexcept
613613

614614
const auto [type, modifiers] = variableType.getType();
615615

616-
variable.type = getSchemaType(type, modifiers);
617-
variable.position = variableDefinition.begin();
616+
variable.type = _schema->LookupType(type);
617+
variable.modifiers = modifiers;
618618

619619
if (!variable.defaultValueString.empty()
620620
&& variable.defaultValue.type() == response::Type::Null
621621
&& (modifiers.empty() || modifiers.front() != service::TypeModifier::Nullable))
622622
{
623623
std::ostringstream error;
624624

625-
error << "Expected Non-Null default value for variable name: " << variable.name
626-
<< " line: " << defaultValueLocation.line
627-
<< " column: " << defaultValueLocation.column;
625+
error << "Expected Non-Null default value for variable name: " << variable.name;
628626

629-
throw std::runtime_error(error.str());
627+
throw service::schema_exception {
628+
{ service::schema_error { error.str(), std::move(defaultValueLocation) } }
629+
};
630630
}
631631

632+
variable.position = variableDefinition.begin();
633+
632634
_variables.push_back(std::move(variable));
633635
});
634636
}
@@ -742,6 +744,41 @@ void RequestLoader::SelectionVisitor::visitField(const peg::ast_node& field)
742744
responseField.type = (*itr)->type().lock();
743745
}
744746

747+
bool wrapped = true;
748+
bool nonNull = false;
749+
750+
while (wrapped)
751+
{
752+
switch (responseField.type->kind())
753+
{
754+
case introspection::TypeKind::NON_NULL:
755+
nonNull = true;
756+
responseField.type = responseField.type->ofType().lock();
757+
break;
758+
759+
case introspection::TypeKind::LIST:
760+
if (!nonNull)
761+
{
762+
responseField.modifiers.push_back(service::TypeModifier::Nullable);
763+
}
764+
765+
nonNull = false;
766+
responseField.modifiers.push_back(service::TypeModifier::List);
767+
responseField.type = responseField.type->ofType().lock();
768+
break;
769+
770+
default:
771+
if (!nonNull)
772+
{
773+
responseField.modifiers.push_back(service::TypeModifier::Nullable);
774+
}
775+
776+
nonNull = false;
777+
wrapped = false;
778+
break;
779+
}
780+
}
781+
745782
const peg::ast_node* selection = nullptr;
746783

747784
peg::on_first_child<peg::selection_set>(field, [&selection](const peg::ast_node& child) {
@@ -750,20 +787,12 @@ void RequestLoader::SelectionVisitor::visitField(const peg::ast_node& field)
750787

751788
if (selection)
752789
{
753-
auto ofType = responseField.type;
754-
755-
while (ofType->kind() == introspection::TypeKind::NON_NULL
756-
|| ofType->kind() == introspection::TypeKind::LIST)
757-
{
758-
ofType = ofType->ofType().lock();
759-
}
760-
761-
switch (ofType->kind())
790+
switch (responseField.type->kind())
762791
{
763792
case introspection::TypeKind::OBJECT:
764793
case introspection::TypeKind::INTERFACE:
765794
{
766-
SelectionVisitor selectionVisitor { _fragments, _schema, ofType };
795+
SelectionVisitor selectionVisitor { _fragments, _schema, responseField.type };
767796

768797
selectionVisitor.visit(*selection);
769798

@@ -780,7 +809,7 @@ void RequestLoader::SelectionVisitor::visitField(const peg::ast_node& field)
780809
case introspection::TypeKind::UNION:
781810
{
782811
ResponseUnionOptions options;
783-
const auto& possibleTypes = ofType->possibleTypes();
812+
const auto& possibleTypes = responseField.type->possibleTypes();
784813

785814
for (const auto& weakType : possibleTypes)
786815
{

0 commit comments

Comments
 (0)