Skip to content

Commit 1129477

Browse files
committed
Output the original request in a header comment block
1 parent 8931442 commit 1129477

File tree

7 files changed

+206
-34
lines changed

7 files changed

+206
-34
lines changed

include/ClientGenerator.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class Generator
2626
{
2727
public:
2828
// Initialize the generator with the introspection client or a custom GraphQL client.
29-
explicit Generator(SchemaOptions&& schemaOptions, RequestOptions&& requestOptions, GeneratorOptions&& options);
29+
explicit Generator(
30+
SchemaOptions&& schemaOptions, RequestOptions&& requestOptions, GeneratorOptions&& options);
3031

3132
// Run the generator and return a list of filenames that were output.
3233
std::vector<std::string> Build() const noexcept;
@@ -38,6 +39,7 @@ class Generator
3839
std::string getSourcePath() const noexcept;
3940

4041
bool outputHeader() const noexcept;
42+
void outputRequestComment(std::ostream& headerFile) const noexcept;
4143
void outputObjectDeclaration(
4244
std::ostream& headerFile, const ObjectType& objectType, bool isQueryType) const;
4345
std::string getFieldDeclaration(const InputField& inputField) const noexcept;

include/RequestLoader.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212

1313
namespace graphql::generator {
1414

15+
using ResponseField = InputField;
16+
using ResponseFieldList = InputFieldList;
17+
18+
struct ResponseType
19+
{
20+
std::string_view type;
21+
std::string_view cppType;
22+
ResponseFieldList fields;
23+
};
24+
1525
struct RequestOptions
1626
{
1727
const std::string requestFilename;
@@ -24,16 +34,30 @@ class RequestLoader
2434
public:
2535
explicit RequestLoader(RequestOptions&& requestOptions, const SchemaLoader& schemaLoader);
2636

37+
std::string_view getRequestFilename() const noexcept;
38+
std::string_view getOperationName() const noexcept;
39+
std::string_view getRequestText() const noexcept;
40+
41+
const ResponseType& getVariablesType() const noexcept;
42+
const ResponseType& getResponseType() const noexcept;
43+
2744
private:
28-
void buildSchema(const SchemaLoader& schemaLoader);
29-
void addTypesToSchema(const SchemaLoader& schemaLoader);
45+
void buildSchema();
46+
void addTypesToSchema();
3047
std::shared_ptr<const schema::BaseType> getSchemaType(
3148
std::string_view type, const TypeModifierStack& modifiers) const noexcept;
3249
void validateRequest() const;
3350

51+
static std::string_view trimWhitespace(std::string_view content) noexcept;
52+
3453
const RequestOptions _requestOptions;
54+
const SchemaLoader& _schemaLoader;
3555
std::shared_ptr<schema::Schema> _schema;
3656
peg::ast _ast;
57+
58+
std::string _requestText;
59+
ResponseType _variablesType;
60+
ResponseType _responseType;
3761
};
3862

3963
} /* namespace graphql::generator */

samples/client/MutateClient.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ static_assert(graphql::internal::MinorVersion == 6, "regenerate with clientgen:
1919
#include <string>
2020
#include <vector>
2121

22+
/** Operation: (default)
23+
24+
# Copyright (c) Microsoft Corporation. All rights reserved.
25+
# Licensed under the MIT License.
26+
27+
mutation {
28+
completedTask: completeTask(input: {id: "ZmFrZVRhc2tJZA==", isComplete: true, clientMutationId: "Hi There!"}) {
29+
completedTask: task {
30+
completedTaskId: id
31+
title
32+
isComplete
33+
}
34+
clientMutationId
35+
}
36+
}
37+
38+
**/
39+
2240
namespace graphql {
2341
namespace mutate {
2442

samples/client/QueryClient.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,47 @@ static_assert(graphql::internal::MinorVersion == 6, "regenerate with clientgen:
1919
#include <string>
2020
#include <vector>
2121

22+
/** Operation: (default)
23+
24+
# Copyright (c) Microsoft Corporation. All rights reserved.
25+
# Licensed under the MIT License.
26+
27+
query Everything {
28+
appointments {
29+
edges {
30+
node {
31+
id
32+
subject
33+
when
34+
isNow
35+
__typename
36+
}
37+
}
38+
}
39+
tasks {
40+
edges {
41+
node {
42+
id
43+
title
44+
isComplete
45+
__typename
46+
}
47+
}
48+
}
49+
unreadCounts {
50+
edges {
51+
node {
52+
id
53+
name
54+
unreadCount
55+
__typename
56+
}
57+
}
58+
}
59+
}
60+
61+
**/
62+
2263
namespace graphql {
2364
namespace query {
2465

samples/client/SubscribeClient.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ static_assert(graphql::internal::MinorVersion == 6, "regenerate with clientgen:
1919
#include <string>
2020
#include <vector>
2121

22+
/** Operation: (default)
23+
24+
# Copyright (c) Microsoft Corporation. All rights reserved.
25+
# Licensed under the MIT License.
26+
27+
subscription TestSubscription {
28+
nextAppointment: nextAppointmentChange {
29+
nextAppointmentId: id
30+
when
31+
subject
32+
isNow
33+
}
34+
}
35+
36+
**/
37+
2238
namespace graphql {
2339
namespace subscribe {
2440

src/ClientGenerator.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ using namespace std::literals;
4848

4949
namespace graphql::generator::client {
5050

51-
Generator::Generator(SchemaOptions&& schemaOptions, RequestOptions&& requestOptions, GeneratorOptions&& options)
51+
Generator::Generator(
52+
SchemaOptions&& schemaOptions, RequestOptions&& requestOptions, GeneratorOptions&& options)
5253
: _schemaLoader(std::make_optional(std::move(schemaOptions)))
5354
, _requestLoader(std::move(requestOptions), _schemaLoader)
5455
, _options(std::move(options))
@@ -139,9 +140,12 @@ static_assert(graphql::internal::MinorVersion == )cpp"
139140
#include <memory>
140141
#include <string>
141142
#include <vector>
142-
143143
)cpp";
144144

145+
outputRequestComment(headerFile);
146+
147+
headerFile << std::endl;
148+
145149
NamespaceScope graphqlNamespace { headerFile, "graphql" };
146150
NamespaceScope schemaNamespace { headerFile, _schemaLoader.getSchemaNamespace() };
147151
NamespaceScope objectNamespace { headerFile, "object", true };
@@ -334,6 +338,19 @@ std::shared_ptr<client::Client> GetClient();
334338
return true;
335339
}
336340

341+
void Generator::outputRequestComment(std::ostream& headerFile) const noexcept
342+
{
343+
headerFile << R"cpp(
344+
/** Operation: )cpp"
345+
<< _requestLoader.getOperationName() << R"cpp(
346+
347+
)cpp" << _requestLoader.getRequestText()
348+
<< R"cpp(
349+
350+
**/
351+
)cpp";
352+
}
353+
337354
void Generator::outputObjectDeclaration(
338355
std::ostream& headerFile, const ObjectType& objectType, bool isQueryType) const
339356
{
@@ -413,9 +430,9 @@ std::string Generator::getFieldDeclaration(const OutputField& outputField) const
413430
std::string fieldName { outputField.cppName };
414431

415432
fieldName[0] = static_cast<char>(std::toupper(static_cast<unsigned char>(fieldName[0])));
416-
output << R"cpp( virtual service::FieldResult<)cpp" << _schemaLoader.getOutputCppType(outputField)
417-
<< R"cpp(> )cpp" << outputField.accessor << fieldName
418-
<< R"cpp((service::FieldParams&& params)cpp";
433+
output << R"cpp( virtual service::FieldResult<)cpp"
434+
<< _schemaLoader.getOutputCppType(outputField) << R"cpp(> )cpp" << outputField.accessor
435+
<< fieldName << R"cpp((service::FieldParams&& params)cpp";
419436

420437
for (const auto& argument : outputField.arguments)
421438
{
@@ -839,8 +856,8 @@ void Generator::outputObjectImplementation(
839856
fieldName[0] = static_cast<char>(std::toupper(static_cast<unsigned char>(fieldName[0])));
840857
sourceFile << R"cpp(
841858
service::FieldResult<)cpp"
842-
<< _schemaLoader.getOutputCppType(outputField) << R"cpp(> )cpp" << objectType.cppType
843-
<< R"cpp(::)cpp" << outputField.accessor << fieldName
859+
<< _schemaLoader.getOutputCppType(outputField) << R"cpp(> )cpp"
860+
<< objectType.cppType << R"cpp(::)cpp" << outputField.accessor << fieldName
844861
<< R"cpp((service::FieldParams&&)cpp";
845862
for (const auto& argument : outputField.arguments)
846863
{

0 commit comments

Comments
 (0)