Skip to content

Commit 4fa3e7b

Browse files
committed
Get the operation type and name from the request doc
1 parent 1129477 commit 4fa3e7b

File tree

6 files changed

+67
-5
lines changed

6 files changed

+67
-5
lines changed

include/RequestLoader.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class RequestLoader
3636

3737
std::string_view getRequestFilename() const noexcept;
3838
std::string_view getOperationName() const noexcept;
39+
std::string_view getOperationType() const noexcept;
3940
std::string_view getRequestText() const noexcept;
4041

4142
const ResponseType& getVariablesType() const noexcept;
@@ -50,12 +51,17 @@ class RequestLoader
5051

5152
static std::string_view trimWhitespace(std::string_view content) noexcept;
5253

54+
void findOperation();
55+
5356
const RequestOptions _requestOptions;
5457
const SchemaLoader& _schemaLoader;
5558
std::shared_ptr<schema::Schema> _schema;
5659
peg::ast _ast;
5760

5861
std::string _requestText;
62+
const peg::ast_node* _operation = nullptr;
63+
std::string_view _operationName;
64+
std::string_view _operationType;
5965
ResponseType _variablesType;
6066
ResponseType _responseType;
6167
};

samples/client/MutateClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static_assert(graphql::internal::MinorVersion == 6, "regenerate with clientgen:
1919
#include <string>
2020
#include <vector>
2121

22-
/** Operation: (default)
22+
/** Operation: mutation (default)
2323
2424
# Copyright (c) Microsoft Corporation. All rights reserved.
2525
# Licensed under the MIT License.

samples/client/QueryClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static_assert(graphql::internal::MinorVersion == 6, "regenerate with clientgen:
1919
#include <string>
2020
#include <vector>
2121

22-
/** Operation: (default)
22+
/** Operation: query Everything
2323
2424
# Copyright (c) Microsoft Corporation. All rights reserved.
2525
# Licensed under the MIT License.

samples/client/SubscribeClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static_assert(graphql::internal::MinorVersion == 6, "regenerate with clientgen:
1919
#include <string>
2020
#include <vector>
2121

22-
/** Operation: (default)
22+
/** Operation: subscription TestSubscription
2323
2424
# Copyright (c) Microsoft Corporation. All rights reserved.
2525
# Licensed under the MIT License.

src/ClientGenerator.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ void Generator::outputRequestComment(std::ostream& headerFile) const noexcept
342342
{
343343
headerFile << R"cpp(
344344
/** Operation: )cpp"
345-
<< _requestLoader.getOperationName() << R"cpp(
345+
<< _requestLoader.getOperationType() << ' ' << _requestLoader.getOperationName()
346+
<< R"cpp(
346347
347348
)cpp" << _requestLoader.getRequestText()
348349
<< R"cpp(

src/RequestLoader.cpp

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <cctype>
1414
#include <fstream>
1515
#include <iterator>
16+
#include <sstream>
1617

1718
using namespace std::literals;
1819

@@ -37,6 +38,8 @@ RequestLoader::RequestLoader(RequestOptions&& requestOptions, const SchemaLoader
3738
}
3839

3940
validateRequest();
41+
42+
findOperation();
4043
}
4144

4245
std::string_view RequestLoader::getRequestFilename() const noexcept
@@ -46,7 +49,12 @@ std::string_view RequestLoader::getRequestFilename() const noexcept
4649

4750
std::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

5260
std::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

Comments
 (0)