Skip to content

Commit

Permalink
feat: generalize stitched service in Request::stitch
Browse files Browse the repository at this point in the history
  • Loading branch information
wravery committed Oct 21, 2024
1 parent 6d55067 commit 8b0102c
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 105 deletions.
3 changes: 3 additions & 0 deletions include/graphqlservice/GraphQLService.h
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,9 @@ class [[nodiscard("unnecessary construction")]] Request
GRAPHQLSERVICE_EXPORT virtual ~Request();

public:
[[nodiscard("unnecessary call")]] GRAPHQLSERVICE_EXPORT std::shared_ptr<const Request> stitch(
const std::shared_ptr<const Request>& added) const;

[[nodiscard("unnecessary call")]] GRAPHQLSERVICE_EXPORT std::list<schema_error> validate(
peg::ast& query) const;

Expand Down
15 changes: 0 additions & 15 deletions samples/learn/StarWarsData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,11 @@ std::shared_ptr<learn::object::Query> MakeQuery() noexcept
std::make_shared<learn::Query>(std::move(heroes), std::move(humans), std::move(droids)));
}

std::shared_ptr<service::Object> GetQueryObject() noexcept
{
return MakeQuery();
}

std::shared_ptr<learn::object::Mutation> MakeMutation() noexcept
{
return std::make_shared<learn::object::Mutation>(std::make_shared<learn::Mutation>());
}

std::shared_ptr<service::Object> GetMutationObject() noexcept
{
return MakeMutation();
}

std::shared_ptr<service::Object> GetSubscriptionObject() noexcept
{
return {};
}

std::shared_ptr<service::Request> GetService() noexcept
{
return std::make_shared<learn::Operations>(MakeQuery(),
Expand Down
4 changes: 0 additions & 4 deletions samples/learn/StarWarsData.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@

namespace graphql::star_wars {

std::shared_ptr<service::Object> GetQueryObject() noexcept;
std::shared_ptr<service::Object> GetMutationObject() noexcept;
std::shared_ptr<service::Object> GetSubscriptionObject() noexcept;

std::shared_ptr<service::Request> GetService() noexcept;

} // namespace graphql::star_wars
Expand Down
78 changes: 2 additions & 76 deletions samples/stitched/StitchedSchema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,83 +10,9 @@ import GraphQL.Today.Mock;

namespace graphql::stitched {

std::shared_ptr<schema::Schema> GetSchema()
std::shared_ptr<const service::Request> GetService()
{
static std::weak_ptr<schema::Schema> s_wpSchema;
auto schema = s_wpSchema.lock();

if (!schema)
{
auto learnSchema = learn::GetSchema();
auto todaySchema = today::GetSchema();
schema = learnSchema->StitchSchema(todaySchema);
s_wpSchema = schema;
}

return schema;
}

class Operations final : public service::Request
{
public:
explicit Operations(std::shared_ptr<service::Object> query,
std::shared_ptr<service::Object> mutation, std::shared_ptr<service::Object> subscription);

private:
std::shared_ptr<service::Object> _query;
std::shared_ptr<service::Object> _mutation;
std::shared_ptr<service::Object> _subscription;
};

Operations::Operations(std::shared_ptr<service::Object> query,
std::shared_ptr<service::Object> mutation, std::shared_ptr<service::Object> subscription)
: service::Request(
{
{ service::strQuery, query },
{ service::strMutation, mutation },
{ service::strSubscription, subscription },
},
GetSchema())
, _query(std::move(query))
, _mutation(std::move(mutation))
, _subscription(std::move(subscription))
{
}

std::shared_ptr<service::Request> GetService()
{
auto learnQuery = star_wars::GetQueryObject();
auto todayQuery = std::static_pointer_cast<service::Object>(
std::make_shared<today::object::Query>(today::mock_query(today::mock_service())));
auto stitchedQuery = learnQuery->StitchObject(todayQuery);

auto learnMutation = star_wars::GetMutationObject();
auto todayMutation = std::static_pointer_cast<service::Object>(
std::make_shared<today::object::Mutation>(today::mock_mutation()));
auto stitchedMutation = learnMutation->StitchObject(todayMutation);

auto learnSubscription = star_wars::GetSubscriptionObject();
auto todaySubscription = std::static_pointer_cast<service::Object>(
std::make_shared<today::object::Subscription>(today::mock_subscription()));
std::shared_ptr<service::Object> stitchedSubscription;

if (learnSubscription)
{
if (todaySubscription)
{
stitchedSubscription = learnSubscription->StitchObject(todaySubscription);
}
else
{
stitchedSubscription = learnSubscription;
}
}
else
{
stitchedSubscription = todaySubscription;
}

return std::make_shared<Operations>(stitchedQuery, stitchedMutation, stitchedSubscription);
return star_wars::GetService()->stitch(today::mock_service()->service);
}

} // namespace graphql::stitched
2 changes: 1 addition & 1 deletion samples/stitched/StitchedSchema.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace graphql::stitched {

std::shared_ptr<service::Request> GetService();
std::shared_ptr<const service::Request> GetService();

} // namespace graphql::stitched

Expand Down
6 changes: 0 additions & 6 deletions samples/today/TodayMock.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ class Query : public std::enable_shared_from_this<Query>
std::vector<std::shared_ptr<Folder>> _unreadCounts;
};

std::shared_ptr<Query> mock_query(const std::shared_ptr<TodayMockService>& service) noexcept;

class PageInfo
{
public:
Expand Down Expand Up @@ -303,8 +301,6 @@ class Mutation
static std::optional<double> _setFloat;
};

std::shared_ptr<Mutation> mock_mutation() noexcept;

class Subscription
{
public:
Expand Down Expand Up @@ -336,8 +332,6 @@ class NextAppointmentChange
static std::size_t _notifyUnsubscribeCount;
};

std::shared_ptr<NextAppointmentChange> mock_subscription() noexcept;

class NodeChange
{
public:
Expand Down
3 changes: 0 additions & 3 deletions samples/today/TodayMock.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ using today::mock_service;
using today::RequestState;

using today::Query;
using today::mock_query;

using today::PageInfo;
using today::Appointment;
Expand All @@ -35,10 +34,8 @@ using today::FolderEdge;
using today::FolderConnection;
using today::CompleteTaskPayload;
using today::Mutation;
using today::mock_mutation;
using today::Subscription;
using today::NextAppointmentChange;
using today::mock_subscription;
using today::NodeChange;
using today::CapturedParams;
using today::NestedType;
Expand Down
77 changes: 77 additions & 0 deletions src/GraphQLService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1735,6 +1735,7 @@ void SubscriptionDefinitionVisitor::visitInlineFragment(const peg::ast_node& inl

Request::Request(TypeMap operationTypes, std::shared_ptr<schema::Schema> schema)
: _operations(std::move(operationTypes))
, _schema(schema)
, _validation(std::make_unique<ValidateExecutableVisitor>(std::move(schema)))
{
}
Expand All @@ -1746,6 +1747,82 @@ Request::~Request()
// forward declaration of the class.
}

std::shared_ptr<const Request> Request::stitch(const std::shared_ptr<const Request>& added) const
{
TypeMap operations;
auto itrOriginalQuery = _operations.find(strQuery);
auto itrAddedQuery = added->_operations.find(strQuery);

if (itrOriginalQuery != _operations.end() && itrOriginalQuery->second)
{
if (itrAddedQuery != added->_operations.end() && itrAddedQuery->second)
{
operations.emplace(strQuery,
itrOriginalQuery->second->StitchObject(itrAddedQuery->second));
}
else
{
operations.emplace(strQuery, itrOriginalQuery->second);
}
}
else if (itrAddedQuery != added->_operations.end() && itrAddedQuery->second)
{
operations.emplace(strQuery, itrAddedQuery->second);
}

auto itrOriginalMutation = _operations.find(strMutation);
auto itrAddedMutation = added->_operations.find(strMutation);

if (itrOriginalMutation != _operations.end() && itrOriginalMutation->second)
{
if (itrAddedMutation != added->_operations.end() && itrAddedMutation->second)
{
operations.emplace(strMutation,
itrOriginalMutation->second->StitchObject(itrAddedMutation->second));
}
else
{
operations.emplace(strMutation, itrOriginalMutation->second);
}
}
else if (itrAddedMutation != added->_operations.end() && itrAddedMutation->second)
{
operations.emplace(strMutation, itrAddedMutation->second);
}

auto itrOriginalSubscription = _operations.find(strSubscription);
auto itrAddedSubscription = added->_operations.find(strSubscription);

if (itrOriginalSubscription != _operations.end() && itrOriginalSubscription->second)
{
if (itrAddedSubscription != added->_operations.end() && itrAddedSubscription->second)
{
operations.emplace(strSubscription,
itrOriginalSubscription->second->StitchObject(itrAddedSubscription->second));
}
else
{
operations.emplace(strSubscription, itrOriginalSubscription->second);
}
}
else if (itrAddedSubscription != added->_operations.end() && itrAddedSubscription->second)
{
operations.emplace(strSubscription, itrAddedSubscription->second);
}

class StitchedRequest : public Request
{
public:
StitchedRequest(TypeMap operations, std::shared_ptr<schema::Schema> schema)
: Request { std::move(operations), std::move(schema) }
{
}
};

return std::make_shared<StitchedRequest>(std::move(operations),
_schema->StitchSchema(added->_schema));
}

std::list<schema_error> Request::validate(peg::ast& query) const
{
std::list<schema_error> errors;
Expand Down

0 comments on commit 8b0102c

Please sign in to comment.