Skip to content

Commit 3cba18d

Browse files
committed
Separate Result and ModifiedResult for ODR
1 parent 5e7ff31 commit 3cba18d

Some content is hidden

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

63 files changed

+208
-194
lines changed

include/graphqlservice/GraphQLService.h

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

595+
// Test if there are any non-None modifiers left.
596+
template <TypeModifier... Other>
597+
concept OnlyNoneModifiers = (... && (Other == TypeModifier::None));
598+
599+
// Test if the next modifier is Nullable.
600+
template <TypeModifier Modifier>
601+
concept NullableModifier = Modifier == TypeModifier::Nullable;
602+
603+
// Test if the next modifier is List.
604+
template <TypeModifier Modifier>
605+
concept ListModifier = Modifier == TypeModifier::List;
606+
607+
// Convert arguments and input types with a non-templated static method.
595608
template <typename Type>
596609
struct Argument
597610
{
@@ -619,18 +632,6 @@ GRAPHQLSERVICE_EXPORT response::Value Argument<response::Value>::convert(
619632

620633
namespace {
621634

622-
// Test if there are any non-None modifiers left.
623-
template <TypeModifier... Other>
624-
concept OnlyNoneModifiers = (... && (Other == TypeModifier::None));
625-
626-
// Test if the next modifier is Nullable.
627-
template <TypeModifier Modifier>
628-
concept NullableModifier = Modifier == TypeModifier::Nullable;
629-
630-
// Test if the next modifier is List.
631-
template <TypeModifier Modifier>
632-
concept ListModifier = Modifier == TypeModifier::List;
633-
634635
// These types are used as scalar arguments even though they are represented with a class.
635636
template <typename Type>
636637
concept ScalarArgumentClass = std::is_same_v<Type, std::string> || std::is_same_v<Type,
@@ -879,14 +880,70 @@ class [[nodiscard]] Object : public std::enable_shared_from_this<Object>
879880
ResolverMap _resolvers;
880881
};
881882

882-
// Test if this Type is Object.
883-
template <typename Type>
884-
concept ObjectType = std::is_same_v<Object, Type>;
885-
886883
// Test if this Type inherits from Object.
887884
template <typename Type>
888885
concept ObjectBaseType = std::is_base_of_v<Object, Type>;
889886

887+
// Convert result types with non-templated static methods.
888+
template <typename Type>
889+
struct Result
890+
{
891+
using future_type = typename std::conditional_t<ObjectBaseType<Type>,
892+
AwaitableObject<std::shared_ptr<const Object>>, AwaitableScalar<Type>>;
893+
894+
// Convert a single value of the specified type to JSON.
895+
[[nodiscard]] static AwaitableResolver convert(
896+
typename future_type result, ResolverParams params);
897+
898+
// Validate a single scalar value is the expected type.
899+
static void validateScalar(const response::Value& value);
900+
};
901+
902+
#ifdef GRAPHQL_DLLEXPORTS
903+
// Export all of the built-in converters
904+
template <>
905+
GRAPHQLSERVICE_EXPORT AwaitableResolver Result<int>::convert(
906+
AwaitableScalar<int> result, ResolverParams params);
907+
template <>
908+
GRAPHQLSERVICE_EXPORT AwaitableResolver Result<double>::convert(
909+
AwaitableScalar<double> result, ResolverParams params);
910+
template <>
911+
GRAPHQLSERVICE_EXPORT AwaitableResolver Result<std::string>::convert(
912+
AwaitableScalar<std::string> result, ResolverParams params);
913+
template <>
914+
GRAPHQLSERVICE_EXPORT AwaitableResolver Result<bool>::convert(
915+
AwaitableScalar<bool> result, ResolverParams params);
916+
template <>
917+
GRAPHQLSERVICE_EXPORT AwaitableResolver Result<response::IdType>::convert(
918+
AwaitableScalar<response::IdType> result, ResolverParams params);
919+
template <>
920+
GRAPHQLSERVICE_EXPORT AwaitableResolver Result<response::Value>::convert(
921+
AwaitableScalar<response::Value> result, ResolverParams params);
922+
template <>
923+
GRAPHQLSERVICE_EXPORT AwaitableResolver Result<Object>::convert(
924+
AwaitableObject<std::shared_ptr<const Object>> result, ResolverParams params);
925+
926+
// Export all of the scalar value validation methods
927+
template <>
928+
GRAPHQLSERVICE_EXPORT void Result<int>::validateScalar(const response::Value& value);
929+
template <>
930+
GRAPHQLSERVICE_EXPORT void Result<double>::validateScalar(const response::Value& value);
931+
template <>
932+
GRAPHQLSERVICE_EXPORT void Result<std::string>::validateScalar(const response::Value& value);
933+
template <>
934+
GRAPHQLSERVICE_EXPORT void Result<bool>::validateScalar(const response::Value& value);
935+
template <>
936+
GRAPHQLSERVICE_EXPORT void Result<response::IdType>::validateScalar(const response::Value& value);
937+
template <>
938+
GRAPHQLSERVICE_EXPORT void Result<response::Value>::validateScalar(const response::Value& value);
939+
#endif // GRAPHQL_DLLEXPORTS
940+
941+
namespace {
942+
943+
// Test if this Type is Object.
944+
template <typename Type>
945+
concept ObjectType = std::is_same_v<Object, Type>;
946+
890947
// Test if this Type inherits from Object but is not Object itself.
891948
template <typename Type>
892949
concept ObjectDerivedType = ObjectBaseType<Type> && !ObjectType<Type>;
@@ -944,10 +1001,6 @@ struct ModifiedResult
9441001
AwaitableObject<std::shared_ptr<const Object>>, AwaitableScalar<type>>;
9451002
};
9461003

947-
// Convert a single value of the specified type to JSON.
948-
[[nodiscard]] static AwaitableResolver convert(
949-
typename ResultTraits<Type>::future_type result, ResolverParams params);
950-
9511004
// Peel off the none modifier. If it's included, it should always be last in the list.
9521005
template <TypeModifier Modifier = TypeModifier::None, TypeModifier... Other>
9531006
[[nodiscard]] static inline AwaitableResolver convert(
@@ -962,7 +1015,7 @@ struct ModifiedResult
9621015

9631016
co_await params.launch;
9641017

965-
auto awaitedResult = co_await ModifiedResult<Object>::convert(
1018+
auto awaitedResult = co_await Result<Object>::convert(
9661019
std::static_pointer_cast<const Object>(co_await result),
9671020
std::move(params));
9681021

@@ -978,7 +1031,7 @@ struct ModifiedResult
9781031
static_assert(sizeof...(Other) == 0, "None modifier should always be last");
9791032

9801033
// Just call through to the partial specialization without the modifier.
981-
return convert(std::move(result), std::move(params));
1034+
return Result<Type>::convert(std::move(result), std::move(params));
9821035
}
9831036

9841037
// Peel off final nullable modifiers for std::shared_ptr of Object and subclasses of Object.
@@ -1012,7 +1065,7 @@ struct ModifiedResult
10121065
typename ResultTraits<Type, Modifier, Other...>::type>,
10131066
"this is the optional version");
10141067

1015-
if constexpr (!std::is_base_of_v<Object, Type>)
1068+
if constexpr (!ObjectBaseType<Type>)
10161069
{
10171070
auto value = result.get_value();
10181071

@@ -1045,7 +1098,7 @@ struct ModifiedResult
10451098
typename ResultTraits<Type, Modifier, Other...>::future_type result,
10461099
ResolverParams params) requires ListModifier<Modifier>
10471100
{
1048-
if constexpr (!std::is_base_of_v<Object, Type>)
1101+
if constexpr (!ObjectBaseType<Type>)
10491102
{
10501103
auto value = result.get_value();
10511104

@@ -1141,11 +1194,6 @@ struct ModifiedResult
11411194
co_return document;
11421195
}
11431196

1144-
private
1145-
:
1146-
// Validate a single scalar value is the expected type.
1147-
static void
1148-
validateScalar(const response::Value& value);
11491197
// Peel off the none modifier. If it's included, it should always be last in the list.
11501198
template <TypeModifier Modifier = TypeModifier::None, TypeModifier... Other>
11511199
static inline void validateScalar(
@@ -1154,7 +1202,7 @@ private
11541202
static_assert(sizeof...(Other) == 0, "None modifier should always be last");
11551203

11561204
// Just call through to the partial specialization without the modifier.
1157-
validateScalar(value);
1205+
Result<Type>::validateScalar(value);
11581206
}
11591207

11601208
// Peel off nullable modifiers.
@@ -1190,14 +1238,13 @@ private
11901238
typename ResultTraits<Type>::future_type result, ResolverParams params,
11911239
ResolverCallback&& resolver)
11921240
{
1193-
static_assert(!std::is_base_of_v<Object, Type>,
1194-
"ModfiedResult<Object> needs special handling");
1241+
static_assert(!ObjectBaseType<Type>, "ModfiedResult<Object> needs special handling");
11951242

11961243
auto value = result.get_value();
11971244

11981245
if (value)
11991246
{
1200-
ModifiedResult::validateScalar(*value);
1247+
Result<Type>::validateScalar(*value);
12011248
co_return ResolverResult { response::Value { std::shared_ptr { std::move(value) } } };
12021249
}
12031250

@@ -1244,47 +1291,7 @@ using IdResult = ModifiedResult<response::IdType>;
12441291
using ScalarResult = ModifiedResult<response::Value>;
12451292
using ObjectResult = ModifiedResult<Object>;
12461293

1247-
#ifdef GRAPHQL_DLLEXPORTS
1248-
// Export all of the built-in converters
1249-
template <>
1250-
GRAPHQLSERVICE_EXPORT AwaitableResolver ModifiedResult<int>::convert(
1251-
AwaitableScalar<int> result, ResolverParams params);
1252-
template <>
1253-
GRAPHQLSERVICE_EXPORT AwaitableResolver ModifiedResult<double>::convert(
1254-
AwaitableScalar<double> result, ResolverParams params);
1255-
template <>
1256-
GRAPHQLSERVICE_EXPORT AwaitableResolver ModifiedResult<std::string>::convert(
1257-
AwaitableScalar<std::string> result, ResolverParams params);
1258-
template <>
1259-
GRAPHQLSERVICE_EXPORT AwaitableResolver ModifiedResult<bool>::convert(
1260-
AwaitableScalar<bool> result, ResolverParams params);
1261-
template <>
1262-
GRAPHQLSERVICE_EXPORT AwaitableResolver ModifiedResult<response::IdType>::convert(
1263-
AwaitableScalar<response::IdType> result, ResolverParams params);
1264-
template <>
1265-
GRAPHQLSERVICE_EXPORT AwaitableResolver ModifiedResult<response::Value>::convert(
1266-
AwaitableScalar<response::Value> result, ResolverParams params);
1267-
template <>
1268-
GRAPHQLSERVICE_EXPORT AwaitableResolver ModifiedResult<Object>::convert(
1269-
AwaitableObject<std::shared_ptr<const Object>> result, ResolverParams params);
1270-
1271-
// Export all of the scalar value validation methods
1272-
template <>
1273-
GRAPHQLSERVICE_EXPORT void ModifiedResult<int>::validateScalar(const response::Value& value);
1274-
template <>
1275-
GRAPHQLSERVICE_EXPORT void ModifiedResult<double>::validateScalar(const response::Value& value);
1276-
template <>
1277-
GRAPHQLSERVICE_EXPORT void ModifiedResult<std::string>::validateScalar(
1278-
const response::Value& value);
1279-
template <>
1280-
GRAPHQLSERVICE_EXPORT void ModifiedResult<bool>::validateScalar(const response::Value& value);
1281-
template <>
1282-
GRAPHQLSERVICE_EXPORT void ModifiedResult<response::IdType>::validateScalar(
1283-
const response::Value& value);
1284-
template <>
1285-
GRAPHQLSERVICE_EXPORT void ModifiedResult<response::Value>::validateScalar(
1286-
const response::Value& value);
1287-
#endif // GRAPHQL_DLLEXPORTS
1294+
} // namespace
12881295

12891296
// Subscription callbacks receive the response::Value representing the result of evaluating the
12901297
// SelectionSet against the payload.

include/graphqlservice/introspection/IntrospectionSchema.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,19 @@ template <>
180180
GRAPHQLSERVICE_EXPORT introspection::TypeKind Argument<introspection::TypeKind>::convert(
181181
const response::Value& value);
182182
template <>
183-
GRAPHQLSERVICE_EXPORT AwaitableResolver ModifiedResult<introspection::TypeKind>::convert(
183+
GRAPHQLSERVICE_EXPORT AwaitableResolver Result<introspection::TypeKind>::convert(
184184
AwaitableScalar<introspection::TypeKind> result, ResolverParams params);
185185
template <>
186-
GRAPHQLSERVICE_EXPORT void ModifiedResult<introspection::TypeKind>::validateScalar(
186+
GRAPHQLSERVICE_EXPORT void Result<introspection::TypeKind>::validateScalar(
187187
const response::Value& value);
188188
template <>
189189
GRAPHQLSERVICE_EXPORT introspection::DirectiveLocation Argument<introspection::DirectiveLocation>::convert(
190190
const response::Value& value);
191191
template <>
192-
GRAPHQLSERVICE_EXPORT AwaitableResolver ModifiedResult<introspection::DirectiveLocation>::convert(
192+
GRAPHQLSERVICE_EXPORT AwaitableResolver Result<introspection::DirectiveLocation>::convert(
193193
AwaitableScalar<introspection::DirectiveLocation> result, ResolverParams params);
194194
template <>
195-
GRAPHQLSERVICE_EXPORT void ModifiedResult<introspection::DirectiveLocation>::validateScalar(
195+
GRAPHQLSERVICE_EXPORT void Result<introspection::DirectiveLocation>::validateScalar(
196196
const response::Value& value);
197197
#endif // GRAPHQL_DLLEXPORTS
198198

samples/learn/schema/DroidObject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ service::AwaitableResolver Droid::resolvePrimaryFunction(service::ResolverParams
109109

110110
service::AwaitableResolver Droid::resolve_typename(service::ResolverParams&& params) const
111111
{
112-
return service::ModifiedResult<std::string>::convert(std::string{ R"gql(Droid)gql" }, std::move(params));
112+
return service::Result<std::string>::convert(std::string{ R"gql(Droid)gql" }, std::move(params));
113113
}
114114

115115
} // namespace object

samples/learn/schema/HumanObject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ service::AwaitableResolver Human::resolveHomePlanet(service::ResolverParams&& pa
109109

110110
service::AwaitableResolver Human::resolve_typename(service::ResolverParams&& params) const
111111
{
112-
return service::ModifiedResult<std::string>::convert(std::string{ R"gql(Human)gql" }, std::move(params));
112+
return service::Result<std::string>::convert(std::string{ R"gql(Human)gql" }, std::move(params));
113113
}
114114

115115
} // namespace object

samples/learn/schema/MutationObject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ service::AwaitableResolver Mutation::resolveCreateReview(service::ResolverParams
6666

6767
service::AwaitableResolver Mutation::resolve_typename(service::ResolverParams&& params) const
6868
{
69-
return service::ModifiedResult<std::string>::convert(std::string{ R"gql(Mutation)gql" }, std::move(params));
69+
return service::Result<std::string>::convert(std::string{ R"gql(Mutation)gql" }, std::move(params));
7070
}
7171

7272
} // namespace object

samples/learn/schema/QueryObject.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ service::AwaitableResolver Query::resolveDroid(service::ResolverParams&& params)
9595

9696
service::AwaitableResolver Query::resolve_typename(service::ResolverParams&& params) const
9797
{
98-
return service::ModifiedResult<std::string>::convert(std::string{ R"gql(Query)gql" }, std::move(params));
98+
return service::Result<std::string>::convert(std::string{ R"gql(Query)gql" }, std::move(params));
9999
}
100100

101101
service::AwaitableResolver Query::resolve_schema(service::ResolverParams&& params) const
102102
{
103-
return service::ModifiedResult<service::Object>::convert(std::static_pointer_cast<service::Object>(std::make_shared<introspection::object::Schema>(std::make_shared<introspection::Schema>(_schema))), std::move(params));
103+
return service::Result<service::Object>::convert(std::static_pointer_cast<service::Object>(std::make_shared<introspection::object::Schema>(std::make_shared<introspection::Schema>(_schema))), std::move(params));
104104
}
105105

106106
service::AwaitableResolver Query::resolve_type(service::ResolverParams&& params) const

samples/learn/schema/ReviewObject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ service::AwaitableResolver Review::resolveCommentary(service::ResolverParams&& p
7474

7575
service::AwaitableResolver Review::resolve_typename(service::ResolverParams&& params) const
7676
{
77-
return service::ModifiedResult<std::string>::convert(std::string{ R"gql(Review)gql" }, std::move(params));
77+
return service::Result<std::string>::convert(std::string{ R"gql(Review)gql" }, std::move(params));
7878
}
7979

8080
} // namespace object

samples/learn/schema/StarWarsSchema.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ learn::Episode Argument<learn::Episode>::convert(const response::Value& value)
4848
}
4949

5050
template <>
51-
service::AwaitableResolver ModifiedResult<learn::Episode>::convert(service::AwaitableScalar<learn::Episode> result, ResolverParams params)
51+
service::AwaitableResolver Result<learn::Episode>::convert(service::AwaitableScalar<learn::Episode> result, ResolverParams params)
5252
{
53-
return resolve(std::move(result), std::move(params),
53+
return ModifiedResult<learn::Episode>::resolve(std::move(result), std::move(params),
5454
[](learn::Episode value, const ResolverParams&)
5555
{
5656
response::Value result(response::Type::EnumValue);
@@ -62,7 +62,7 @@ service::AwaitableResolver ModifiedResult<learn::Episode>::convert(service::Awai
6262
}
6363

6464
template <>
65-
void ModifiedResult<learn::Episode>::validateScalar(const response::Value& value)
65+
void Result<learn::Episode>::validateScalar(const response::Value& value)
6666
{
6767
if (!value.maybe_enum())
6868
{

samples/today/nointrospection/AppointmentConnectionObject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ service::AwaitableResolver AppointmentConnection::resolveEdges(service::Resolver
7676

7777
service::AwaitableResolver AppointmentConnection::resolve_typename(service::ResolverParams&& params) const
7878
{
79-
return service::ModifiedResult<std::string>::convert(std::string{ R"gql(AppointmentConnection)gql" }, std::move(params));
79+
return service::Result<std::string>::convert(std::string{ R"gql(AppointmentConnection)gql" }, std::move(params));
8080
}
8181

8282
} // namespace object

samples/today/nointrospection/AppointmentEdgeObject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ service::AwaitableResolver AppointmentEdge::resolveCursor(service::ResolverParam
7575

7676
service::AwaitableResolver AppointmentEdge::resolve_typename(service::ResolverParams&& params) const
7777
{
78-
return service::ModifiedResult<std::string>::convert(std::string{ R"gql(AppointmentEdge)gql" }, std::move(params));
78+
return service::Result<std::string>::convert(std::string{ R"gql(AppointmentEdge)gql" }, std::move(params));
7979
}
8080

8181
} // namespace object

0 commit comments

Comments
 (0)