Skip to content

Commit eb81bbd

Browse files
authored
Merge pull request #249 from wravery/simplify-input-types
Use concepts and type traits instead of specializing service::isInputType() for each input type
2 parents 2bd0647 + 8a4a53c commit eb81bbd

32 files changed

+351
-394
lines changed

cmake/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.3.1
1+
4.4.0

include/graphqlservice/GraphQLClient.h

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,23 @@ enum class [[nodiscard]] TypeModifier {
7070
List,
7171
};
7272

73-
// Specialized to return true for all INPUT_OBJECT types.
73+
// These types are used as scalar variables even though they are represented with a class.
7474
template <typename Type>
75-
[[nodiscard]] constexpr bool isInputType() noexcept
76-
{
77-
return false;
78-
}
75+
concept ScalarVariableClass = std::is_same_v<Type, std::string> || std::is_same_v<Type,
76+
response::IdType> || std::is_same_v<Type, response::Value>;
7977

80-
// Special-case an innermost nullable INPUT_OBJECT type.
78+
// Any non-scalar class used in a variable is a generated INPUT_OBJECT type.
79+
template <typename Type>
80+
concept InputVariableClass = std::is_class_v<Type> && !ScalarVariableClass<Type>;
81+
82+
// Test if there are any non-None modifiers left.
8183
template <TypeModifier... Other>
82-
[[nodiscard]] constexpr bool onlyNoneModifiers() noexcept
83-
{
84-
return (... && (Other == TypeModifier::None));
85-
}
84+
concept OnlyNoneModifiers = (... && (Other == TypeModifier::None));
85+
86+
// Special-case an innermost nullable INPUT_OBJECT type.
87+
template <typename Type, TypeModifier... Other>
88+
concept InputVariableUniquePtr = InputVariableClass<Type> && OnlyNoneModifiers<Other...>;
89+
8690

8791
// Serialize variable input values with chained type modifiers which add nullable or list wrappers.
8892
template <typename Type>
@@ -94,8 +98,8 @@ struct ModifiedVariable
9498
{
9599
// Peel off modifiers until we get to the underlying type.
96100
using type = typename std::conditional_t<TypeModifier::Nullable == Modifier,
97-
typename std::conditional_t<isInputType<U>() && onlyNoneModifiers<Other...>(),
98-
std::unique_ptr<U>, std::optional<typename VariableTraits<U, Other...>::type>>,
101+
typename std::conditional_t<InputVariableUniquePtr<U, Other...>, std::unique_ptr<U>,
102+
std::optional<typename VariableTraits<U, Other...>::type>>,
99103
typename std::conditional_t<TypeModifier::List == Modifier,
100104
std::vector<typename VariableTraits<U, Other...>::type>, U>>;
101105
};
@@ -173,7 +177,7 @@ struct ModifiedVariable
173177

174178
if (nullableValue)
175179
{
176-
if constexpr (isInputType<Type>() && onlyNoneModifiers<Other...>())
180+
if constexpr (InputVariableUniquePtr<Type, Other...>)
177181
{
178182
// Special case duplicating the std::unique_ptr.
179183
result = std::make_unique<Type>(Type { *nullableValue });

include/graphqlservice/GraphQLService.h

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -592,19 +592,22 @@ enum class [[nodiscard]] TypeModifier {
592592
List,
593593
};
594594

595-
// Specialized to return true for all INPUT_OBJECT types.
595+
// These types are used as scalar arguments even though they are represented with a class.
596596
template <typename Type>
597-
[[nodiscard]] constexpr bool isInputType() noexcept
598-
{
599-
return false;
600-
}
597+
concept ScalarArgumentClass = std::is_same_v<Type, std::string> || std::is_same_v<Type,
598+
response::IdType> || std::is_same_v<Type, response::Value>;
601599

602-
// Special-case an innermost nullable INPUT_OBJECT type.
600+
// Any non-scalar class used in an argument is a generated INPUT_OBJECT type.
601+
template <typename Type>
602+
concept InputArgumentClass = std::is_class_v<Type> && !ScalarArgumentClass<Type>;
603+
604+
// Test if there are any non-None modifiers left.
603605
template <TypeModifier... Other>
604-
[[nodiscard]] constexpr bool onlyNoneModifiers() noexcept
605-
{
606-
return (... && (Other == TypeModifier::None));
607-
}
606+
concept OnlyNoneModifiers = (... && (Other == TypeModifier::None));
607+
608+
// Special-case an innermost nullable INPUT_OBJECT type.
609+
template <typename Type, TypeModifier... Other>
610+
concept InputArgumentUniquePtr = InputArgumentClass<Type> && OnlyNoneModifiers<Other...>;
608611

609612
// Extract individual arguments with chained type modifiers which add nullable or list wrappers.
610613
// If the argument is not optional, use require and let it throw a schema_exception when the
@@ -619,8 +622,8 @@ struct ModifiedArgument
619622
{
620623
// Peel off modifiers until we get to the underlying type.
621624
using type = typename std::conditional_t<TypeModifier::Nullable == Modifier,
622-
typename std::conditional_t<isInputType<U>() && onlyNoneModifiers<Other...>(),
623-
std::unique_ptr<U>, std::optional<typename ArgumentTraits<U, Other...>::type>>,
625+
typename std::conditional_t<InputArgumentUniquePtr<U, Other...>, std::unique_ptr<U>,
626+
std::optional<typename ArgumentTraits<U, Other...>::type>>,
624627
typename std::conditional_t<TypeModifier::List == Modifier,
625628
std::vector<typename ArgumentTraits<U, Other...>::type>, U>>;
626629
};
@@ -699,7 +702,7 @@ struct ModifiedArgument
699702

700703
auto result = require<Other...>(name, arguments);
701704

702-
if constexpr (isInputType<Type>() && onlyNoneModifiers<Other...>())
705+
if constexpr (InputArgumentUniquePtr<Type, Other...>)
703706
{
704707
return std::make_unique<decltype(result)>(std::move(result));
705708
}
@@ -768,7 +771,7 @@ struct ModifiedArgument
768771

769772
if (nullableValue)
770773
{
771-
if constexpr (isInputType<Type>() && onlyNoneModifiers<Other...>())
774+
if constexpr (InputArgumentUniquePtr<Type, Other...>)
772775
{
773776
// Special case duplicating the std::unique_ptr.
774777
result = std::make_unique<Type>(Type { *nullableValue });

include/graphqlservice/internal/Version.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
namespace graphql::internal {
1212

13-
constexpr std::string_view FullVersion { "4.3.1" };
13+
constexpr std::string_view FullVersion { "4.4.0" };
1414

1515
constexpr size_t MajorVersion = 4;
16-
constexpr size_t MinorVersion = 3;
17-
constexpr size_t PatchVersion = 1;
16+
constexpr size_t MinorVersion = 4;
17+
constexpr size_t PatchVersion = 0;
1818

1919
} // namespace graphql::internal
2020

include/graphqlservice/introspection/IntrospectionSchema.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
#include "graphqlservice/internal/Schema.h"
1212

13-
// Check if the library version is compatible with schemagen 4.3.0
13+
// Check if the library version is compatible with schemagen 4.4.0
1414
static_assert(graphql::internal::MajorVersion == 4, "regenerate with schemagen: major version mismatch");
15-
static_assert(graphql::internal::MinorVersion == 3, "regenerate with schemagen: minor version mismatch");
15+
static_assert(graphql::internal::MinorVersion == 4, "regenerate with schemagen: minor version mismatch");
1616

1717
#include <array>
1818
#include <memory>

res/ClientGen.rc

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

44
#include <winver.h>
55

6-
#define GRAPHQL_RC_VERSION 4,3,1,0
7-
#define GRAPHQL_RC_VERSION_STR "4.3.1"
6+
#define GRAPHQL_RC_VERSION 4,4,0,0
7+
#define GRAPHQL_RC_VERSION_STR "4.4.0"
88

99
#ifndef DEBUG
1010
#define VER_DEBUG 0

res/SchemaGen.rc

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

44
#include <winver.h>
55

6-
#define GRAPHQL_RC_VERSION 4,3,1,0
7-
#define GRAPHQL_RC_VERSION_STR "4.3.1"
6+
#define GRAPHQL_RC_VERSION 4,4,0,0
7+
#define GRAPHQL_RC_VERSION_STR "4.4.0"
88

99
#ifndef DEBUG
1010
#define VER_DEBUG 0

res/graphqlclient_version.rc

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

44
#include <winver.h>
55

6-
#define GRAPHQL_RC_VERSION 4,3,1,0
7-
#define GRAPHQL_RC_VERSION_STR "4.3.1"
6+
#define GRAPHQL_RC_VERSION 4,4,0,0
7+
#define GRAPHQL_RC_VERSION_STR "4.4.0"
88

99
#ifndef DEBUG
1010
#define VER_DEBUG 0

res/graphqljson_version.rc

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

44
#include <winver.h>
55

6-
#define GRAPHQL_RC_VERSION 4,3,1,0
7-
#define GRAPHQL_RC_VERSION_STR "4.3.1"
6+
#define GRAPHQL_RC_VERSION 4,4,0,0
7+
#define GRAPHQL_RC_VERSION_STR "4.4.0"
88

99
#ifndef DEBUG
1010
#define VER_DEBUG 0

res/graphqlpeg_version.rc

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

44
#include <winver.h>
55

6-
#define GRAPHQL_RC_VERSION 4,3,1,0
7-
#define GRAPHQL_RC_VERSION_STR "4.3.1"
6+
#define GRAPHQL_RC_VERSION 4,4,0,0
7+
#define GRAPHQL_RC_VERSION_STR "4.4.0"
88

99
#ifndef DEBUG
1010
#define VER_DEBUG 0

0 commit comments

Comments
 (0)