Skip to content

Commit 7c8b096

Browse files
committed
Replace generated isInputType functions with concepts
1 parent 681c08d commit 7c8b096

File tree

7 files changed

+141
-187
lines changed

7 files changed

+141
-187
lines changed

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 });

samples/learn/schema/StarWarsSchema.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,6 @@ void AddMutationDetails(const std::shared_ptr<schema::ObjectType>& typeMutation,
109109
std::shared_ptr<schema::Schema> GetSchema();
110110

111111
} // namespace learn
112-
113-
namespace service {
114-
115-
template <>
116-
[[nodiscard]] constexpr bool isInputType<learn::ReviewInput>() noexcept
117-
{
118-
return true;
119-
}
120-
121-
} // namespace service
122112
} // namespace graphql
123113

124114
#endif // STARWARSSCHEMA_H

samples/today/nointrospection/TodaySchema.h

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -282,64 +282,6 @@ void AddExpensiveDetails(const std::shared_ptr<schema::ObjectType>& typeExpensiv
282282
std::shared_ptr<schema::Schema> GetSchema();
283283

284284
} // namespace today
285-
286-
namespace service {
287-
288-
template <>
289-
[[nodiscard]] constexpr bool isInputType<today::CompleteTaskInput>() noexcept
290-
{
291-
return true;
292-
}
293-
294-
template <>
295-
[[nodiscard]] constexpr bool isInputType<today::ThirdNestedInput>() noexcept
296-
{
297-
return true;
298-
}
299-
300-
template <>
301-
[[nodiscard]] constexpr bool isInputType<today::FourthNestedInput>() noexcept
302-
{
303-
return true;
304-
}
305-
306-
template <>
307-
[[nodiscard]] constexpr bool isInputType<today::IncludeNullableSelfInput>() noexcept
308-
{
309-
return true;
310-
}
311-
312-
template <>
313-
[[nodiscard]] constexpr bool isInputType<today::IncludeNonNullableListSelfInput>() noexcept
314-
{
315-
return true;
316-
}
317-
318-
template <>
319-
[[nodiscard]] constexpr bool isInputType<today::StringOperationFilterInput>() noexcept
320-
{
321-
return true;
322-
}
323-
324-
template <>
325-
[[nodiscard]] constexpr bool isInputType<today::SecondNestedInput>() noexcept
326-
{
327-
return true;
328-
}
329-
330-
template <>
331-
[[nodiscard]] constexpr bool isInputType<today::ForwardDeclaredInput>() noexcept
332-
{
333-
return true;
334-
}
335-
336-
template <>
337-
[[nodiscard]] constexpr bool isInputType<today::FirstNestedInput>() noexcept
338-
{
339-
return true;
340-
}
341-
342-
} // namespace service
343285
} // namespace graphql
344286

345287
#endif // TODAYSCHEMA_H

samples/today/schema/TodaySchema.h

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -282,64 +282,6 @@ void AddExpensiveDetails(const std::shared_ptr<schema::ObjectType>& typeExpensiv
282282
std::shared_ptr<schema::Schema> GetSchema();
283283

284284
} // namespace today
285-
286-
namespace service {
287-
288-
template <>
289-
[[nodiscard]] constexpr bool isInputType<today::CompleteTaskInput>() noexcept
290-
{
291-
return true;
292-
}
293-
294-
template <>
295-
[[nodiscard]] constexpr bool isInputType<today::ThirdNestedInput>() noexcept
296-
{
297-
return true;
298-
}
299-
300-
template <>
301-
[[nodiscard]] constexpr bool isInputType<today::FourthNestedInput>() noexcept
302-
{
303-
return true;
304-
}
305-
306-
template <>
307-
[[nodiscard]] constexpr bool isInputType<today::IncludeNullableSelfInput>() noexcept
308-
{
309-
return true;
310-
}
311-
312-
template <>
313-
[[nodiscard]] constexpr bool isInputType<today::IncludeNonNullableListSelfInput>() noexcept
314-
{
315-
return true;
316-
}
317-
318-
template <>
319-
[[nodiscard]] constexpr bool isInputType<today::StringOperationFilterInput>() noexcept
320-
{
321-
return true;
322-
}
323-
324-
template <>
325-
[[nodiscard]] constexpr bool isInputType<today::SecondNestedInput>() noexcept
326-
{
327-
return true;
328-
}
329-
330-
template <>
331-
[[nodiscard]] constexpr bool isInputType<today::ForwardDeclaredInput>() noexcept
332-
{
333-
return true;
334-
}
335-
336-
template <>
337-
[[nodiscard]] constexpr bool isInputType<today::FirstNestedInput>() noexcept
338-
{
339-
return true;
340-
}
341-
342-
} // namespace service
343285
} // namespace graphql
344286

345287
#endif // TODAYSCHEMA_H

samples/validation/schema/ValidationSchema.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,6 @@ void AddArgumentsDetails(const std::shared_ptr<schema::ObjectType>& typeArgument
158158
std::shared_ptr<schema::Schema> GetSchema();
159159

160160
} // namespace validation
161-
162-
namespace service {
163-
164-
template <>
165-
[[nodiscard]] constexpr bool isInputType<validation::ComplexInput>() noexcept
166-
{
167-
return true;
168-
}
169-
170-
} // namespace service
171161
} // namespace graphql
172162

173163
#endif // VALIDATIONSCHEMA_H

src/SchemaGenerator.cpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -645,32 +645,6 @@ GRAPHQLSERVICE_EXPORT )cpp" << _loader.getSchemaNamespace()
645645
)cpp";
646646
}
647647

648-
if (!_loader.getInputTypes().empty())
649-
{
650-
if (schemaNamespace.exit())
651-
{
652-
headerFile << std::endl;
653-
}
654-
655-
if (serviceNamespace.enter())
656-
{
657-
headerFile << std::endl;
658-
}
659-
660-
for (const auto& inputType : _loader.getInputTypes())
661-
{
662-
headerFile << R"cpp(template <>
663-
[[nodiscard]] constexpr bool isInputType<)cpp"
664-
<< _loader.getSchemaNamespace() << R"cpp(::)cpp" << inputType.cppType
665-
<< R"cpp(>() noexcept
666-
{
667-
return true;
668-
}
669-
670-
)cpp";
671-
}
672-
}
673-
674648
return true;
675649
}
676650

0 commit comments

Comments
 (0)