Skip to content

Commit f4c99d2

Browse files
committed
Implement serializeVariables
1 parent 04d8225 commit f4c99d2

File tree

11 files changed

+301
-202
lines changed

11 files changed

+301
-202
lines changed

include/ClientGenerator.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,19 @@ class Generator
3939
std::string getHeaderPath() const noexcept;
4040
std::string getSourcePath() const noexcept;
4141
const std::string& getClientNamespace() const noexcept;
42+
const std::string& getRequestNamespace() const noexcept;
43+
const std::string& getFullNamespace() const noexcept;
44+
std::string getResponseFieldCppType(const ResponseField& responseField) const noexcept;
4245

4346
bool outputHeader() const noexcept;
4447
void outputRequestComment(std::ostream& headerFile) const noexcept;
4548
void outputGetRequestDeclaration(std::ostream& headerFile) const noexcept;
4649
bool outputResponseFieldType(std::ostream& headerFile, const ResponseField& responseField,
4750
size_t indent = 0) const noexcept;
4851

49-
std::string getResponseFieldCppType(const ResponseField& responseField) const noexcept;
50-
5152
bool outputSource() const noexcept;
5253
void outputGetRequestImplementation(std::ostream& sourceFile) const noexcept;
54+
static std::string getTypeModifierList(const TypeModifierStack& modifiers) noexcept;
5355

5456
const SchemaLoader _schemaLoader;
5557
const RequestLoader _requestLoader;

include/RequestLoader.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,16 @@ class RequestLoader
8585
static std::string getOutputCppType(
8686
std::string_view outputCppType, const TypeModifierStack& modifiers) noexcept;
8787

88+
static std::pair<RequestSchemaType, TypeModifierStack> unwrapSchemaType(
89+
RequestSchemaType&& type) noexcept;
90+
8891
private:
8992
void buildSchema();
9093
void addTypesToSchema();
9194
RequestSchemaType getSchemaType(
9295
std::string_view type, const TypeModifierStack& modifiers) const noexcept;
9396
void validateRequest() const;
9497

95-
static std::pair<RequestSchemaType, TypeModifierStack> unwrapSchemaType(
96-
RequestSchemaType&& type) noexcept;
9798
static std::string_view trimWhitespace(std::string_view content) noexcept;
9899

99100
void findOperation();

include/graphqlservice/GraphQLClient.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
#include <iterator>
2727
#include <optional>
2828
#include <stdexcept>
29-
#include <string>
30-
#include <string_view>
3129
#include <type_traits>
3230
#include <vector>
3331

samples/client/MutateClient.cpp

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,56 @@
1515

1616
using namespace std::literals;
1717

18-
namespace graphql::mutation::CompleteTaskMutation {
18+
namespace graphql::client {
19+
20+
using namespace mutation::CompleteTaskMutation;
21+
22+
static const std::array<std::string_view, 4> s_namesTaskState = {
23+
"New"sv,
24+
"Started"sv,
25+
"Complete"sv,
26+
"Unassigned"sv,
27+
};
28+
29+
response::Value ModifiedVariable<TaskState>::serialize(TaskState&& value)
30+
{
31+
response::Value result { response::Type::EnumValue };
32+
33+
result.set<response::StringType>(response::StringType { s_namesTaskState[static_cast<size_t>(value)] });
34+
35+
return result;
36+
}
37+
38+
response::Value ModifiedVariable<Variables::CompleteTaskInput>::serialize(Variables::CompleteTaskInput&& inputValue)
39+
{
40+
response::Value result { response::Type::Map };
41+
42+
result.emplace_back(R"js(id)js"s, ModifiedVariable<response::IdType>::serialize(std::move(inputValue.id)));
43+
result.emplace_back(R"js(testTaskState)js"s, ModifiedVariable<TaskState>::serialize<TypeModifier::Nullable>(std::move(inputValue.testTaskState)));
44+
result.emplace_back(R"js(isComplete)js"s, ModifiedVariable<response::BooleanType>::serialize<TypeModifier::Nullable>(std::move(inputValue.isComplete)));
45+
result.emplace_back(R"js(clientMutationId)js"s, ModifiedVariable<response::StringType>::serialize<TypeModifier::Nullable>(std::move(inputValue.clientMutationId)));
46+
47+
return result;
48+
}
49+
50+
TaskState parseTaskState(const response::Value& value)
51+
{
52+
if (!value.maybe_enum())
53+
{
54+
throw std::logic_error { "not a valid TaskState value" };
55+
}
56+
57+
const auto itr = std::find(s_namesTaskState.cbegin(), s_namesTaskState.cend(), value.get<response::StringType>());
58+
59+
if (itr == s_namesTaskState.cend())
60+
{
61+
throw std::logic_error { "not a valid TaskState value" };
62+
}
63+
64+
return static_cast<TaskState>(itr - s_namesTaskState.cbegin());
65+
}
66+
67+
namespace mutation::CompleteTaskMutation {
1968

2069
const std::string& GetRequestText() noexcept
2170
{
@@ -52,60 +101,15 @@ const peg::ast& GetRequestObject() noexcept
52101
return s_request;
53102
}
54103

55-
static const std::array<std::string_view, 4> s_namesTaskState = {
56-
"New"sv,
57-
"Started"sv,
58-
"Complete"sv,
59-
"Unassigned"sv,
60-
};
61-
62-
response::Value serializeTaskState(TaskState value)
63-
{
64-
response::Value result(response::Type::EnumValue);
65-
66-
result.set<response::StringType>(response::StringType { s_namesTaskState[static_cast<size_t>(value)] });
67-
68-
return result;
69-
}
70-
71-
response::Value serializeCompleteTaskInput(Variables::CompleteTaskInput&& inputValue)
72-
{
73-
response::Value result;
74-
75-
// response::IdType id;
76-
// std::optional<TaskState> testTaskState;
77-
// std::optional<response::BooleanType> isComplete;
78-
// std::optional<response::StringType> clientMutationId;
79-
80-
return result;
81-
}
82-
83104
response::Value serializeVariables(Variables&& variables)
84105
{
85106
response::Value result;
86107

87-
// input = serializeCompleteTaskInput(std::move(variables.input));
108+
result.emplace_back(R"js(input)js"s, ModifiedVariable<Variables::CompleteTaskInput>::serialize(std::move(variables.input)));
88109

89110
return result;
90111
}
91112

92-
TaskState parseTaskState(const response::Value& value)
93-
{
94-
if (!value.maybe_enum())
95-
{
96-
throw std::logic_error { "not a valid TaskState value" };
97-
}
98-
99-
const auto itr = std::find(s_namesTaskState.cbegin(), s_namesTaskState.cend(), value.get<response::StringType>());
100-
101-
if (itr == s_namesTaskState.cend())
102-
{
103-
throw std::logic_error { "not a valid TaskState value" };
104-
}
105-
106-
return static_cast<TaskState>(itr - s_namesTaskState.cbegin());
107-
}
108-
109113
Response parseResponse(response::Value&& response)
110114
{
111115
Response result;
@@ -115,4 +119,5 @@ Response parseResponse(response::Value&& response)
115119
return result;
116120
}
117121

118-
} // namespace graphql::mutation::CompleteTaskMutation
122+
} // namespace mutation::CompleteTaskMutation
123+
} // namespace graphql::client

samples/client/MutateClient.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static_assert(graphql::internal::MinorVersion == 6, "regenerate with clientgen:
4545
/// }
4646
/// }
4747
/// </code>
48-
namespace graphql::mutation::CompleteTaskMutation {
48+
namespace graphql::client::mutation::CompleteTaskMutation {
4949

5050
// Return the original text of the request document.
5151
const std::string& GetRequestText() noexcept;
@@ -97,6 +97,6 @@ struct Response
9797

9898
Response parseResponse(response::Value&& response);
9999

100-
} // namespace graphql::mutation::CompleteTaskMutation
100+
} // namespace graphql::client::mutation::CompleteTaskMutation
101101

102102
#endif // MUTATECLIENT_H

samples/client/QueryClient.cpp

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,35 @@
1515

1616
using namespace std::literals;
1717

18-
namespace graphql::query::Query {
18+
namespace graphql::client {
19+
20+
using namespace query::Query;
21+
22+
static const std::array<std::string_view, 4> s_namesTaskState = {
23+
"New"sv,
24+
"Started"sv,
25+
"Complete"sv,
26+
"Unassigned"sv,
27+
};
28+
29+
TaskState parseTaskState(const response::Value& value)
30+
{
31+
if (!value.maybe_enum())
32+
{
33+
throw std::logic_error { "not a valid TaskState value" };
34+
}
35+
36+
const auto itr = std::find(s_namesTaskState.cbegin(), s_namesTaskState.cend(), value.get<response::StringType>());
37+
38+
if (itr == s_namesTaskState.cend())
39+
{
40+
throw std::logic_error { "not a valid TaskState value" };
41+
}
42+
43+
return static_cast<TaskState>(itr - s_namesTaskState.cbegin());
44+
}
45+
46+
namespace query::Query {
1947

2048
const std::string& GetRequestText() noexcept
2149
{
@@ -78,30 +106,6 @@ const peg::ast& GetRequestObject() noexcept
78106
return s_request;
79107
}
80108

81-
static const std::array<std::string_view, 4> s_namesTaskState = {
82-
"New"sv,
83-
"Started"sv,
84-
"Complete"sv,
85-
"Unassigned"sv,
86-
};
87-
88-
TaskState parseTaskState(const response::Value& value)
89-
{
90-
if (!value.maybe_enum())
91-
{
92-
throw std::logic_error { "not a valid TaskState value" };
93-
}
94-
95-
const auto itr = std::find(s_namesTaskState.cbegin(), s_namesTaskState.cend(), value.get<response::StringType>());
96-
97-
if (itr == s_namesTaskState.cend())
98-
{
99-
throw std::logic_error { "not a valid TaskState value" };
100-
}
101-
102-
return static_cast<TaskState>(itr - s_namesTaskState.cbegin());
103-
}
104-
105109
Response parseResponse(response::Value&& response)
106110
{
107111
Response result;
@@ -114,4 +118,5 @@ Response parseResponse(response::Value&& response)
114118
return result;
115119
}
116120

117-
} // namespace graphql::query::Query
121+
} // namespace query::Query
122+
} // namespace graphql::client

samples/client/QueryClient.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static_assert(graphql::internal::MinorVersion == 6, "regenerate with clientgen:
2828

2929

3030
/// <summary>
31-
/// Operation: query (default)
31+
/// Operation: query (unnamed)
3232
/// </summary>
3333
/// <code class="language-graphql">
3434
/// # Copyright (c) Microsoft Corporation. All rights reserved.
@@ -71,7 +71,7 @@ static_assert(graphql::internal::MinorVersion == 6, "regenerate with clientgen:
7171
/// testTaskState
7272
/// }
7373
/// </code>
74-
namespace graphql::query::Query {
74+
namespace graphql::client::query::Query {
7575

7676
// Return the original text of the request document.
7777
const std::string& GetRequestText() noexcept;
@@ -155,6 +155,6 @@ struct Response
155155

156156
Response parseResponse(response::Value&& response);
157157

158-
} // namespace graphql::query::Query
158+
} // namespace graphql::client::query::Query
159159

160160
#endif // QUERYCLIENT_H

samples/client/SubscribeClient.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515

1616
using namespace std::literals;
1717

18-
namespace graphql::subscription::TestSubscription {
18+
namespace graphql::client {
19+
20+
using namespace subscription::TestSubscription;
21+
22+
namespace subscription::TestSubscription {
1923

2024
const std::string& GetRequestText() noexcept
2125
{
@@ -59,4 +63,5 @@ Response parseResponse(response::Value&& response)
5963
return result;
6064
}
6165

62-
} // namespace graphql::subscription::TestSubscription
66+
} // namespace subscription::TestSubscription
67+
} // namespace graphql::client

samples/client/SubscribeClient.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static_assert(graphql::internal::MinorVersion == 6, "regenerate with clientgen:
4343
/// }
4444
/// }
4545
/// </code>
46-
namespace graphql::subscription::TestSubscription {
46+
namespace graphql::client::subscription::TestSubscription {
4747

4848
// Return the original text of the request document.
4949
const std::string& GetRequestText() noexcept;
@@ -66,6 +66,6 @@ struct Response
6666

6767
Response parseResponse(response::Value&& response);
6868

69-
} // namespace graphql::subscription::TestSubscription
69+
} // namespace graphql::client::subscription::TestSubscription
7070

7171
#endif // SUBSCRIBECLIENT_H

0 commit comments

Comments
 (0)