Skip to content

Commit b8f5208

Browse files
committed
Add namespaces to clientgen output
1 parent 8bd60de commit b8f5208

File tree

10 files changed

+314
-1083
lines changed

10 files changed

+314
-1083
lines changed

include/ClientGenerator.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#ifndef CLIENTGENERATOR_H
77
#define CLIENTGENERATOR_H
88

9-
#include "SchemaLoader.h"
109
#include "RequestLoader.h"
10+
#include "SchemaLoader.h"
1111

1212
namespace graphql::generator::client {
1313

@@ -38,12 +38,13 @@ class Generator
3838
std::string getSourceDir() const noexcept;
3939
std::string getHeaderPath() const noexcept;
4040
std::string getSourcePath() const noexcept;
41+
const std::string& getClientNamespace() const noexcept;
4142

4243
bool outputHeader() const noexcept;
43-
void outputRequestComment(std::ostream& headerFile) const noexcept;
44+
void outputGetRequestDeclaration(std::ostream& headerFile) const noexcept;
4445

4546
bool outputSource() const noexcept;
46-
void outputGetRequest(std::ostream& sourceFile) const noexcept;
47+
void outputGetRequestImplementation(std::ostream& sourceFile) const noexcept;
4748

4849
const SchemaLoader _schemaLoader;
4950
const RequestLoader _requestLoader;

include/RequestLoader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ class RequestLoader
5555
explicit RequestLoader(RequestOptions&& requestOptions, const SchemaLoader& schemaLoader);
5656

5757
std::string_view getRequestFilename() const noexcept;
58-
std::string_view getOperationName() const noexcept;
58+
std::string_view getOperationDisplayName() const noexcept;
59+
std::string getOperationNamespace() const noexcept;
5960
std::string_view getOperationType() const noexcept;
6061
std::string_view getRequestText() const noexcept;
6162

samples/client/MutateClient.cpp

Lines changed: 28 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -3,133 +3,52 @@
33

44
// WARNING! Do not edit this file manually, your changes will be overwritten.
55

6-
#include "graphqlservice/introspection/Introspection.h"
7-
8-
#include "graphqlservice/GraphQLParse.h"
6+
#include "MutateClient.h"
97

108
#include <algorithm>
119
#include <array>
1210
#include <functional>
1311
#include <sstream>
1412
#include <stdexcept>
1513
#include <string_view>
16-
#include <tuple>
17-
#include <vector>
1814

1915
using namespace std::literals;
2016

21-
namespace graphql {
22-
namespace service {
23-
24-
static const std::array<std::string_view, 4> s_namesTaskState = {
25-
"New",
26-
"Started",
27-
"Complete",
28-
"Unassigned"
29-
};
17+
namespace graphql::mutation::CompleteTaskMutation {
3018

31-
template <>
32-
mutate::TaskState ModifiedArgument<mutate::TaskState>::convert(const response::Value& value)
19+
const std::string& GetRequestText() noexcept
3320
{
34-
if (!value.maybe_enum())
35-
{
36-
throw service::schema_exception { { "not a valid TaskState value" } };
37-
}
38-
39-
auto itr = std::find(s_namesTaskState.cbegin(), s_namesTaskState.cend(), value.get<response::StringType>());
40-
41-
if (itr == s_namesTaskState.cend())
42-
{
43-
throw service::schema_exception { { "not a valid TaskState value" } };
44-
}
45-
46-
return static_cast<mutate::TaskState>(itr - s_namesTaskState.cbegin());
21+
static const auto s_request = R"gql(
22+
# Copyright (c) Microsoft Corporation. All rights reserved.
23+
# Licensed under the MIT License.
24+
25+
mutation CompleteTaskMutation($input: CompleteTaskInput! = {id: "ZmFrZVRhc2tJZA==", isComplete: true, clientMutationId: "Hi There!"}) {
26+
completedTask: completeTask(input: $input) {
27+
completedTask: task {
28+
completedTaskId: id
29+
title
30+
isComplete
31+
}
32+
clientMutationId
33+
}
34+
}
35+
)gql"s;
36+
37+
return s_request;
4738
}
4839

49-
template <>
50-
std::future<service::ResolverResult> ModifiedResult<mutate::TaskState>::convert(service::FieldResult<mutate::TaskState>&& result, ResolverParams&& params)
40+
const peg::ast& GetRequestObject() noexcept
5141
{
52-
return resolve(std::move(result), std::move(params),
53-
[](mutate::TaskState&& value, const ResolverParams&)
54-
{
55-
response::Value result(response::Type::EnumValue);
56-
57-
result.set<response::StringType>(std::string(s_namesTaskState[static_cast<size_t>(value)]));
42+
static const auto s_request = []() noexcept {
43+
auto ast = peg::parseString(GetRequestText());
5844

59-
return result;
60-
});
61-
}
45+
// This has already been validated against the schema by clientgen.
46+
ast.validated = true;
6247

63-
template <>
64-
mutate::CompleteTaskInput ModifiedArgument<mutate::CompleteTaskInput>::convert(const response::Value& value)
65-
{
66-
const auto defaultValue = []()
67-
{
68-
response::Value values(response::Type::Map);
69-
response::Value entry;
70-
71-
72-
return values;
48+
return ast;
7349
}();
7450

75-
76-
return {
77-
std::move(valueId),
78-
std::move(valueIsComplete),
79-
std::move(valueClientMutationId)
80-
};
81-
}
82-
83-
} /* namespace service */
84-
85-
namespace mutate {
86-
87-
std::string_view GetRequestText() noexcept
88-
{
89-
return R"gql(# Copyright (c) Microsoft Corporation. All rights reserved.
90-
# Licensed under the MIT License.
91-
92-
mutation CompleteTaskMutation($input: CompleteTaskInput! = {id: "ZmFrZVRhc2tJZA==", isComplete: true, clientMutationId: "Hi There!"}) {
93-
completedTask: completeTask(input: $input) {
94-
completedTask: task {
95-
completedTaskId: id
96-
title
97-
isComplete
98-
}
99-
clientMutationId
100-
}
101-
})gql"sv;
102-
}
103-
namespace object {
104-
105-
} /* namespace object */
106-
107-
Operations::Operations(std::shared_ptr<object::Query> query, std::shared_ptr<object::Mutation> mutation, std::shared_ptr<object::Subscription> subscription)
108-
: service::Request({
109-
{ "query", query },
110-
{ "mutation", mutation },
111-
{ "subscription", subscription }
112-
}, GetClient())
113-
, _query(std::move(query))
114-
, _mutation(std::move(mutation))
115-
, _subscription(std::move(subscription))
116-
{
117-
}
118-
119-
std::shared_ptr<client::Client> GetClient()
120-
{
121-
static std::weak_ptr<client::Client> s_wpClient;
122-
auto client = s_wpClient.lock();
123-
124-
if (!client)
125-
{
126-
client = std::make_shared<client::Client>(false);
127-
AddTypesToClient(client);
128-
s_wpClient = client;
129-
}
130-
131-
return client;
51+
return s_request;
13252
}
13353

134-
} /* namespace mutate */
135-
} /* namespace graphql */
54+
} /* namespace graphql::mutation::CompleteTaskMutation */

samples/client/MutateClient.h

Lines changed: 30 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -8,99 +8,49 @@
88
#ifndef MUTATECLIENT_H
99
#define MUTATECLIENT_H
1010

11-
#include "graphqlservice/GraphQLClient.h"
12-
#include "graphqlservice/GraphQLService.h"
11+
// Copyright (c) Microsoft Corporation. All rights reserved.
12+
// Licensed under the MIT License.
13+
14+
// WARNING! Do not edit this file manually, your changes will be overwritten.
15+
16+
#include "graphqlservice/GraphQLParse.h"
17+
#include "graphqlservice/GraphQLResponse.h"
1318

1419
// Check if the library version is compatible with clientgen 3.6.0
1520
static_assert(graphql::internal::MajorVersion == 3, "regenerate with clientgen: major version mismatch");
1621
static_assert(graphql::internal::MinorVersion == 6, "regenerate with clientgen: minor version mismatch");
1722

18-
#include <memory>
23+
#include <optional>
1924
#include <string>
20-
#include <string_view>
25+
#include <variant>
2126
#include <vector>
2227

23-
namespace graphql {
24-
namespace mutate {
28+
namespace graphql::mutation::CompleteTaskMutation {
2529

2630
/** Operation: mutation CompleteTaskMutation
27-
28-
# Copyright (c) Microsoft Corporation. All rights reserved.
29-
# Licensed under the MIT License.
30-
31-
mutation CompleteTaskMutation($input: CompleteTaskInput! = {id: "ZmFrZVRhc2tJZA==", isComplete: true, clientMutationId: "Hi There!"}) {
32-
completedTask: completeTask(input: $input) {
33-
completedTask: task {
34-
completedTaskId: id
35-
title
36-
isComplete
37-
}
38-
clientMutationId
39-
}
40-
}
41-
42-
**/
31+
**
32+
** # Copyright (c) Microsoft Corporation. All rights reserved.
33+
** # Licensed under the MIT License.
34+
**
35+
** mutation CompleteTaskMutation($input: CompleteTaskInput! = {id: "ZmFrZVRhc2tJZA==", isComplete: true, clientMutationId: "Hi There!"}) {
36+
** completedTask: completeTask(input: $input) {
37+
** completedTask: task {
38+
** completedTaskId: id
39+
** title
40+
** isComplete
41+
** }
42+
** clientMutationId
43+
** }
44+
** }
45+
**
46+
**/
4347

4448
// Return the original text of the request document.
45-
std::string_view GetRequestText() noexcept;
46-
47-
48-
enum class TaskState
49-
{
50-
New,
51-
Started,
52-
Complete,
53-
Unassigned
54-
};
55-
56-
struct CompleteTaskInput
57-
{
58-
};
59-
60-
namespace object {
61-
62-
class Query;
63-
class PageInfo;
64-
class AppointmentEdge;
65-
class AppointmentConnection;
66-
class TaskEdge;
67-
class TaskConnection;
68-
class FolderEdge;
69-
class FolderConnection;
70-
class CompleteTaskPayload;
71-
class Mutation;
72-
class Subscription;
73-
class Appointment;
74-
class Task;
75-
class Folder;
76-
class NestedType;
77-
class Expensive;
78-
79-
} /* namespace object */
80-
81-
struct Node
82-
{
83-
};
84-
85-
namespace object {
86-
87-
} /* namespace object */
88-
89-
class Operations
90-
: public service::Request
91-
{
92-
public:
93-
explicit Operations(std::shared_ptr<object::Query> query, std::shared_ptr<object::Mutation> mutation, std::shared_ptr<object::Subscription> subscription);
94-
95-
private:
96-
std::shared_ptr<object::Query> _query;
97-
std::shared_ptr<object::Mutation> _mutation;
98-
std::shared_ptr<object::Subscription> _subscription;
99-
};
49+
const std::string& GetRequestText() noexcept;
10050

101-
std::shared_ptr<client::Client> GetClient();
51+
// Return a pre-parsed, pre-validated request object.
52+
const peg::ast& GetRequestObject() noexcept;
10253

103-
} /* namespace mutate */
104-
} /* namespace graphql */
54+
} /* namespace graphql::mutation::CompleteTaskMutation */
10555

10656
#endif // MUTATECLIENT_H

0 commit comments

Comments
 (0)