Skip to content

Commit 4a57b2b

Browse files
committed
Add a test for unimplemented field errors
1 parent 1898ea1 commit 4a57b2b

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

samples/TodaySchema.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ Query::Query()
106106
{ "tasksById", [this](service::ResolverParams&& params) { return resolveTasksById(std::move(params)); } },
107107
{ "unreadCountsById", [this](service::ResolverParams&& params) { return resolveUnreadCountsById(std::move(params)); } },
108108
{ "nested", [this](service::ResolverParams&& params) { return resolveNested(std::move(params)); } },
109+
{ "unimplemented", [this](service::ResolverParams&& params) { return resolveUnimplemented(std::move(params)); } },
109110
{ "__typename", [this](service::ResolverParams&& params) { return resolve__typename(std::move(params)); } },
110111
{ "__schema", [this](service::ResolverParams&& params) { return resolve__schema(std::move(params)); } },
111112
{ "__type", [this](service::ResolverParams&& params) { return resolve__type(std::move(params)); } }
@@ -282,6 +283,22 @@ std::future<response::Value> Query::resolveNested(service::ResolverParams&& para
282283
return service::ModifiedResult<NestedType>::convert(std::move(result), std::move(params));
283284
}
284285

286+
std::future<response::StringType> Query::getUnimplemented(service::FieldParams&&) const
287+
{
288+
std::promise<response::StringType> promise;
289+
290+
promise.set_exception(std::make_exception_ptr(std::runtime_error(R"ex(Query::getUnimplemented is not implemented)ex")));
291+
292+
return promise.get_future();
293+
}
294+
295+
std::future<response::Value> Query::resolveUnimplemented(service::ResolverParams&& params)
296+
{
297+
auto result = getUnimplemented(service::FieldParams(params, std::move(params.fieldDirectives)));
298+
299+
return service::ModifiedResult<response::StringType>::convert(std::move(result), std::move(params));
300+
}
301+
285302
std::future<response::Value> Query::resolve__typename(service::ResolverParams&& params)
286303
{
287304
std::promise<response::StringType> promise;
@@ -1205,7 +1222,8 @@ void AddTypesToSchema(std::shared_ptr<introspection::Schema> schema)
12051222
std::make_shared<introspection::Field>("unreadCountsById", R"md()md", std::unique_ptr<std::string>(nullptr), std::vector<std::shared_ptr<introspection::InputValue>>({
12061223
std::make_shared<introspection::InputValue>("ids", R"md()md", schema->WrapType(introspection::__TypeKind::NON_NULL, schema->WrapType(introspection::__TypeKind::LIST, schema->WrapType(introspection::__TypeKind::NON_NULL, schema->LookupType("ID")))), R"gql()gql")
12071224
}), schema->WrapType(introspection::__TypeKind::NON_NULL, schema->WrapType(introspection::__TypeKind::LIST, schema->LookupType("Folder")))),
1208-
std::make_shared<introspection::Field>("nested", R"md()md", std::unique_ptr<std::string>(nullptr), std::vector<std::shared_ptr<introspection::InputValue>>(), schema->WrapType(introspection::__TypeKind::NON_NULL, schema->LookupType("NestedType")))
1225+
std::make_shared<introspection::Field>("nested", R"md()md", std::unique_ptr<std::string>(nullptr), std::vector<std::shared_ptr<introspection::InputValue>>(), schema->WrapType(introspection::__TypeKind::NON_NULL, schema->LookupType("NestedType"))),
1226+
std::make_shared<introspection::Field>("unimplemented", R"md()md", std::unique_ptr<std::string>(nullptr), std::vector<std::shared_ptr<introspection::InputValue>>(), schema->WrapType(introspection::__TypeKind::NON_NULL, schema->LookupType("String")))
12091227
});
12101228
typePageInfo->AddFields({
12111229
std::make_shared<introspection::Field>("hasNextPage", R"md()md", std::unique_ptr<std::string>(nullptr), std::vector<std::shared_ptr<introspection::InputValue>>(), schema->WrapType(introspection::__TypeKind::NON_NULL, schema->LookupType("Boolean"))),

samples/TodaySchema.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class Query
7676
virtual std::future<std::vector<std::shared_ptr<Task>>> getTasksById(service::FieldParams&& params, std::vector<std::vector<uint8_t>>&& idsArg) const;
7777
virtual std::future<std::vector<std::shared_ptr<Folder>>> getUnreadCountsById(service::FieldParams&& params, std::vector<std::vector<uint8_t>>&& idsArg) const;
7878
virtual std::future<std::shared_ptr<NestedType>> getNested(service::FieldParams&& params) const;
79+
virtual std::future<response::StringType> getUnimplemented(service::FieldParams&& params) const;
7980

8081
private:
8182
std::future<response::Value> resolveNode(service::ResolverParams&& params);
@@ -86,6 +87,7 @@ class Query
8687
std::future<response::Value> resolveTasksById(service::ResolverParams&& params);
8788
std::future<response::Value> resolveUnreadCountsById(service::ResolverParams&& params);
8889
std::future<response::Value> resolveNested(service::ResolverParams&& params);
90+
std::future<response::Value> resolveUnimplemented(service::ResolverParams&& params);
8991

9092
std::future<response::Value> resolve__typename(service::ResolverParams&& params);
9193
std::future<response::Value> resolve__schema(service::ResolverParams&& params);

samples/schema.today.graphql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type Query {
2626
unreadCountsById(ids: [ID!]!): [Folder]!
2727

2828
nested: NestedType!
29+
30+
unimplemented: String!
2931
}
3032

3133
"Node interface for Relay support"

tests.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,30 @@ TEST_F(TodayServiceCase, QueryAppointmentsById)
838838
}
839839
}
840840

841+
TEST_F(TodayServiceCase, UnimplementedFieldError)
842+
{
843+
auto ast = R"(query {
844+
unimplemented
845+
})"_graphql;
846+
response::Value variables(response::Type::Map);
847+
auto result = _service->resolve(nullptr, *ast.root, "", std::move(variables)).get();
848+
849+
try
850+
{
851+
ASSERT_TRUE(result.type() == response::Type::Map);
852+
const auto& errors = result["errors"];
853+
ASSERT_TRUE(errors.type() == response::Type::List);
854+
ASSERT_EQ(size_t(1), errors.size());
855+
const auto& message = errors[0];
856+
ASSERT_TRUE(message.type() == response::Type::String);
857+
ASSERT_EQ(R"e(Field name: unimplemented unknown error: Query::getUnimplemented is not implemented)e", message.get<const response::StringType&>());
858+
}
859+
catch (const service::schema_exception& ex)
860+
{
861+
FAIL() << response::toJSON(response::Value(ex.getErrors()));
862+
}
863+
}
864+
841865
TEST_F(TodayServiceCase, SubscribeNodeChangeMatchingId)
842866
{
843867
auto ast = peg::parseString(R"(subscription TestSubscription {

0 commit comments

Comments
 (0)