Skip to content

Commit 173fb57

Browse files
authored
Merge pull request #318 from wravery/next
feat(sharedtypes): add clientgen --shared-types param to reuse types in schema
2 parents 658bc81 + 4e0cc8c commit 173fb57

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1162
-649
lines changed

include/RequestLoader.h

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,30 +84,32 @@ struct [[nodiscard("unnecessary construction")]] RequestOptions
8484
const std::string requestFilename;
8585
const std::optional<std::string> operationName;
8686
const bool noIntrospection = false;
87+
const bool sharedTypes = false;
8788
};
8889

8990
class SchemaLoader;
9091

9192
class [[nodiscard("unnecessary construction")]] RequestLoader
9293
{
9394
public:
94-
explicit RequestLoader(RequestOptions && requestOptions, const SchemaLoader& schemaLoader);
95+
explicit RequestLoader(RequestOptions&& requestOptions, const SchemaLoader& schemaLoader);
9596

9697
[[nodiscard("unnecessary call")]] std::string_view getRequestFilename() const noexcept;
9798
[[nodiscard("unnecessary call")]] const OperationList& getOperations() const noexcept;
9899
[[nodiscard("unnecessary call")]] std::string_view getOperationDisplayName(
99100
const Operation& operation) const noexcept;
100-
[[nodiscard("unnecessary call")]] std::string getOperationNamespace(const Operation& operation)
101-
const noexcept;
102-
[[nodiscard("unnecessary call")]] std::string_view getOperationType(const Operation& operation)
103-
const noexcept;
101+
[[nodiscard("unnecessary call")]] std::string getOperationNamespace(
102+
const Operation& operation) const noexcept;
103+
[[nodiscard("unnecessary call")]] std::string_view getOperationType(
104+
const Operation& operation) const noexcept;
104105
[[nodiscard("unnecessary call")]] std::string_view getRequestText() const noexcept;
105106

106107
[[nodiscard("unnecessary call")]] const ResponseType& getResponseType(
107108
const Operation& operation) const noexcept;
108109
[[nodiscard("unnecessary call")]] const RequestVariableList& getVariables(
109110
const Operation& operation) const noexcept;
110111

112+
[[nodiscard("unnecessary call")]] bool useSharedTypes() const noexcept;
111113
[[nodiscard("unnecessary call")]] const RequestInputTypeList& getReferencedInputTypes(
112114
const Operation& operation) const noexcept;
113115
[[nodiscard("unnecessary call")]] const RequestSchemaTypeList& getReferencedEnums(
@@ -116,32 +118,30 @@ class [[nodiscard("unnecessary construction")]] RequestLoader
116118
[[nodiscard("unnecessary call")]] std::string getInputCppType(
117119
const RequestSchemaType& wrappedInputType) const noexcept;
118120
[[nodiscard("unnecessary call")]] std::string getInputCppType(
119-
const RequestSchemaType& inputType,
120-
const TypeModifierStack& modifiers) const noexcept;
121+
const RequestSchemaType& inputType, const TypeModifierStack& modifiers) const noexcept;
121122
[[nodiscard("unnecessary call")]] static std::string getOutputCppType(
122-
std::string_view outputCppType,
123-
const TypeModifierStack& modifiers) noexcept;
123+
std::string_view outputCppType, const TypeModifierStack& modifiers) noexcept;
124124

125125
[[nodiscard("unnecessary call")]] static std::pair<RequestSchemaType, TypeModifierStack>
126-
unwrapSchemaType(RequestSchemaType && type) noexcept;
126+
unwrapSchemaType(RequestSchemaType&& type) noexcept;
127127

128128
private:
129129
void buildSchema();
130130
void addTypesToSchema();
131-
[[nodiscard("unnecessary call")]] RequestSchemaType getSchemaType(std::string_view type,
132-
const TypeModifierStack& modifiers) const noexcept;
131+
[[nodiscard("unnecessary call")]] RequestSchemaType getSchemaType(
132+
std::string_view type, const TypeModifierStack& modifiers) const noexcept;
133133
void validateRequest() const;
134134

135135
[[nodiscard("unnecessary call")]] static std::string_view trimWhitespace(
136136
std::string_view content) noexcept;
137137

138138
void findOperation();
139139
void collectFragments() noexcept;
140-
void collectVariables(Operation & operation) noexcept;
141-
void collectInputTypes(Operation & operation, const RequestSchemaType& variableType) noexcept;
142-
void reorderInputTypeDependencies(Operation & operation);
143-
void collectEnums(Operation & operation, const RequestSchemaType& variableType) noexcept;
144-
void collectEnums(Operation & operation, const ResponseField& responseField) noexcept;
140+
void collectVariables(Operation& operation) noexcept;
141+
void collectInputTypes(Operation& operation, const RequestSchemaType& variableType) noexcept;
142+
void reorderInputTypeDependencies(Operation& operation);
143+
void collectEnums(Operation& operation, const RequestSchemaType& variableType) noexcept;
144+
void collectEnums(Operation& operation, const ResponseField& responseField) noexcept;
145145

146146
using FragmentDefinitionMap = std::map<std::string_view, const peg::ast_node*>;
147147

@@ -150,8 +150,7 @@ class [[nodiscard("unnecessary construction")]] RequestLoader
150150
{
151151
public:
152152
explicit SelectionVisitor(const SchemaLoader& schemaLoader,
153-
const FragmentDefinitionMap& fragments,
154-
const std::shared_ptr<schema::Schema>& schema,
153+
const FragmentDefinitionMap& fragments, const std::shared_ptr<schema::Schema>& schema,
155154
const RequestSchemaType& type);
156155

157156
void visit(const peg::ast_node& selection);
@@ -163,7 +162,7 @@ class [[nodiscard("unnecessary construction")]] RequestLoader
163162
void visitFragmentSpread(const peg::ast_node& fragmentSpread);
164163
void visitInlineFragment(const peg::ast_node& inlineFragment);
165164

166-
void mergeFragmentFields(ResponseFieldList && fragmentFields) noexcept;
165+
void mergeFragmentFields(ResponseFieldList&& fragmentFields) noexcept;
167166

168167
const SchemaLoader& _schemaLoader;
169168
const FragmentDefinitionMap& _fragments;

samples/client/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ add_subdirectory(subscribe)
99
add_subdirectory(nestedinput)
1010
add_subdirectory(multiple)
1111

12-
add_subdirectory(benchmark)
13-
1412
if(GRAPHQL_BUILD_MODULES)
1513
# client_benchmark
14+
add_subdirectory(benchmark)
1615
add_executable(client_benchmark benchmark.cpp)
1716
target_link_libraries(client_benchmark PRIVATE
18-
todaygraphql
1917
benchmark_client)
2018

2119
if(WIN32 AND BUILD_SHARED_LIBS)

samples/client/benchmark.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ import GraphQL.Client;
1616
import GraphQL.Service;
1717

1818
import GraphQL.Today.Mock;
19-
20-
import GraphQL.Benchmark.BenchmarkClient;
19+
import GraphQL.Today.TodayClient;
2120

2221
using namespace graphql;
2322

@@ -93,7 +92,7 @@ int main(int argc, char** argv)
9392

9493
try
9594
{
96-
using namespace client::query::Query;
95+
using namespace today::client::query::Query;
9796

9897
auto query = GetRequestObject();
9998
const auto& name = GetOperationName();

samples/client/benchmark/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ cmake_minimum_required(VERSION 3.28)
77
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/cppgraphqlgen-functions.cmake)
88

99
if(GRAPHQL_UPDATE_SAMPLES AND GRAPHQL_BUILD_CLIENTGEN)
10-
update_graphql_client_files(benchmark ../../today/schema.today.graphql client.benchmark.today.graphql Benchmark benchmark)
10+
update_graphql_client_files(benchmark ../../today/schema.today.graphql client.benchmark.today.graphql Today today --shared-types)
1111
endif()
1212

1313
add_graphql_client_target(benchmark)
14+
target_link_libraries(benchmark_client PRIVATE todaygraphql)

samples/client/benchmark/BenchmarkClient.cpp renamed to samples/client/benchmark/TodayClient.cpp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

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

6-
#include "BenchmarkClient.h"
6+
#include "TodayClient.h"
77

88
#include "graphqlservice/internal/SortedMap.h"
99

@@ -16,8 +16,9 @@
1616

1717
using namespace std::literals;
1818

19-
namespace graphql::client {
20-
namespace benchmark {
19+
namespace graphql {
20+
namespace today {
21+
namespace client {
2122

2223
const std::string& GetRequestText() noexcept
2324
{
@@ -59,14 +60,16 @@ const peg::ast& GetRequestObject() noexcept
5960
return s_request;
6061
}
6162

62-
} // namespace benchmark
63+
} // namespace client
64+
} // namespace today
65+
namespace client {
6366

64-
using namespace benchmark;
67+
using namespace today;
6568

6669
template <>
67-
query::Query::Response::appointments_AppointmentConnection::pageInfo_PageInfo Response<query::Query::Response::appointments_AppointmentConnection::pageInfo_PageInfo>::parse(response::Value&& response)
70+
graphql::today::client::query::Query::Response::appointments_AppointmentConnection::pageInfo_PageInfo Response<graphql::today::client::query::Query::Response::appointments_AppointmentConnection::pageInfo_PageInfo>::parse(response::Value&& response)
6871
{
69-
query::Query::Response::appointments_AppointmentConnection::pageInfo_PageInfo result;
72+
graphql::today::client::query::Query::Response::appointments_AppointmentConnection::pageInfo_PageInfo result;
7073

7174
if (response.type() == response::Type::Map)
7275
{
@@ -86,9 +89,9 @@ query::Query::Response::appointments_AppointmentConnection::pageInfo_PageInfo Re
8689
}
8790

8891
template <>
89-
query::Query::Response::appointments_AppointmentConnection::edges_AppointmentEdge::node_Appointment Response<query::Query::Response::appointments_AppointmentConnection::edges_AppointmentEdge::node_Appointment>::parse(response::Value&& response)
92+
graphql::today::client::query::Query::Response::appointments_AppointmentConnection::edges_AppointmentEdge::node_Appointment Response<graphql::today::client::query::Query::Response::appointments_AppointmentConnection::edges_AppointmentEdge::node_Appointment>::parse(response::Value&& response)
9093
{
91-
query::Query::Response::appointments_AppointmentConnection::edges_AppointmentEdge::node_Appointment result;
94+
graphql::today::client::query::Query::Response::appointments_AppointmentConnection::edges_AppointmentEdge::node_Appointment result;
9295

9396
if (response.type() == response::Type::Map)
9497
{
@@ -123,9 +126,9 @@ query::Query::Response::appointments_AppointmentConnection::edges_AppointmentEdg
123126
}
124127

125128
template <>
126-
query::Query::Response::appointments_AppointmentConnection::edges_AppointmentEdge Response<query::Query::Response::appointments_AppointmentConnection::edges_AppointmentEdge>::parse(response::Value&& response)
129+
graphql::today::client::query::Query::Response::appointments_AppointmentConnection::edges_AppointmentEdge Response<graphql::today::client::query::Query::Response::appointments_AppointmentConnection::edges_AppointmentEdge>::parse(response::Value&& response)
127130
{
128-
query::Query::Response::appointments_AppointmentConnection::edges_AppointmentEdge result;
131+
graphql::today::client::query::Query::Response::appointments_AppointmentConnection::edges_AppointmentEdge result;
129132

130133
if (response.type() == response::Type::Map)
131134
{
@@ -135,7 +138,7 @@ query::Query::Response::appointments_AppointmentConnection::edges_AppointmentEdg
135138
{
136139
if (member.first == R"js(node)js"sv)
137140
{
138-
result.node = ModifiedResponse<query::Query::Response::appointments_AppointmentConnection::edges_AppointmentEdge::node_Appointment>::parse<TypeModifier::Nullable>(std::move(member.second));
141+
result.node = ModifiedResponse<graphql::today::client::query::Query::Response::appointments_AppointmentConnection::edges_AppointmentEdge::node_Appointment>::parse<TypeModifier::Nullable>(std::move(member.second));
139142
continue;
140143
}
141144
}
@@ -145,9 +148,9 @@ query::Query::Response::appointments_AppointmentConnection::edges_AppointmentEdg
145148
}
146149

147150
template <>
148-
query::Query::Response::appointments_AppointmentConnection Response<query::Query::Response::appointments_AppointmentConnection>::parse(response::Value&& response)
151+
graphql::today::client::query::Query::Response::appointments_AppointmentConnection Response<graphql::today::client::query::Query::Response::appointments_AppointmentConnection>::parse(response::Value&& response)
149152
{
150-
query::Query::Response::appointments_AppointmentConnection result;
153+
graphql::today::client::query::Query::Response::appointments_AppointmentConnection result;
151154

152155
if (response.type() == response::Type::Map)
153156
{
@@ -157,12 +160,12 @@ query::Query::Response::appointments_AppointmentConnection Response<query::Query
157160
{
158161
if (member.first == R"js(pageInfo)js"sv)
159162
{
160-
result.pageInfo = ModifiedResponse<query::Query::Response::appointments_AppointmentConnection::pageInfo_PageInfo>::parse(std::move(member.second));
163+
result.pageInfo = ModifiedResponse<graphql::today::client::query::Query::Response::appointments_AppointmentConnection::pageInfo_PageInfo>::parse(std::move(member.second));
161164
continue;
162165
}
163166
if (member.first == R"js(edges)js"sv)
164167
{
165-
result.edges = ModifiedResponse<query::Query::Response::appointments_AppointmentConnection::edges_AppointmentEdge>::parse<TypeModifier::Nullable, TypeModifier::List, TypeModifier::Nullable>(std::move(member.second));
168+
result.edges = ModifiedResponse<graphql::today::client::query::Query::Response::appointments_AppointmentConnection::edges_AppointmentEdge>::parse<TypeModifier::Nullable, TypeModifier::List, TypeModifier::Nullable>(std::move(member.second));
166169
continue;
167170
}
168171
}
@@ -171,7 +174,9 @@ query::Query::Response::appointments_AppointmentConnection Response<query::Query
171174
return result;
172175
}
173176

174-
namespace query::Query {
177+
} // namespace client
178+
179+
namespace today::client::query::Query {
175180

176181
const std::string& GetOperationName() noexcept
177182
{
@@ -182,6 +187,8 @@ const std::string& GetOperationName() noexcept
182187

183188
Response parseResponse(response::Value&& response)
184189
{
190+
using namespace graphql::client;
191+
185192
Response result;
186193

187194
if (response.type() == response::Type::Map)
@@ -203,12 +210,12 @@ Response parseResponse(response::Value&& response)
203210

204211
[[nodiscard("unnecessary call")]] const std::string& Traits::GetRequestText() noexcept
205212
{
206-
return benchmark::GetRequestText();
213+
return client::GetRequestText();
207214
}
208215

209216
[[nodiscard("unnecessary call")]] const peg::ast& Traits::GetRequestObject() noexcept
210217
{
211-
return benchmark::GetRequestObject();
218+
return client::GetRequestObject();
212219
}
213220

214221
[[nodiscard("unnecessary call")]] const std::string& Traits::GetOperationName() noexcept
@@ -221,5 +228,5 @@ Response parseResponse(response::Value&& response)
221228
return Query::parseResponse(std::move(response));
222229
}
223230

224-
} // namespace query::Query
225-
} // namespace graphql::client
231+
} // namespace today::client::query::Query
232+
} // namespace graphql

samples/client/benchmark/BenchmarkClient.h renamed to samples/client/benchmark/TodayClient.h

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55

66
#pragma once
77

8-
#ifndef BENCHMARKCLIENT_H
9-
#define BENCHMARKCLIENT_H
8+
#ifndef TODAYCLIENT_H
9+
#define TODAYCLIENT_H
1010

1111
#include "graphqlservice/GraphQLClient.h"
1212
#include "graphqlservice/GraphQLParse.h"
1313
#include "graphqlservice/GraphQLResponse.h"
1414

1515
#include "graphqlservice/internal/Version.h"
1616

17+
#include "TodaySchema.h"
18+
1719
#include <optional>
1820
#include <string>
1921
#include <vector>
@@ -22,12 +24,10 @@
2224
static_assert(graphql::internal::MajorVersion == 5, "regenerate with clientgen: major version mismatch");
2325
static_assert(graphql::internal::MinorVersion == 0, "regenerate with clientgen: minor version mismatch");
2426

25-
namespace graphql::client {
27+
namespace graphql::today {
2628

27-
/// <summary>
28-
/// Operation: query (unnamed)
29-
/// </summary>
30-
/// <code class="language-graphql">
29+
/// # Operation: query (unnamed)
30+
/// ```graphql
3131
/// # Copyright (c) Microsoft Corporation. All rights reserved.
3232
/// # Licensed under the MIT License.
3333
///
@@ -46,21 +46,19 @@ namespace graphql::client {
4646
/// }
4747
/// }
4848
/// }
49-
/// </code>
50-
namespace benchmark {
49+
/// ```
50+
namespace client {
5151

5252
// Return the original text of the request document.
5353
[[nodiscard("unnecessary call")]] const std::string& GetRequestText() noexcept;
5454

5555
// Return a pre-parsed, pre-validated request object.
5656
[[nodiscard("unnecessary call")]] const peg::ast& GetRequestObject() noexcept;
5757

58-
} // namespace benchmark
59-
6058
namespace query::Query {
6159

62-
using benchmark::GetRequestText;
63-
using benchmark::GetRequestObject;
60+
using graphql::today::client::GetRequestText;
61+
using graphql::today::client::GetRequestObject;
6462

6563
// Return the name of this operation in the shared request document.
6664
[[nodiscard("unnecessary call")]] const std::string& GetOperationName() noexcept;
@@ -108,6 +106,7 @@ struct Traits
108106
};
109107

110108
} // namespace query::Query
111-
} // namespace graphql::client
109+
} // namespace client
110+
} // namespace graphql::today
112111

113-
#endif // BENCHMARKCLIENT_H
112+
#endif // TODAYCLIENT_H

samples/client/benchmark/BenchmarkClient.ixx renamed to samples/client/benchmark/TodayClient.ixx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,21 @@
55

66
module;
77

8-
#include "BenchmarkClient.h"
8+
#include "TodayClient.h"
99

10-
export module GraphQL.Benchmark.BenchmarkClient;
10+
export module GraphQL.Today.TodayClient;
1111

12-
export namespace graphql::client {
12+
export namespace graphql::today {
1313

14-
namespace benchmark {
14+
namespace client {
1515

16-
using benchmark::GetRequestText;
17-
using benchmark::GetRequestObject;
18-
19-
} // namespace benchmark
16+
using client::GetRequestText;
17+
using client::GetRequestObject;
2018

2119
namespace query::Query {
2220

23-
using benchmark::GetRequestText;
24-
using benchmark::GetRequestObject;
21+
using graphql::today::client::GetRequestText;
22+
using graphql::today::client::GetRequestObject;
2523
using Query::GetOperationName;
2624

2725
using Query::Response;
@@ -31,4 +29,5 @@ using Query::Traits;
3129

3230
} // namespace query::Query
3331

34-
} // namespace graphql::client
32+
} // namespace client
33+
} // namespace graphql::today

0 commit comments

Comments
 (0)