Skip to content

Commit

Permalink
Merge pull request #71 from wravery/master
Browse files Browse the repository at this point in the history
Handle blank lines in the sample app and fix problems with schema_exception
  • Loading branch information
wravery committed Sep 1, 2019
2 parents 2317389 + e353b01 commit ab32d4f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 24 deletions.
34 changes: 27 additions & 7 deletions include/graphqlservice/GraphQLService.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ namespace graphql::service {
class schema_exception : public std::exception
{
public:
schema_exception() = delete;
schema_exception(const schema_exception&) = delete;
explicit schema_exception(schema_exception&&) = default;

explicit schema_exception(std::vector<std::string>&& messages);

schema_exception() = delete;

const char* what() const noexcept override;

const response::Value& getErrors() const noexcept;
Expand Down Expand Up @@ -555,6 +553,16 @@ struct ModifiedResult
}
}
}
catch (schema_exception& scx)
{
auto messages = scx.getErrors().release<response::ListType>();

errors.reserve(errors.size() + messages.size());
for (auto& error : messages)
{
errors.emplace_back(std::move(error));
}
}
catch (const std::exception & ex)
{
std::ostringstream message;
Expand Down Expand Up @@ -595,11 +603,16 @@ struct ModifiedResult
return std::async(std::launch::deferred,
[](auto && resultFuture, ResolverParams && paramsFuture, ResolverCallback && resolverFuture) noexcept
{
response::Value document(response::Type::Map);
response::Value data;
response::Value errors(response::Type::List);

try
{
document.emplace_back(std::string{ strData }, resolverFuture(resultFuture.get(), paramsFuture));
data = resolverFuture(resultFuture.get(), paramsFuture);
}
catch (schema_exception& scx)
{
errors = scx.getErrors();
}
catch (const std::exception & ex)
{
Expand All @@ -608,11 +621,18 @@ struct ModifiedResult
message << "Field name: " << paramsFuture.fieldName
<< " unknown error: " << ex.what();

response::Value errors(response::Type::List);
response::Value error(response::Type::Map);

error.emplace_back(std::string{ strMessage }, response::Value(message.str()));
errors.emplace_back(std::move(error));
}

response::Value document(response::Type::Map);

document.emplace_back(std::string{ strData }, std::move(data));

if (errors.size() > 0)
{
document.emplace_back(std::string{ strErrors }, std::move(errors));
}

Expand Down
10 changes: 3 additions & 7 deletions samples/today/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <graphqlservice/JSONResponse.h>

#include <iterator>
#include <iostream>
#include <stdexcept>
#include <cstdio>
Expand Down Expand Up @@ -66,13 +67,8 @@ int main(int argc, char** argv)
}
else
{
std::string input;
std::string line;

while (std::getline(std::cin, line))
{
input.append(line);
}
std::istream_iterator<char> start{ std::cin >> std::noskipws }, end{};
std::string input{ start, end };

query = peg::parseString(std::move(input));
}
Expand Down
23 changes: 13 additions & 10 deletions src/GraphQLResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,37 +69,36 @@ struct TypedData : std::variant<

Value::Value(Type type /*= Type::Null*/)
: _type(type)
, _data(std::make_unique<TypedData>())
{
switch (type)
{
case Type::Map:
*_data = { std::make_optional<MapData>() };
_data = std::make_unique<TypedData>(TypedData{ std::make_optional<MapData>() });
break;

case Type::List:
*_data = { std::make_optional<ListData>() };
_data = std::make_unique<TypedData>(TypedData{ std::make_optional<ListData>() });
break;

case Type::String:
case Type::EnumValue:
*_data = { std::make_optional<StringOrEnumData>() };
_data = std::make_unique<TypedData>(TypedData{ std::make_optional<StringOrEnumData>() });
break;

case Type::Scalar:
*_data = { std::make_optional<ScalarData>() };
_data = std::make_unique<TypedData>(TypedData{ std::make_optional<ScalarData>() });
break;

case Type::Boolean:
*_data = { BooleanType{ false } };
_data = std::make_unique<TypedData>(TypedData{ BooleanType{ false } });
break;

case Type::Int:
*_data = { IntType{ 0 } };
_data = std::make_unique<TypedData>(TypedData{ IntType{ 0 } });
break;

case Type::Float:
*_data = { FloatType{ 0.0 } };
_data = std::make_unique<TypedData>(TypedData{ FloatType{ 0.0 } });
break;

default:
Expand Down Expand Up @@ -158,8 +157,12 @@ Value::Value(const Value& other)

Value& Value::operator=(Value&& rhs) noexcept
{
const_cast<Type&>(_type) = rhs._type;
_data = std::move(rhs._data);
if (&rhs != this)
{
const_cast<Type&>(_type) = rhs._type;
_data = std::move(rhs._data);
}

return *this;
}

Expand Down
10 changes: 10 additions & 0 deletions src/GraphQLService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,16 @@ std::future<response::Value> Object::resolve(const SelectionSetParams & selectio
}
}
}
catch (schema_exception & scx)
{
auto messages = scx.getErrors().release<response::ListType>();

errors.reserve(errors.size() + messages.size());
for (auto& error : messages)
{
errors.emplace_back(std::move(error));
}
}
catch (const std::exception & ex)
{
std::ostringstream message;
Expand Down

0 comments on commit ab32d4f

Please sign in to comment.