Skip to content

Commit 96b3365

Browse files
committed
Add [[nodiscard]] to first declaration of input types
1 parent 03e0cce commit 96b3365

File tree

8 files changed

+49
-27
lines changed

8 files changed

+49
-27
lines changed

samples/learn/schema/StarWarsSchema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ enum class [[nodiscard]] Episode
5151
};
5252
}
5353

54-
struct ReviewInput
54+
struct [[nodiscard]] ReviewInput
5555
{
5656
int stars {};
5757
std::optional<std::string> commentary {};

samples/today/nointrospection/TodaySchema.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,11 @@ template <>
115115
today::ThirdNestedInput ModifiedArgument<today::ThirdNestedInput>::convert(const response::Value& value)
116116
{
117117
auto valueId = service::ModifiedArgument<response::IdType>::require("id", value);
118+
auto valueSecond = service::ModifiedArgument<today::SecondNestedInput>::require<service::TypeModifier::Nullable>("second", value);
118119

119120
return {
120-
std::move(valueId)
121+
std::move(valueId),
122+
std::move(valueSecond)
121123
};
122124
}
123125

@@ -312,7 +314,8 @@ void AddTypesToSchema(const std::shared_ptr<schema::Schema>& schema)
312314
schema::InputValue::Make(R"gql(clientMutationId)gql"sv, R"md()md"sv, schema->LookupType(R"gql(String)gql"sv), R"gql()gql"sv)
313315
});
314316
typeThirdNestedInput->AddInputValues({
315-
schema::InputValue::Make(R"gql(id)gql"sv, R"md()md"sv, schema->WrapType(introspection::TypeKind::NON_NULL, schema->LookupType(R"gql(ID)gql"sv)), R"gql()gql"sv)
317+
schema::InputValue::Make(R"gql(id)gql"sv, R"md()md"sv, schema->WrapType(introspection::TypeKind::NON_NULL, schema->LookupType(R"gql(ID)gql"sv)), R"gql()gql"sv),
318+
schema::InputValue::Make(R"gql(second)gql"sv, R"md()md"sv, schema->LookupType(R"gql(SecondNestedInput)gql"sv), R"gql()gql"sv)
316319
});
317320
typeFourthNestedInput->AddInputValues({
318321
schema::InputValue::Make(R"gql(id)gql"sv, R"md()md"sv, schema->WrapType(introspection::TypeKind::NON_NULL, schema->LookupType(R"gql(ID)gql"sv)), R"gql()gql"sv)

samples/today/nointrospection/TodaySchema.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,35 +54,38 @@ enum class [[nodiscard]] TaskState
5454
};
5555
}
5656

57-
struct CompleteTaskInput
57+
struct [[nodiscard]] CompleteTaskInput
5858
{
5959
response::IdType id {};
6060
std::optional<TaskState> testTaskState {};
6161
std::optional<bool> isComplete {};
6262
std::optional<std::string> clientMutationId {};
6363
};
6464

65-
struct ThirdNestedInput
65+
struct [[nodiscard]] SecondNestedInput;
66+
67+
struct [[nodiscard]] ThirdNestedInput
6668
{
6769
response::IdType id {};
70+
std::unique_ptr<SecondNestedInput> second {};
6871
};
6972

70-
struct FourthNestedInput
73+
struct [[nodiscard]] FourthNestedInput
7174
{
7275
response::IdType id {};
7376
};
7477

75-
struct IncludeNullableSelfInput
78+
struct [[nodiscard]] IncludeNullableSelfInput
7679
{
7780
std::unique_ptr<IncludeNullableSelfInput> self {};
7881
};
7982

80-
struct IncludeNonNullableListSelfInput
83+
struct [[nodiscard]] IncludeNonNullableListSelfInput
8184
{
8285
std::vector<IncludeNonNullableListSelfInput> selves {};
8386
};
8487

85-
struct StringOperationFilterInput
88+
struct [[nodiscard]] StringOperationFilterInput
8689
{
8790
std::optional<std::vector<StringOperationFilterInput>> and_ {};
8891
std::optional<std::vector<StringOperationFilterInput>> or_ {};
@@ -104,13 +107,13 @@ struct SecondNestedInput
104107
ThirdNestedInput third {};
105108
};
106109

107-
struct ForwardDeclaredInput
110+
struct [[nodiscard]] ForwardDeclaredInput
108111
{
109112
std::unique_ptr<IncludeNullableSelfInput> nullableSelf {};
110113
IncludeNonNullableListSelfInput listSelves {};
111114
};
112115

113-
struct FirstNestedInput
116+
struct [[nodiscard]] FirstNestedInput
114117
{
115118
response::IdType id {};
116119
SecondNestedInput second {};

samples/today/schema.today.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ input SecondNestedInput {
179179

180180
input ThirdNestedInput {
181181
id: ID!
182+
second: SecondNestedInput
182183
}
183184

184185
input FourthNestedInput {

samples/today/schema/TodaySchema.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,11 @@ template <>
115115
today::ThirdNestedInput ModifiedArgument<today::ThirdNestedInput>::convert(const response::Value& value)
116116
{
117117
auto valueId = service::ModifiedArgument<response::IdType>::require("id", value);
118+
auto valueSecond = service::ModifiedArgument<today::SecondNestedInput>::require<service::TypeModifier::Nullable>("second", value);
118119

119120
return {
120-
std::move(valueId)
121+
std::move(valueId),
122+
std::move(valueSecond)
121123
};
122124
}
123125

@@ -315,7 +317,8 @@ void AddTypesToSchema(const std::shared_ptr<schema::Schema>& schema)
315317
schema::InputValue::Make(R"gql(clientMutationId)gql"sv, R"md()md"sv, schema->LookupType(R"gql(String)gql"sv), R"gql()gql"sv)
316318
});
317319
typeThirdNestedInput->AddInputValues({
318-
schema::InputValue::Make(R"gql(id)gql"sv, R"md()md"sv, schema->WrapType(introspection::TypeKind::NON_NULL, schema->LookupType(R"gql(ID)gql"sv)), R"gql()gql"sv)
320+
schema::InputValue::Make(R"gql(id)gql"sv, R"md()md"sv, schema->WrapType(introspection::TypeKind::NON_NULL, schema->LookupType(R"gql(ID)gql"sv)), R"gql()gql"sv),
321+
schema::InputValue::Make(R"gql(second)gql"sv, R"md()md"sv, schema->LookupType(R"gql(SecondNestedInput)gql"sv), R"gql()gql"sv)
319322
});
320323
typeFourthNestedInput->AddInputValues({
321324
schema::InputValue::Make(R"gql(id)gql"sv, R"md()md"sv, schema->WrapType(introspection::TypeKind::NON_NULL, schema->LookupType(R"gql(ID)gql"sv)), R"gql()gql"sv)

samples/today/schema/TodaySchema.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,35 +54,38 @@ enum class [[nodiscard]] TaskState
5454
};
5555
}
5656

57-
struct CompleteTaskInput
57+
struct [[nodiscard]] CompleteTaskInput
5858
{
5959
response::IdType id {};
6060
std::optional<TaskState> testTaskState {};
6161
std::optional<bool> isComplete {};
6262
std::optional<std::string> clientMutationId {};
6363
};
6464

65-
struct ThirdNestedInput
65+
struct [[nodiscard]] SecondNestedInput;
66+
67+
struct [[nodiscard]] ThirdNestedInput
6668
{
6769
response::IdType id {};
70+
std::unique_ptr<SecondNestedInput> second {};
6871
};
6972

70-
struct FourthNestedInput
73+
struct [[nodiscard]] FourthNestedInput
7174
{
7275
response::IdType id {};
7376
};
7477

75-
struct IncludeNullableSelfInput
78+
struct [[nodiscard]] IncludeNullableSelfInput
7679
{
7780
std::unique_ptr<IncludeNullableSelfInput> self {};
7881
};
7982

80-
struct IncludeNonNullableListSelfInput
83+
struct [[nodiscard]] IncludeNonNullableListSelfInput
8184
{
8285
std::vector<IncludeNonNullableListSelfInput> selves {};
8386
};
8487

85-
struct StringOperationFilterInput
88+
struct [[nodiscard]] StringOperationFilterInput
8689
{
8790
std::optional<std::vector<StringOperationFilterInput>> and_ {};
8891
std::optional<std::vector<StringOperationFilterInput>> or_ {};
@@ -104,13 +107,13 @@ struct SecondNestedInput
104107
ThirdNestedInput third {};
105108
};
106109

107-
struct ForwardDeclaredInput
110+
struct [[nodiscard]] ForwardDeclaredInput
108111
{
109112
std::unique_ptr<IncludeNullableSelfInput> nullableSelf {};
110113
IncludeNonNullableListSelfInput listSelves {};
111114
};
112115

113-
struct FirstNestedInput
116+
struct [[nodiscard]] FirstNestedInput
114117
{
115118
response::IdType id {};
116119
SecondNestedInput second {};

samples/validation/schema/ValidationSchema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ enum class [[nodiscard]] CatCommand
7474
};
7575
}
7676

77-
struct ComplexInput
77+
struct [[nodiscard]] ComplexInput
7878
{
7979
std::optional<std::string> name {};
8080
std::optional<std::string> owner {};

src/SchemaGenerator.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,16 +266,15 @@ static_assert(graphql::internal::MinorVersion == )cpp"
266266
// Output the full declarations
267267
for (const auto& inputType : _loader.getInputTypes())
268268
{
269-
forwardDeclared.insert(inputType.type);
270-
271269
if (!inputType.declarations.empty())
272270
{
273271
// Forward declare nullable dependencies
274272
for (auto declaration : inputType.declarations)
275273
{
276-
if (forwardDeclared.insert(declaration).second)
274+
if (declaration != inputType.cppType
275+
&& forwardDeclared.insert(declaration).second)
277276
{
278-
headerFile << R"cpp(struct )cpp" << declaration << R"cpp(;
277+
headerFile << R"cpp(struct [[nodiscard]] )cpp" << declaration << R"cpp(;
279278
)cpp";
280279
pendingSeparator.add();
281280
}
@@ -284,9 +283,19 @@ static_assert(graphql::internal::MinorVersion == )cpp"
284283
pendingSeparator.reset();
285284
}
286285

287-
headerFile << R"cpp(struct )cpp" << inputType.cppType << R"cpp(
286+
headerFile << R"cpp(struct )cpp";
287+
288+
if (forwardDeclared.find(inputType.cppType) == forwardDeclared.end())
289+
{
290+
headerFile << R"cpp([[nodiscard]] )cpp";
291+
}
292+
293+
headerFile << inputType.cppType << R"cpp(
288294
{
289295
)cpp";
296+
297+
forwardDeclared.insert(inputType.cppType);
298+
290299
for (const auto& inputField : inputType.fields)
291300
{
292301
headerFile << getFieldDeclaration(inputField);

0 commit comments

Comments
 (0)