Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more tweaks for the next release candidate #319

Merged
merged 2 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 56 additions & 5 deletions cmake/cppgraphqlgen-functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,23 @@ function(update_graphql_schema_files SCHEMA_TARGET SCHEMA_GRAPHQL SCHEMA_PREFIX
DEPENDS ${SCHEMA_GRAPHQL} ${GRAPHQL_UPDATE_SCHEMA_FILES_SCRIPT} cppgraphqlgen::schemagen
COMMENT "Generating ${SCHEMA_TARGET} GraphQL schema"
VERBATIM)
endfunction()

function(add_graphql_schema_target SCHEMA_TARGET)
add_custom_target(${SCHEMA_TARGET}_update_schema ALL
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${SCHEMA_TARGET}_schema_files)
file(REAL_PATH ${SCHEMA_GRAPHQL} SCHEMA_GRAPHQL BASE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
set_target_properties(${SCHEMA_TARGET}_update_schema PROPERTIES
SCHEMA_GRAPHQL ${SCHEMA_GRAPHQL}
SCHEMA_PREFIX ${SCHEMA_PREFIX}
SCHEMA_NAMESPACE ${SCHEMA_NAMESPACE})
endfunction()

function(add_graphql_schema_target SCHEMA_TARGET)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${SCHEMA_TARGET}_schema_files)
file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/${SCHEMA_TARGET}_schema_files SCHEMA_FILES)
add_library(${SCHEMA_TARGET}_schema STATIC ${SCHEMA_FILES})
add_dependencies(${SCHEMA_TARGET}_schema ${SCHEMA_TARGET}_update_schema)
if(TARGET ${SCHEMA_TARGET}_update_schema)
add_dependencies(${SCHEMA_TARGET}_schema ${SCHEMA_TARGET}_update_schema)
endif()
target_compile_features(${SCHEMA_TARGET}_schema PUBLIC cxx_std_20)
target_include_directories(${SCHEMA_TARGET}_schema PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
target_link_libraries(${SCHEMA_TARGET}_schema PUBLIC cppgraphqlgen::graphqlservice)
Expand Down Expand Up @@ -90,19 +97,63 @@ function(update_graphql_client_files CLIENT_TARGET SCHEMA_GRAPHQL REQUEST_GRAPHQ
DEPENDS ${SCHEMA_GRAPHQL} ${REQUEST_GRAPHQL} ${GRAPHQL_UPDATE_CLIENT_FILES_SCRIPT} cppgraphqlgen::clientgen
COMMENT "Generating ${CLIENT_TARGET} client"
VERBATIM)

add_custom_target(${CLIENT_TARGET}_update_client ALL
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${CLIENT_TARGET}_client_files)
endfunction()

function(add_graphql_client_target CLIENT_TARGET)
function(update_graphql_shared_client_files CLIENT_TARGET SCHEMA_TARGET REQUEST_GRAPHQL)
set_property(DIRECTORY APPEND
PROPERTY CMAKE_CONFIGURE_DEPENDS ${CLIENT_TARGET}_client_files)
get_target_property(SCHEMA_GRAPHQL ${SCHEMA_TARGET}_update_schema SCHEMA_GRAPHQL)
file(RELATIVE_PATH SCHEMA_GRAPHQL ${CMAKE_CURRENT_SOURCE_DIR} ${SCHEMA_GRAPHQL})
get_target_property(SCHEMA_PREFIX ${SCHEMA_TARGET}_update_schema SCHEMA_PREFIX)
get_target_property(SCHEMA_NAMESPACE ${SCHEMA_TARGET}_update_schema SCHEMA_NAMESPACE)

# Collect optional arguments
set(ADDITIONAL_CLIENTGEN_ARGS "--shared-types")
if(ARGC GREATER 4)
math(EXPR LAST_ARG "${ARGC} - 1")
foreach(ARGN RANGE 4 ${LAST_ARG})
set(NEXT_ARG "${ARGV${ARGN}}")
list(APPEND ADDITIONAL_CLIENTGEN_ARGS "${NEXT_ARG}")
endforeach()
endif()

add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/${CLIENT_TARGET}_client_files
COMMAND
${CMAKE_COMMAND} "-DCLIENT_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}"
"-DCLIENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}"
"-DCLIENTGEN_PROGRAM=$<TARGET_FILE:cppgraphqlgen::clientgen>" "-DCLIENT_TARGET=${CLIENT_TARGET}"
"-DSCHEMA_GRAPHQL=${SCHEMA_GRAPHQL}" "-DREQUEST_GRAPHQL=${REQUEST_GRAPHQL}"
"-DCLIENT_PREFIX=${SCHEMA_PREFIX}" "-DCLIENT_NAMESPACE=${SCHEMA_NAMESPACE}"
"-DADDITIONAL_CLIENTGEN_ARGS=${ADDITIONAL_CLIENTGEN_ARGS}"
-P ${GRAPHQL_UPDATE_CLIENT_FILES_SCRIPT}
DEPENDS ${SCHEMA_GRAPHQL} ${REQUEST_GRAPHQL} ${GRAPHQL_UPDATE_CLIENT_FILES_SCRIPT} cppgraphqlgen::clientgen
COMMENT "Generating ${CLIENT_TARGET} client"
VERBATIM)

add_custom_target(${CLIENT_TARGET}_update_client ALL
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${CLIENT_TARGET}_client_files)
set_target_properties(${CLIENT_TARGET}_update_client PROPERTIES
SCHEMA_TARGET ${SCHEMA_TARGET})
endfunction()

function(add_graphql_client_target CLIENT_TARGET)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${CLIENT_TARGET}_client_files)
file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/${CLIENT_TARGET}_client_files CLIENT_FILES)
add_library(${CLIENT_TARGET}_client STATIC ${CLIENT_FILES})
add_dependencies(${CLIENT_TARGET}_client ${CLIENT_TARGET}_update_client)
if(TARGET ${CLIENT_TARGET}_update_client)
add_dependencies(${CLIENT_TARGET}_client ${CLIENT_TARGET}_update_client)
endif()
target_compile_features(${CLIENT_TARGET}_client PUBLIC cxx_std_20)
target_include_directories(${CLIENT_TARGET}_client PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
target_link_libraries(${CLIENT_TARGET}_client PUBLIC cppgraphqlgen::graphqlclient)
get_target_property(SCHEMA_TARGET ${CLIENT_TARGET}_update_client SCHEMA_TARGET)
if(SCHEMA_TARGET)
target_link_libraries(${CLIENT_TARGET}_client PUBLIC ${SCHEMA_TARGET}_schema)
endif()
file(GLOB CLIENT_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
target_sources(${CLIENT_TARGET}_client PUBLIC FILE_SET HEADERS
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}
Expand Down
4 changes: 2 additions & 2 deletions include/SchemaGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class [[nodiscard("unnecessary construction")]] Generator
std::ostream& moduleFile, std::string_view objectNamespace, std::string_view cppType) const;
void outputObjectImplements(std::ostream& headerFile, const ObjectType& objectType) const;
void outputObjectStubs(std::ostream& headerFile, const ObjectType& objectType) const;
void outputObjectDeclaration(
std::ostream& headerFile, const ObjectType& objectType, bool isQueryType) const;
void outputObjectDeclaration(std::ostream& headerFile, const ObjectType& objectType,
bool isQueryType, bool isSubscriptionType) const;
[[nodiscard("unnecessary memory copy")]] std::string getFieldDeclaration(
const InputField& inputField) const noexcept;
[[nodiscard("unnecessary memory copy")]] std::string getFieldDeclaration(
Expand Down
8 changes: 4 additions & 4 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

cmake_minimum_required(VERSION 3.28)

add_subdirectory(client)
add_subdirectory(learn)
add_subdirectory(validation)

if(GRAPHQL_BUILD_MODULES)
add_subdirectory(today)
endif()

add_subdirectory(client)
add_subdirectory(learn)
add_subdirectory(validation)

if(GRAPHQL_BUILD_HTTP_SAMPLE)
find_package(boost_beast CONFIG QUIET)
if(boost_beast_FOUND)
Expand Down
1 change: 1 addition & 0 deletions samples/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ if(GRAPHQL_BUILD_MODULES)
add_subdirectory(benchmark)
add_executable(client_benchmark benchmark.cpp)
target_link_libraries(client_benchmark PRIVATE
todaygraphql
benchmark_client)

if(WIN32 AND BUILD_SHARED_LIBS)
Expand Down
3 changes: 1 addition & 2 deletions samples/client/benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ cmake_minimum_required(VERSION 3.28)
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/cppgraphqlgen-functions.cmake)

if(GRAPHQL_UPDATE_SAMPLES AND GRAPHQL_BUILD_CLIENTGEN)
update_graphql_client_files(benchmark ../../today/schema.today.graphql client.benchmark.today.graphql Today today --shared-types)
update_graphql_shared_client_files(benchmark today client.benchmark.today.graphql)
endif()

add_graphql_client_target(benchmark)
target_link_libraries(benchmark_client PRIVATE todaygraphql)
3 changes: 2 additions & 1 deletion samples/learn/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ add_library(star_wars STATIC
QueryData.cpp
ReviewData.cpp
MutationData.cpp
StarWarsData.cpp)
StarWarsData.cpp
SubscriptionData.cpp)
target_link_libraries(star_wars PUBLIC learn_schema)
target_include_directories(star_wars INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

Expand Down
5 changes: 4 additions & 1 deletion samples/learn/StarWarsData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "MutationData.h"
#include "QueryData.h"
#include "ReviewData.h"
#include "SubscriptionData.h"

using namespace std::literals;

Expand Down Expand Up @@ -113,7 +114,9 @@ std::shared_ptr<service::Request> GetService() noexcept
auto query =
std::make_shared<learn::Query>(std::move(heroes), std::move(humans), std::move(droids));
auto mutation = std::make_shared<learn::Mutation>();
auto service = std::make_shared<learn::Operations>(std::move(query), std::move(mutation));
auto service = std::make_shared<learn::Operations>(std::move(query),
std::move(mutation),
std::shared_ptr<learn::Subscription> {});

return service;
}
Expand Down
19 changes: 19 additions & 0 deletions samples/learn/SubscriptionData.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#include "CharacterObject.h"

#include "SubscriptionData.h"

namespace graphql::learn {

Subscription::Subscription() noexcept
{
}

std::shared_ptr<object::Character> Subscription::getCharacterChanged() const noexcept
{
return {};
}

} // namespace graphql::learn
29 changes: 29 additions & 0 deletions samples/learn/SubscriptionData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#pragma once

#ifndef SUBSCRIPTIONDATA_H
#define SUBSCRIPTIONDATA_H

#include "SubscriptionObject.h"

namespace graphql::learn {

namespace object {

class Character;

} // namespace object

class Subscription
{
public:
explicit Subscription() noexcept;

std::shared_ptr<object::Character> getCharacterChanged() const noexcept;
};

} // namespace graphql::learn

#endif // SUBSCRIPTIONDATA_H
11 changes: 9 additions & 2 deletions samples/learn/schema/StarWarsSchema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "QueryObject.h"
#include "MutationObject.h"
#include "SubscriptionObject.h"

#include "graphqlservice/internal/Schema.h"

Expand Down Expand Up @@ -145,13 +146,15 @@ ReviewInput& ReviewInput::operator=(ReviewInput&& other) noexcept
return *this;
}

Operations::Operations(std::shared_ptr<object::Query> query, std::shared_ptr<object::Mutation> mutation)
Operations::Operations(std::shared_ptr<object::Query> query, std::shared_ptr<object::Mutation> mutation, std::shared_ptr<object::Subscription> subscription)
: service::Request({
{ service::strQuery, query },
{ service::strMutation, mutation }
{ service::strMutation, mutation },
{ service::strSubscription, subscription }
}, GetSchema())
, _query(std::move(query))
, _mutation(std::move(mutation))
, _subscription(std::move(subscription))
{
}

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

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

schema->AddQueryType(typeQuery);
schema->AddMutationType(typeMutation);
schema->AddSubscriptionType(typeSubscription);
}

std::shared_ptr<schema::Schema> GetSchema()
Expand Down
12 changes: 8 additions & 4 deletions samples/learn/schema/StarWarsSchema.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,30 @@ class Droid;
class Query;
class Review;
class Mutation;
class Subscription;

} // namespace object

class [[nodiscard("unnecessary construction")]] Operations final
: public service::Request
{
public:
explicit Operations(std::shared_ptr<object::Query> query, std::shared_ptr<object::Mutation> mutation);
explicit Operations(std::shared_ptr<object::Query> query, std::shared_ptr<object::Mutation> mutation, std::shared_ptr<object::Subscription> subscription);

template <class TQuery, class TMutation>
explicit Operations(std::shared_ptr<TQuery> query, std::shared_ptr<TMutation> mutation)
template <class TQuery, class TMutation, class TSubscription = service::SubscriptionPlaceholder>
explicit Operations(std::shared_ptr<TQuery> query, std::shared_ptr<TMutation> mutation, std::shared_ptr<TSubscription> subscription = {})
: Operations {
std::make_shared<object::Query>(std::move(query)),
std::make_shared<object::Mutation>(std::move(mutation))
std::make_shared<object::Mutation>(std::move(mutation)),
subscription ? std::make_shared<object::Subscription>(std::move(subscription)) : std::shared_ptr<object::Subscription> {}
}
{
}

private:
std::shared_ptr<object::Query> _query;
std::shared_ptr<object::Mutation> _mutation;
std::shared_ptr<object::Subscription> _subscription;
};

void AddCharacterDetails(const std::shared_ptr<schema::InterfaceType>& typeCharacter, const std::shared_ptr<schema::Schema>& schema);
Expand All @@ -111,6 +114,7 @@ void AddDroidDetails(const std::shared_ptr<schema::ObjectType>& typeDroid, const
void AddQueryDetails(const std::shared_ptr<schema::ObjectType>& typeQuery, const std::shared_ptr<schema::Schema>& schema);
void AddReviewDetails(const std::shared_ptr<schema::ObjectType>& typeReview, const std::shared_ptr<schema::Schema>& schema);
void AddMutationDetails(const std::shared_ptr<schema::ObjectType>& typeMutation, const std::shared_ptr<schema::Schema>& schema);
void AddSubscriptionDetails(const std::shared_ptr<schema::ObjectType>& typeSubscription, const std::shared_ptr<schema::Schema>& schema);

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

Expand Down
2 changes: 2 additions & 0 deletions samples/learn/schema/StarWarsSchema.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export import GraphQL.StarWars.DroidObject;
export import GraphQL.StarWars.QueryObject;
export import GraphQL.StarWars.ReviewObject;
export import GraphQL.StarWars.MutationObject;
export import GraphQL.StarWars.SubscriptionObject;

export namespace graphql::learn {

Expand All @@ -32,6 +33,7 @@ using learn::AddDroidDetails;
using learn::AddQueryDetails;
using learn::AddReviewDetails;
using learn::AddMutationDetails;
using learn::AddSubscriptionDetails;

using learn::GetSchema;

Expand Down
Loading