Skip to content

Commit 4d54be8

Browse files
authored
Merge pull request #319 from wravery/next
more tweaks for the next release candidate
2 parents 173fb57 + 29c3c83 commit 4d54be8

20 files changed

+474
-70
lines changed

cmake/cppgraphqlgen-functions.cmake

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,23 @@ function(update_graphql_schema_files SCHEMA_TARGET SCHEMA_GRAPHQL SCHEMA_PREFIX
3636
DEPENDS ${SCHEMA_GRAPHQL} ${GRAPHQL_UPDATE_SCHEMA_FILES_SCRIPT} cppgraphqlgen::schemagen
3737
COMMENT "Generating ${SCHEMA_TARGET} GraphQL schema"
3838
VERBATIM)
39-
endfunction()
4039

41-
function(add_graphql_schema_target SCHEMA_TARGET)
4240
add_custom_target(${SCHEMA_TARGET}_update_schema ALL
4341
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${SCHEMA_TARGET}_schema_files)
42+
file(REAL_PATH ${SCHEMA_GRAPHQL} SCHEMA_GRAPHQL BASE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
43+
set_target_properties(${SCHEMA_TARGET}_update_schema PROPERTIES
44+
SCHEMA_GRAPHQL ${SCHEMA_GRAPHQL}
45+
SCHEMA_PREFIX ${SCHEMA_PREFIX}
46+
SCHEMA_NAMESPACE ${SCHEMA_NAMESPACE})
47+
endfunction()
4448

49+
function(add_graphql_schema_target SCHEMA_TARGET)
4550
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${SCHEMA_TARGET}_schema_files)
4651
file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/${SCHEMA_TARGET}_schema_files SCHEMA_FILES)
4752
add_library(${SCHEMA_TARGET}_schema STATIC ${SCHEMA_FILES})
48-
add_dependencies(${SCHEMA_TARGET}_schema ${SCHEMA_TARGET}_update_schema)
53+
if(TARGET ${SCHEMA_TARGET}_update_schema)
54+
add_dependencies(${SCHEMA_TARGET}_schema ${SCHEMA_TARGET}_update_schema)
55+
endif()
4956
target_compile_features(${SCHEMA_TARGET}_schema PUBLIC cxx_std_20)
5057
target_include_directories(${SCHEMA_TARGET}_schema PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
5158
target_link_libraries(${SCHEMA_TARGET}_schema PUBLIC cppgraphqlgen::graphqlservice)
@@ -90,19 +97,63 @@ function(update_graphql_client_files CLIENT_TARGET SCHEMA_GRAPHQL REQUEST_GRAPHQ
9097
DEPENDS ${SCHEMA_GRAPHQL} ${REQUEST_GRAPHQL} ${GRAPHQL_UPDATE_CLIENT_FILES_SCRIPT} cppgraphqlgen::clientgen
9198
COMMENT "Generating ${CLIENT_TARGET} client"
9299
VERBATIM)
100+
101+
add_custom_target(${CLIENT_TARGET}_update_client ALL
102+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${CLIENT_TARGET}_client_files)
93103
endfunction()
94104

95-
function(add_graphql_client_target CLIENT_TARGET)
105+
function(update_graphql_shared_client_files CLIENT_TARGET SCHEMA_TARGET REQUEST_GRAPHQL)
106+
set_property(DIRECTORY APPEND
107+
PROPERTY CMAKE_CONFIGURE_DEPENDS ${CLIENT_TARGET}_client_files)
108+
get_target_property(SCHEMA_GRAPHQL ${SCHEMA_TARGET}_update_schema SCHEMA_GRAPHQL)
109+
file(RELATIVE_PATH SCHEMA_GRAPHQL ${CMAKE_CURRENT_SOURCE_DIR} ${SCHEMA_GRAPHQL})
110+
get_target_property(SCHEMA_PREFIX ${SCHEMA_TARGET}_update_schema SCHEMA_PREFIX)
111+
get_target_property(SCHEMA_NAMESPACE ${SCHEMA_TARGET}_update_schema SCHEMA_NAMESPACE)
112+
113+
# Collect optional arguments
114+
set(ADDITIONAL_CLIENTGEN_ARGS "--shared-types")
115+
if(ARGC GREATER 4)
116+
math(EXPR LAST_ARG "${ARGC} - 1")
117+
foreach(ARGN RANGE 4 ${LAST_ARG})
118+
set(NEXT_ARG "${ARGV${ARGN}}")
119+
list(APPEND ADDITIONAL_CLIENTGEN_ARGS "${NEXT_ARG}")
120+
endforeach()
121+
endif()
122+
123+
add_custom_command(
124+
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/${CLIENT_TARGET}_client_files
125+
COMMAND
126+
${CMAKE_COMMAND} "-DCLIENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}"
127+
"-DCLIENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}"
128+
"-DCLIENTGEN_PROGRAM=$<TARGET_FILE:cppgraphqlgen::clientgen>" "-DCLIENT_TARGET=${CLIENT_TARGET}"
129+
"-DSCHEMA_GRAPHQL=${SCHEMA_GRAPHQL}" "-DREQUEST_GRAPHQL=${REQUEST_GRAPHQL}"
130+
"-DCLIENT_PREFIX=${SCHEMA_PREFIX}" "-DCLIENT_NAMESPACE=${SCHEMA_NAMESPACE}"
131+
"-DADDITIONAL_CLIENTGEN_ARGS=${ADDITIONAL_CLIENTGEN_ARGS}"
132+
-P ${GRAPHQL_UPDATE_CLIENT_FILES_SCRIPT}
133+
DEPENDS ${SCHEMA_GRAPHQL} ${REQUEST_GRAPHQL} ${GRAPHQL_UPDATE_CLIENT_FILES_SCRIPT} cppgraphqlgen::clientgen
134+
COMMENT "Generating ${CLIENT_TARGET} client"
135+
VERBATIM)
136+
96137
add_custom_target(${CLIENT_TARGET}_update_client ALL
97138
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${CLIENT_TARGET}_client_files)
139+
set_target_properties(${CLIENT_TARGET}_update_client PROPERTIES
140+
SCHEMA_TARGET ${SCHEMA_TARGET})
141+
endfunction()
98142

143+
function(add_graphql_client_target CLIENT_TARGET)
99144
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${CLIENT_TARGET}_client_files)
100145
file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/${CLIENT_TARGET}_client_files CLIENT_FILES)
101146
add_library(${CLIENT_TARGET}_client STATIC ${CLIENT_FILES})
102-
add_dependencies(${CLIENT_TARGET}_client ${CLIENT_TARGET}_update_client)
147+
if(TARGET ${CLIENT_TARGET}_update_client)
148+
add_dependencies(${CLIENT_TARGET}_client ${CLIENT_TARGET}_update_client)
149+
endif()
103150
target_compile_features(${CLIENT_TARGET}_client PUBLIC cxx_std_20)
104151
target_include_directories(${CLIENT_TARGET}_client PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
105152
target_link_libraries(${CLIENT_TARGET}_client PUBLIC cppgraphqlgen::graphqlclient)
153+
get_target_property(SCHEMA_TARGET ${CLIENT_TARGET}_update_client SCHEMA_TARGET)
154+
if(SCHEMA_TARGET)
155+
target_link_libraries(${CLIENT_TARGET}_client PUBLIC ${SCHEMA_TARGET}_schema)
156+
endif()
106157
file(GLOB CLIENT_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
107158
target_sources(${CLIENT_TARGET}_client PUBLIC FILE_SET HEADERS
108159
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}

include/SchemaGenerator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ class [[nodiscard("unnecessary construction")]] Generator
4949
std::ostream& moduleFile, std::string_view objectNamespace, std::string_view cppType) const;
5050
void outputObjectImplements(std::ostream& headerFile, const ObjectType& objectType) const;
5151
void outputObjectStubs(std::ostream& headerFile, const ObjectType& objectType) const;
52-
void outputObjectDeclaration(
53-
std::ostream& headerFile, const ObjectType& objectType, bool isQueryType) const;
52+
void outputObjectDeclaration(std::ostream& headerFile, const ObjectType& objectType,
53+
bool isQueryType, bool isSubscriptionType) const;
5454
[[nodiscard("unnecessary memory copy")]] std::string getFieldDeclaration(
5555
const InputField& inputField) const noexcept;
5656
[[nodiscard("unnecessary memory copy")]] std::string getFieldDeclaration(

samples/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
cmake_minimum_required(VERSION 3.28)
55

6-
add_subdirectory(client)
7-
add_subdirectory(learn)
8-
add_subdirectory(validation)
9-
106
if(GRAPHQL_BUILD_MODULES)
117
add_subdirectory(today)
128
endif()
139

10+
add_subdirectory(client)
11+
add_subdirectory(learn)
12+
add_subdirectory(validation)
13+
1414
if(GRAPHQL_BUILD_HTTP_SAMPLE)
1515
find_package(boost_beast CONFIG QUIET)
1616
if(boost_beast_FOUND)

samples/client/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ if(GRAPHQL_BUILD_MODULES)
1414
add_subdirectory(benchmark)
1515
add_executable(client_benchmark benchmark.cpp)
1616
target_link_libraries(client_benchmark PRIVATE
17+
todaygraphql
1718
benchmark_client)
1819

1920
if(WIN32 AND BUILD_SHARED_LIBS)

samples/client/benchmark/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ 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 Today today --shared-types)
10+
update_graphql_shared_client_files(benchmark today client.benchmark.today.graphql)
1111
endif()
1212

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

samples/learn/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ add_library(star_wars STATIC
1111
QueryData.cpp
1212
ReviewData.cpp
1313
MutationData.cpp
14-
StarWarsData.cpp)
14+
StarWarsData.cpp
15+
SubscriptionData.cpp)
1516
target_link_libraries(star_wars PUBLIC learn_schema)
1617
target_include_directories(star_wars INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
1718

samples/learn/StarWarsData.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "MutationData.h"
99
#include "QueryData.h"
1010
#include "ReviewData.h"
11+
#include "SubscriptionData.h"
1112

1213
using namespace std::literals;
1314

@@ -113,7 +114,9 @@ std::shared_ptr<service::Request> GetService() noexcept
113114
auto query =
114115
std::make_shared<learn::Query>(std::move(heroes), std::move(humans), std::move(droids));
115116
auto mutation = std::make_shared<learn::Mutation>();
116-
auto service = std::make_shared<learn::Operations>(std::move(query), std::move(mutation));
117+
auto service = std::make_shared<learn::Operations>(std::move(query),
118+
std::move(mutation),
119+
std::shared_ptr<learn::Subscription> {});
117120

118121
return service;
119122
}

samples/learn/SubscriptionData.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
#include "CharacterObject.h"
5+
6+
#include "SubscriptionData.h"
7+
8+
namespace graphql::learn {
9+
10+
Subscription::Subscription() noexcept
11+
{
12+
}
13+
14+
std::shared_ptr<object::Character> Subscription::getCharacterChanged() const noexcept
15+
{
16+
return {};
17+
}
18+
19+
} // namespace graphql::learn

samples/learn/SubscriptionData.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
#pragma once
5+
6+
#ifndef SUBSCRIPTIONDATA_H
7+
#define SUBSCRIPTIONDATA_H
8+
9+
#include "SubscriptionObject.h"
10+
11+
namespace graphql::learn {
12+
13+
namespace object {
14+
15+
class Character;
16+
17+
} // namespace object
18+
19+
class Subscription
20+
{
21+
public:
22+
explicit Subscription() noexcept;
23+
24+
std::shared_ptr<object::Character> getCharacterChanged() const noexcept;
25+
};
26+
27+
} // namespace graphql::learn
28+
29+
#endif // SUBSCRIPTIONDATA_H

samples/learn/schema/StarWarsSchema.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "QueryObject.h"
77
#include "MutationObject.h"
8+
#include "SubscriptionObject.h"
89

910
#include "graphqlservice/internal/Schema.h"
1011

@@ -145,13 +146,15 @@ ReviewInput& ReviewInput::operator=(ReviewInput&& other) noexcept
145146
return *this;
146147
}
147148

148-
Operations::Operations(std::shared_ptr<object::Query> query, std::shared_ptr<object::Mutation> mutation)
149+
Operations::Operations(std::shared_ptr<object::Query> query, std::shared_ptr<object::Mutation> mutation, std::shared_ptr<object::Subscription> subscription)
149150
: service::Request({
150151
{ service::strQuery, query },
151-
{ service::strMutation, mutation }
152+
{ service::strMutation, mutation },
153+
{ service::strSubscription, subscription }
152154
}, GetSchema())
153155
, _query(std::move(query))
154156
, _mutation(std::move(mutation))
157+
, _subscription(std::move(subscription))
155158
{
156159
}
157160

@@ -173,6 +176,8 @@ void AddTypesToSchema(const std::shared_ptr<schema::Schema>& schema)
173176
schema->AddType(R"gql(Review)gql"sv, typeReview);
174177
auto typeMutation = schema::ObjectType::Make(R"gql(Mutation)gql"sv, R"md()md"sv);
175178
schema->AddType(R"gql(Mutation)gql"sv, typeMutation);
179+
auto typeSubscription = schema::ObjectType::Make(R"gql(Subscription)gql"sv, R"md()md"sv);
180+
schema->AddType(R"gql(Subscription)gql"sv, typeSubscription);
176181

177182
typeEpisode->AddEnumValues({
178183
{ service::s_namesEpisode[static_cast<std::size_t>(learn::Episode::NEW_HOPE)], R"md()md"sv, std::nullopt },
@@ -192,9 +197,11 @@ void AddTypesToSchema(const std::shared_ptr<schema::Schema>& schema)
192197
AddQueryDetails(typeQuery, schema);
193198
AddReviewDetails(typeReview, schema);
194199
AddMutationDetails(typeMutation, schema);
200+
AddSubscriptionDetails(typeSubscription, schema);
195201

196202
schema->AddQueryType(typeQuery);
197203
schema->AddMutationType(typeMutation);
204+
schema->AddSubscriptionType(typeSubscription);
198205
}
199206

200207
std::shared_ptr<schema::Schema> GetSchema()

0 commit comments

Comments
 (0)