1313#include < cctype>
1414#include < fstream>
1515#include < iterator>
16+ #include < sstream>
1617
1718using namespace std ::literals;
1819
@@ -37,6 +38,8 @@ RequestLoader::RequestLoader(RequestOptions&& requestOptions, const SchemaLoader
3738 }
3839
3940 validateRequest ();
41+
42+ findOperation ();
4043}
4144
4245std::string_view RequestLoader::getRequestFilename () const noexcept
@@ -46,7 +49,12 @@ std::string_view RequestLoader::getRequestFilename() const noexcept
4649
4750std::string_view RequestLoader::getOperationName () const noexcept
4851{
49- return _requestOptions.operationName .empty () ? " (default)" sv : _requestOptions.operationName ;
52+ return _operationName.empty () ? " (default)" sv : _operationName;
53+ }
54+
55+ std::string_view RequestLoader::getOperationType () const noexcept
56+ {
57+ return _operationType;
5058}
5159
5260std::string_view RequestLoader::getRequestText () const noexcept
@@ -458,4 +466,51 @@ std::string_view RequestLoader::trimWhitespace(std::string_view content) noexcep
458466 return content;
459467}
460468
469+ void RequestLoader::findOperation ()
470+ {
471+ peg::on_first_child_if<peg::operation_definition>(*_ast.root ,
472+ [this ](const peg::ast_node& operationDefinition) noexcept -> bool
473+ {
474+ std::string_view operationType = service::strQuery;
475+
476+ peg::on_first_child<peg::operation_type>(operationDefinition,
477+ [&operationType](const peg::ast_node& child)
478+ {
479+ operationType = child.string_view ();
480+ });
481+
482+ std::string_view name;
483+
484+ peg::on_first_child<peg::operation_name>(operationDefinition,
485+ [&name](const peg::ast_node& child)
486+ {
487+ name = child.string_view ();
488+ });
489+
490+ if (_requestOptions.operationName .empty () || name == _requestOptions.operationName )
491+ {
492+ _operationName = name;
493+ _operationType = operationType;
494+ _operation = &operationDefinition;
495+ return true ;
496+ }
497+
498+ return false ;
499+ });
500+
501+ if (!_operation)
502+ {
503+ std::ostringstream message;
504+
505+ message << " Missing operation" ;
506+
507+ if (!_operationName.empty ())
508+ {
509+ message << " name: " << _operationName;
510+ }
511+
512+ throw service::schema_exception { { message.str () } };
513+ }
514+ }
515+
461516} /* namespace graphql::generator */
0 commit comments