From 06e9b7e03330cafd481c7647122812a0a6a712bf Mon Sep 17 00:00:00 2001 From: Darshan Ram Venkataraman Lakshminarasimhan <75383269+DashRam64@users.noreply.github.com> Date: Sat, 28 Nov 2026 13:18:07 +0000 Subject: [PATCH] Suggest an input type when schemagen finds an output type as an argument Using an object, interface, or union type as a field argument or input object field failed with only "Invalid argument type: ", which doesn't say what is wrong. The error now names the kind of type and says that arguments and input fields must be a scalar, enum, or input type, so an input type should be declared instead. Adds schemaloader_tests covering object, interface, and union arguments, an object type used as an input field, and a valid input type argument. Fixes #300 --- src/SchemaLoader.cpp | 23 ++++++ test/CMakeLists.txt | 15 ++++ test/SchemaLoaderTests.cpp | 160 +++++++++++++++++++++++++++++++++++++ 3 files changed, 198 insertions(+) create mode 100644 test/SchemaLoaderTests.cpp diff --git a/src/SchemaLoader.cpp b/src/SchemaLoader.cpp index 9129ec48..86701044 100644 --- a/src/SchemaLoader.cpp +++ b/src/SchemaLoader.cpp @@ -369,6 +369,29 @@ void SchemaLoader::fixupInputFieldList(InputFieldList& fields) << " column: " << entry.position->column; } + // Object, interface, and union types are output types, so they cannot be used as + // arguments or input object fields. Suggest declaring an input type instead. + std::string_view typeKind; + + switch (itr->second) + { + case SchemaType::Interface: + typeKind = "an interface"sv; + break; + + case SchemaType::Union: + typeKind = "a union"sv; + break; + + default: + typeKind = "an object"sv; + break; + } + + error << " (" << entry.type << " is " << typeKind + << " type, but arguments and input fields must be a scalar, enum, or input " + "type; declare an input type to use here instead)"; + throw std::runtime_error(error.str()); } } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a3b0d52f..b9279c33 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -98,6 +98,17 @@ target_include_directories(response_tests PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../include) gtest_add_tests(TARGET response_tests) +if(GRAPHQL_BUILD_SCHEMAGEN OR GRAPHQL_BUILD_CLIENTGEN) + add_executable(schemaloader_tests SchemaLoaderTests.cpp) + target_link_libraries(schemaloader_tests PRIVATE + generator_util + GTest::GTest + GTest::Main) + target_include_directories(schemaloader_tests PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/../include) + gtest_add_tests(TARGET schemaloader_tests) +endif() + if(WIN32 AND BUILD_SHARED_LIBS) add_custom_command(OUTPUT copied_test_dlls COMMAND ${CMAKE_COMMAND} -E copy_if_different @@ -126,4 +137,8 @@ if(WIN32 AND BUILD_SHARED_LIBS) add_dependencies(pegtl_executable_tests copy_test_dlls) add_dependencies(pegtl_schema_tests copy_test_dlls) add_dependencies(response_tests copy_test_dlls) + + if(GRAPHQL_BUILD_SCHEMAGEN OR GRAPHQL_BUILD_CLIENTGEN) + add_dependencies(schemaloader_tests copy_test_dlls) + endif() endif() diff --git a/test/SchemaLoaderTests.cpp b/test/SchemaLoaderTests.cpp new file mode 100644 index 00000000..51c7ae78 --- /dev/null +++ b/test/SchemaLoaderTests.cpp @@ -0,0 +1,160 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +#include + +#include "SchemaLoader.h" + +#include +#include +#include +#include +#include + +using namespace graphql; + +using namespace std::literals; + +namespace { + +// SchemaLoader reads the schema from a file, so write each test schema to a scratch file in the +// working directory and return the error message from loading it (empty if it loaded). +std::string loadSchemaError(std::string_view testName, std::string_view schema) +{ + const auto schemaPath = "schemaloader_"s + std::string { testName } + ".graphql"s; + + { + std::ofstream schemaFile(schemaPath, std::ios::binary | std::ios::trunc); + + schemaFile << schema; + } + + std::string error; + + try + { + generator::SchemaLoader loader({ schemaPath, "Test", "test" }); + } + catch (const std::exception& ex) + { + error = ex.what(); + } + + std::remove(schemaPath.c_str()); + + return error; +} + +} // namespace + +TEST(SchemaLoaderCase, ObjectTypeArgumentSuggestsInputType) +{ + const auto error = loadSchemaError("ObjectTypeArgument", R"gql( +type ContactMethod { + name: String +} + +type Contact { + id: ID +} + +type Query { + getContactsByContactMethod(search: ContactMethod): [Contact] +} +)gql"sv); + + EXPECT_EQ( + "Invalid argument type: ContactMethod line: 11 column: 29 (ContactMethod is an object " + "type, " + "but arguments and input fields must be a scalar, enum, or input type; declare an input " + "type to use here instead)", + error); +} + +TEST(SchemaLoaderCase, InterfaceTypeArgumentSuggestsInputType) +{ + const auto error = loadSchemaError("InterfaceTypeArgument", R"gql( +interface Node { + id: ID! +} + +type Thing implements Node { + id: ID! +} + +type Query { + node(match: Node): Thing +} +)gql"sv); + + EXPECT_EQ("Invalid argument type: Node line: 11 column: 7 (Node is an interface type, but " + "arguments and input fields must be a scalar, enum, or input type; declare an input " + "type to use here instead)", + error); +} + +TEST(SchemaLoaderCase, UnionTypeArgumentSuggestsInputType) +{ + const auto error = loadSchemaError("UnionTypeArgument", R"gql( +type A { + a: Int +} + +type B { + b: Int +} + +union AorB = A | B + +type Query { + find(matches: [AorB!]): A +} +)gql"sv); + + EXPECT_EQ("Invalid argument type: AorB line: 13 column: 7 (AorB is a union type, but " + "arguments and input fields must be a scalar, enum, or input type; declare an input " + "type to use here instead)", + error); +} + +TEST(SchemaLoaderCase, ObjectTypeInputFieldSuggestsInputType) +{ + const auto error = loadSchemaError("ObjectTypeInputField", R"gql( +type Contact { + id: ID +} + +input ContactFilter { + contact: Contact +} + +type Query { + contacts(filter: ContactFilter): [Contact] +} +)gql"sv); + + EXPECT_EQ( + "Invalid argument type: Contact line: 7 column: 2 (Contact is an object type, but " + "arguments and input fields must be a scalar, enum, or input type; declare an input type " + "to use here instead)", + error); +} + +TEST(SchemaLoaderCase, InputTypeArgumentIsValid) +{ + const auto error = loadSchemaError("InputTypeArgument", R"gql( +input ContactMethodInput { + name: String +} + +type Contact { + id: ID +} + +type Query { + getContactsByContactMethod(search: ContactMethodInput): [Contact] +} +)gql"sv); + + EXPECT_TRUE(error.empty()) << error; +}