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 progress on 4.0 tasks #174

Merged
merged 9 commits into from
Dec 3, 2021
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
3 changes: 3 additions & 0 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ write_basic_package_version_file(${PROJECT_NAME}-config-version.cmake COMPATIBIL
install(FILES
${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake
${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}-functions.cmake
${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}-update-schema-files.cmake
${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}-update-client-files.cmake
DESTINATION ${GRAPHQL_INSTALL_CMAKE_DIR}/${PROJECT_NAME})
14 changes: 13 additions & 1 deletion cmake/cppgraphqlgen-config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
cppgraphqlgen
-------------

The following import targets are created
The following import targets are created:

::

Expand All @@ -15,8 +15,20 @@ The following import targets are created
cppgraphqlgen::graphqlintrospection
cppgraphqlgen::graphqljson
cppgraphqlgen::schemagen
cppgraphqlgen::clientgen

The following functions are defined to help with code generation and build targets:

::

update_graphql_schema_files
add_graphql_schema_target
add_graphql_schema_no_introspection_target
update_graphql_client_files
add_graphql_client_target
#]=======================================================================]

include(CMakeFindDependencyMacro)
find_package(Threads REQUIRED)
include("${CMAKE_CURRENT_LIST_DIR}/cppgraphqlgen-targets.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/cppgraphqlgen-functions.cmake")
101 changes: 101 additions & 0 deletions cmake/cppgraphqlgen-functions.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

get_filename_component(GRAPHQL_UPDATE_SCHEMA_FILES_SCRIPT
"${CMAKE_CURRENT_LIST_DIR}/cppgraphqlgen-update-schema-files.cmake"
ABSOLUTE)

get_filename_component(GRAPHQL_UPDATE_CLIENT_FILES_SCRIPT
"${CMAKE_CURRENT_LIST_DIR}/cppgraphqlgen-update-client-files.cmake"
ABSOLUTE)

function(update_graphql_schema_files SCHEMA_TARGET SCHEMA_GRAPHQL SCHEMA_PREFIX SCHEMA_NAMESPACE)
set_property(DIRECTORY APPEND
PROPERTY CMAKE_CONFIGURE_DEPENDS ${SCHEMA_TARGET}_schema_files)

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

add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/${SCHEMA_TARGET}_schema_files
COMMAND
${CMAKE_COMMAND} "-DSCHEMA_SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}"
"-DSCHEMA_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}"
"-DSCHEMAGEN_PROGRAM=$<TARGET_FILE:cppgraphqlgen::schemagen>"
"-DSCHEMA_TARGET=${SCHEMA_TARGET}" "-DSCHEMA_GRAPHQL=${SCHEMA_GRAPHQL}"
"-DSCHEMA_PREFIX=${SCHEMA_PREFIX}" "-DSCHEMA_NAMESPACE=${SCHEMA_NAMESPACE}"
"-DADDITIONAL_SCHEMAGEN_ARGS=${ADDITIONAL_SCHEMAGEN_ARGS}"
-P ${GRAPHQL_UPDATE_SCHEMA_FILES_SCRIPT}
DEPENDS ${SCHEMA_GRAPHQL} ${GRAPHQL_UPDATE_SCHEMA_FILES_SCRIPT}
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(STRINGS ${SCHEMA_TARGET}_schema_files SCHEMA_FILES)
add_library(${SCHEMA_TARGET}_schema STATIC ${SCHEMA_FILES})
add_dependencies(${SCHEMA_TARGET}_schema ${SCHEMA_TARGET}_update_schema)
target_include_directories(${SCHEMA_TARGET}_schema PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
target_link_libraries(${SCHEMA_TARGET}_schema PUBLIC cppgraphqlgen::graphqlintrospection)
endfunction()

function(add_graphql_schema_no_introspection_target SCHEMA_NO_INTROSPECTION_TARGET)
add_custom_target(${SCHEMA_NO_INTROSPECTION_TARGET}_update_schema ALL
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${SCHEMA_NO_INTROSPECTION_TARGET}_schema_files)

file(STRINGS ${SCHEMA_NO_INTROSPECTION_TARGET}_schema_files SCHEMA_FILES)
add_library(${SCHEMA_NO_INTROSPECTION_TARGET}_schema STATIC ${SCHEMA_FILES})
add_dependencies(${SCHEMA_NO_INTROSPECTION_TARGET}_schema ${SCHEMA_NO_INTROSPECTION_TARGET}_update_schema)
target_include_directories(${SCHEMA_NO_INTROSPECTION_TARGET}_schema PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
target_link_libraries(${SCHEMA_NO_INTROSPECTION_TARGET}_schema PUBLIC cppgraphqlgen::graphqlservice)
endfunction()

function(update_graphql_client_files CLIENT_TARGET SCHEMA_GRAPHQL REQUEST_GRAPHQL CLIENT_PREFIX CLIENT_NAMESPACE)
set_property(DIRECTORY APPEND
PROPERTY CMAKE_CONFIGURE_DEPENDS ${CLIENT_TARGET}_client_files)

# Collect optional arguments
set(ADDITIONAL_CLIENTGEN_ARGS "")
if(ARGC GREATER 5)
math(EXPR LAST_ARG "${ARGC} - 1")
foreach(ARGN RANGE 5 ${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=${CLIENT_PREFIX}" "-DCLIENT_NAMESPACE=${CLIENT_NAMESPACE}"
"-DADDITIONAL_CLIENTGEN_ARGS=${ADDITIONAL_CLIENTGEN_ARGS}"
-P ${GRAPHQL_UPDATE_CLIENT_FILES_SCRIPT}
DEPENDS ${SCHEMA_GRAPHQL} ${REQUEST_GRAPHQL} ${GRAPHQL_UPDATE_CLIENT_FILES_SCRIPT}
COMMENT "Generating ${CLIENT_TARGET} client"
VERBATIM)
endfunction()

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

file(STRINGS ${CLIENT_TARGET}_client_files CLIENT_FILES)
add_library(${CLIENT_TARGET}_client STATIC ${CLIENT_FILES})
add_dependencies(${CLIENT_TARGET}_client ${CLIENT_TARGET}_update_client)
target_include_directories(${CLIENT_TARGET}_client PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
target_link_libraries(${CLIENT_TARGET}_client PUBLIC cppgraphqlgen::graphqlclient)
endfunction()
58 changes: 58 additions & 0 deletions cmake/cppgraphqlgen-update-client-files.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

# Normalize the filesystem paths
get_filename_component(CLIENT_SOURCE_DIR ${CLIENT_SOURCE_DIR} ABSOLUTE)
get_filename_component(CLIENT_BINARY_DIR ${CLIENT_BINARY_DIR} ABSOLUTE)
get_filename_component(CLIENTGEN_PROGRAM ${CLIENTGEN_PROGRAM} ABSOLUTE)
get_filename_component(SCHEMA_GRAPHQL "${CLIENT_SOURCE_DIR}/${SCHEMA_GRAPHQL}" ABSOLUTE)
get_filename_component(REQUEST_GRAPHQL "${CLIENT_SOURCE_DIR}/${REQUEST_GRAPHQL}" ABSOLUTE)

file(MAKE_DIRECTORY ${CLIENT_BINARY_DIR})

# Cleanup all of the stale files in the binary directory
file(GLOB PREVIOUS_FILES ${CLIENT_BINARY_DIR}/*.h ${CLIENT_BINARY_DIR}/*.cpp
${CLIENT_BINARY_DIR}/${CLIENT_TARGET}_client_files)
foreach(PREVIOUS_FILE ${PREVIOUS_FILES})
file(REMOVE ${PREVIOUS_FILE})
endforeach()

set(CLIENTGEN_ARGS "--schema=${SCHEMA_GRAPHQL}" "--request=${REQUEST_GRAPHQL}" "--prefix=${CLIENT_PREFIX}" "--namespace=${CLIENT_NAMESPACE}")
foreach(CLIENTGEN_ARG ${ADDITIONAL_CLIENTGEN_ARGS})
list(APPEND CLIENTGEN_ARGS ${CLIENTGEN_ARG})
endforeach()

# Regenerate the sources in the binary directory
execute_process(
COMMAND ${CLIENTGEN_PROGRAM} ${CLIENTGEN_ARGS}
OUTPUT_FILE ${CLIENT_TARGET}_client_files
WORKING_DIRECTORY ${CLIENT_BINARY_DIR})

# Get the up-to-date list of files in the binary directory
set(FILE_NAMES "")
file(GLOB NEW_FILES ${CLIENT_BINARY_DIR}/*.h ${CLIENT_BINARY_DIR}/*.cpp)
foreach(NEW_FILE ${NEW_FILES})
get_filename_component(NEW_FILE ${NEW_FILE} NAME)
list(APPEND FILE_NAMES "${NEW_FILE}")
endforeach()

# Don't update the files in the source directory if no files were generated in the binary directory.
if(NOT FILE_NAMES)
message(FATAL_ERROR "Schema generation failed!")
endif()

# Remove stale files in the source directory
cmake_policy(SET CMP0057 NEW)
file(GLOB OLD_FILES ${CLIENT_SOURCE_DIR}/*.h ${CLIENT_SOURCE_DIR}/*.cpp)
foreach(OLD_FILE ${OLD_FILES})
get_filename_component(OLD_FILE ${OLD_FILE} NAME)
if(NOT OLD_FILE IN_LIST FILE_NAMES)
file(REMOVE "${CLIENT_SOURCE_DIR}/${OLD_FILE}")
endif()
endforeach()

# Copy new and modified files from the binary directory to the source directory
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CLIENT_BINARY_DIR}/${CLIENT_TARGET}_client_files
${NEW_FILES}
${CLIENT_SOURCE_DIR})
57 changes: 57 additions & 0 deletions cmake/cppgraphqlgen-update-schema-files.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

# Normalize the filesystem paths
get_filename_component(SCHEMA_SOURCE_DIR ${SCHEMA_SOURCE_DIR} ABSOLUTE)
get_filename_component(SCHEMA_BINARY_DIR ${SCHEMA_BINARY_DIR} ABSOLUTE)
get_filename_component(SCHEMAGEN_PROGRAM ${SCHEMAGEN_PROGRAM} ABSOLUTE)
get_filename_component(SCHEMA_GRAPHQL "${SCHEMA_SOURCE_DIR}/${SCHEMA_GRAPHQL}" ABSOLUTE)

file(MAKE_DIRECTORY ${SCHEMA_BINARY_DIR})

# Cleanup all of the stale files in the binary directory
file(GLOB PREVIOUS_FILES ${SCHEMA_BINARY_DIR}/*.h ${SCHEMA_BINARY_DIR}/*.cpp
${SCHEMA_BINARY_DIR}/${SCHEMA_TARGET}_schema_files)
foreach(PREVIOUS_FILE ${PREVIOUS_FILES})
file(REMOVE ${PREVIOUS_FILE})
endforeach()

set(SCHEMAGEN_ARGS "--schema=${SCHEMA_GRAPHQL}" "--prefix=${SCHEMA_PREFIX}" "--namespace=${SCHEMA_NAMESPACE}")
foreach(SCHEMAGEN_ARG ${ADDITIONAL_SCHEMAGEN_ARGS})
list(APPEND SCHEMAGEN_ARGS ${SCHEMAGEN_ARG})
endforeach()

# Regenerate the sources in the binary directory
execute_process(
COMMAND ${SCHEMAGEN_PROGRAM} ${SCHEMAGEN_ARGS}
OUTPUT_FILE ${SCHEMA_TARGET}_schema_files
WORKING_DIRECTORY ${SCHEMA_BINARY_DIR})

# Get the up-to-date list of files in the binary directory
set(FILE_NAMES "")
file(GLOB NEW_FILES ${SCHEMA_BINARY_DIR}/*.h ${SCHEMA_BINARY_DIR}/*.cpp)
foreach(NEW_FILE ${NEW_FILES})
get_filename_component(NEW_FILE ${NEW_FILE} NAME)
list(APPEND FILE_NAMES "${NEW_FILE}")
endforeach()

# Don't update the files in the source directory if no files were generated in the binary directory.
if(NOT FILE_NAMES)
message(FATAL_ERROR "Schema generation failed!")
endif()

# Remove stale files in the source directory
cmake_policy(SET CMP0057 NEW)
file(GLOB OLD_FILES ${SCHEMA_SOURCE_DIR}/*.h ${SCHEMA_SOURCE_DIR}/*.cpp)
foreach(OLD_FILE ${OLD_FILES})
get_filename_component(OLD_FILE ${OLD_FILE} NAME)
if(NOT OLD_FILE IN_LIST FILE_NAMES)
file(REMOVE "${SCHEMA_SOURCE_DIR}/${OLD_FILE}")
endif()
endforeach()

# Copy new and modified files from the binary directory to the source directory
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different
${SCHEMA_BINARY_DIR}/${SCHEMA_TARGET}_schema_files
${NEW_FILES}
${SCHEMA_SOURCE_DIR})
4 changes: 2 additions & 2 deletions include/SchemaGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ struct GeneratorOptions
{
const std::optional<GeneratorPaths> paths;
const bool verbose = false;
const bool separateFiles = false;
const bool noStubs = false;
const bool stubs = false;
const bool mergeFiles = false;
const bool noIntrospection = false;
};

Expand Down
11 changes: 5 additions & 6 deletions include/Validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,16 @@ struct ValidateArgumentMap
internal::string_view_map<ValidateArgumentValuePtr> values;
};

using ValidateArgumentVariant = std::variant<ValidateArgumentVariable, response::IntType,
response::FloatType, std::string_view, response::BooleanType, ValidateArgumentEnumValue,
ValidateArgumentList, ValidateArgumentMap>;
using ValidateArgumentVariant = std::variant<ValidateArgumentVariable, int, double,
std::string_view, bool, ValidateArgumentEnumValue, ValidateArgumentList, ValidateArgumentMap>;

struct ValidateArgumentValue
{
ValidateArgumentValue(ValidateArgumentVariable&& value);
ValidateArgumentValue(response::IntType value);
ValidateArgumentValue(response::FloatType value);
ValidateArgumentValue(int value);
ValidateArgumentValue(double value);
ValidateArgumentValue(std::string_view value);
ValidateArgumentValue(response::BooleanType value);
ValidateArgumentValue(bool value);
ValidateArgumentValue(ValidateArgumentEnumValue&& value);
ValidateArgumentValue(ValidateArgumentList&& value);
ValidateArgumentValue(ValidateArgumentMap&& value);
Expand Down
46 changes: 19 additions & 27 deletions include/graphqlservice/GraphQLClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ namespace graphql::client {
// Errors may specify the line number and column number where the error occurred.
struct ErrorLocation
{
response::IntType line {};
response::IntType column {};
int line {};
int column {};
};

// Errors may specify a path to the field which triggered the error. The path consists of
// field names and the indices of elements in a list.
using ErrorPathSegment = std::variant<response::StringType, response::IntType>;
using ErrorPathSegment = std::variant<std::string, int>;

// Error returned from the service.
struct Error
Expand Down Expand Up @@ -141,27 +141,23 @@ struct ModifiedVariable
// Convenient type aliases for testing, generated code won't actually use these. These are also
// the specializations which are implemented in the GraphQLClient library, other specializations
// for input types should be generated in schemagen.
using IntVariable = ModifiedVariable<response::IntType>;
using FloatVariable = ModifiedVariable<response::FloatType>;
using StringVariable = ModifiedVariable<response::StringType>;
using BooleanVariable = ModifiedVariable<response::BooleanType>;
using IntVariable = ModifiedVariable<int>;
using FloatVariable = ModifiedVariable<double>;
using StringVariable = ModifiedVariable<std::string>;
using BooleanVariable = ModifiedVariable<bool>;
using IdVariable = ModifiedVariable<response::IdType>;
using ScalarVariable = ModifiedVariable<response::Value>;

#ifdef GRAPHQL_DLLEXPORTS
// Export all of the built-in converters
template <>
GRAPHQLCLIENT_EXPORT response::Value ModifiedVariable<response::IntType>::serialize(
response::IntType&& value);
GRAPHQLCLIENT_EXPORT response::Value ModifiedVariable<int>::serialize(int&& value);
template <>
GRAPHQLCLIENT_EXPORT response::Value ModifiedVariable<response::FloatType>::serialize(
response::FloatType&& value);
GRAPHQLCLIENT_EXPORT response::Value ModifiedVariable<double>::serialize(double&& value);
template <>
GRAPHQLCLIENT_EXPORT response::Value ModifiedVariable<response::StringType>::serialize(
response::StringType&& value);
GRAPHQLCLIENT_EXPORT response::Value ModifiedVariable<std::string>::serialize(std::string&& value);
template <>
GRAPHQLCLIENT_EXPORT response::Value ModifiedVariable<response::BooleanType>::serialize(
response::BooleanType&& value);
GRAPHQLCLIENT_EXPORT response::Value ModifiedVariable<bool>::serialize(bool&& value);
template <>
GRAPHQLCLIENT_EXPORT response::Value ModifiedVariable<response::IdType>::serialize(
response::IdType&& value);
Expand Down Expand Up @@ -245,27 +241,23 @@ struct ModifiedResponse
// Convenient type aliases for testing, generated code won't actually use these. These are also
// the specializations which are implemented in the GraphQLClient library, other specializations
// for output types should be generated in schemagen.
using IntResponse = ModifiedResponse<response::IntType>;
using FloatResponse = ModifiedResponse<response::FloatType>;
using StringResponse = ModifiedResponse<response::StringType>;
using BooleanResponse = ModifiedResponse<response::BooleanType>;
using IntResponse = ModifiedResponse<int>;
using FloatResponse = ModifiedResponse<double>;
using StringResponse = ModifiedResponse<std::string>;
using BooleanResponse = ModifiedResponse<bool>;
using IdResponse = ModifiedResponse<response::IdType>;
using ScalarResponse = ModifiedResponse<response::Value>;

#ifdef GRAPHQL_DLLEXPORTS
// Export all of the built-in converters
template <>
GRAPHQLCLIENT_EXPORT response::IntType ModifiedResponse<response::IntType>::parse(
response::Value response);
GRAPHQLCLIENT_EXPORT int ModifiedResponse<int>::parse(response::Value response);
template <>
GRAPHQLCLIENT_EXPORT response::FloatType ModifiedResponse<response::FloatType>::parse(
response::Value response);
GRAPHQLCLIENT_EXPORT double ModifiedResponse<double>::parse(response::Value response);
template <>
GRAPHQLCLIENT_EXPORT response::StringType ModifiedResponse<response::StringType>::parse(
response::Value response);
GRAPHQLCLIENT_EXPORT std::string ModifiedResponse<std::string>::parse(response::Value response);
template <>
GRAPHQLCLIENT_EXPORT response::BooleanType ModifiedResponse<response::BooleanType>::parse(
response::Value response);
GRAPHQLCLIENT_EXPORT bool ModifiedResponse<bool>::parse(response::Value response);
template <>
GRAPHQLCLIENT_EXPORT response::IdType ModifiedResponse<response::IdType>::parse(
response::Value response);
Expand Down
Loading