Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/SchemaLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down
15 changes: 15 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
160 changes: 160 additions & 0 deletions test/SchemaLoaderTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#include <gtest/gtest.h>

#include "SchemaLoader.h"

#include <cstdio>
#include <fstream>
#include <stdexcept>
#include <string>
#include <string_view>

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