Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use stitched schema for Introspection on root Query #331

Merged
merged 2 commits into from
Oct 23, 2024
Merged
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
3 changes: 2 additions & 1 deletion include/graphqlservice/GraphQLService.h
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,8 @@ class [[nodiscard("unnecessary construction")]] Object : public std::enable_shar
GRAPHQLSERVICE_EXPORT virtual ~Object() = default;

[[nodiscard("unnecessary call")]] GRAPHQLSERVICE_EXPORT std::shared_ptr<Object> StitchObject(
const std::shared_ptr<const Object>& added) const;
const std::shared_ptr<const Object>& added,
const std::shared_ptr<schema::Schema>& schema = {}) const;

[[nodiscard("unnecessary call")]] GRAPHQLSERVICE_EXPORT AwaitableResolver resolve(
const SelectionSetParams& selectionSetParams, const peg::ast_node& selection,
Expand Down
98 changes: 75 additions & 23 deletions src/GraphQLService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include "graphqlservice/GraphQLService.h"

#include "graphqlservice/internal/Grammar.h"
#include "graphqlservice/internal/Introspection.h"

#include "graphqlservice/introspection/SchemaObject.h"
#include "graphqlservice/introspection/TypeObject.h"

#include "Validation.h"

Expand Down Expand Up @@ -267,17 +271,17 @@ void await_worker_queue::resumePending()
// Default to immediate synchronous execution.
await_async::await_async()
: _pimpl { std::static_pointer_cast<const Concept>(
std::make_shared<Model<std::suspend_never>>(std::make_shared<std::suspend_never>())) }
std::make_shared<Model<std::suspend_never>>(std::make_shared<std::suspend_never>())) }
{
}

// Implicitly convert a std::launch parameter used with std::async to an awaitable.
await_async::await_async(std::launch launch)
: _pimpl { ((launch & std::launch::async) == std::launch::async)
? std::static_pointer_cast<const Concept>(std::make_shared<Model<await_worker_thread>>(
std::make_shared<await_worker_thread>()))
std::make_shared<await_worker_thread>()))
: std::static_pointer_cast<const Concept>(std::make_shared<Model<std::suspend_never>>(
std::make_shared<std::suspend_never>())) }
std::make_shared<std::suspend_never>())) }
{
}

Expand Down Expand Up @@ -1253,21 +1257,53 @@ Object::Object(TypeNames&& typeNames, ResolverMap&& resolvers) noexcept
{
}

std::shared_ptr<Object> Object::StitchObject(const std::shared_ptr<const Object>& added) const
std::shared_ptr<Object> Object::StitchObject(const std::shared_ptr<const Object>& added,
const std::shared_ptr<schema::Schema>& schema /* = {} */) const
{
auto typeNames = _typeNames;
auto resolvers = _resolvers;

for (const auto& name : added->_typeNames)
if (schema && schema->supportsIntrospection())
{
typeNames.emplace(name);
constexpr auto schemaField = R"gql(__schema)gql"sv;
constexpr auto typeField = R"gql(__type)gql"sv;

resolvers.erase(schemaField);
resolvers.emplace(schemaField, [schema](ResolverParams&& params) {
return Result<Object>::convert(
std::static_pointer_cast<Object>(std::make_shared<introspection::object::Schema>(
std::make_shared<introspection::Schema>(schema))),
std::move(params));
});

resolvers.erase(typeField);
resolvers.emplace(typeField, [schema](ResolverParams&& params) {
auto argName = ModifiedArgument<std::string>::require("name", params.arguments);
const auto& baseType = schema->LookupType(argName);
std::shared_ptr<introspection::object::Type> result { baseType
? std::make_shared<introspection::object::Type>(
std::make_shared<introspection::Type>(baseType))
: nullptr };

return ModifiedResult<introspection::object::Type>::convert<TypeModifier::Nullable>(
result,
std::move(params));
});
}

auto resolvers = _resolvers;
bool hasStitchedResolvers = false;

for (const auto& [name, resolver] : added->_resolvers)
if (added)
{
hasStitchedResolvers = resolvers.emplace(name, resolver).second || hasStitchedResolvers;
for (const auto& name : added->_typeNames)
{
typeNames.emplace(name);
}

for (const auto& [name, resolver] : added->_resolvers)
{
hasStitchedResolvers = resolvers.emplace(name, resolver).second || hasStitchedResolvers;
}
}

auto object = std::make_shared<Object>(std::move(typeNames), std::move(resolvers));
Expand Down Expand Up @@ -1779,64 +1815,81 @@ Request::~Request()
std::shared_ptr<const Request> Request::stitch(const std::shared_ptr<const Request>& added) const
{
TypeMap operations;
auto schema = _schema->StitchSchema(added->_schema);
std::shared_ptr<const Object> query;
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));
query = itrOriginalQuery->second->StitchObject(itrAddedQuery->second, schema);
}
else
{
operations.emplace(strQuery, itrOriginalQuery->second);
query = itrOriginalQuery->second->StitchObject({}, schema);
}
}
else if (itrAddedQuery != added->_operations.end() && itrAddedQuery->second)
{
operations.emplace(strQuery, itrAddedQuery->second);
query = itrAddedQuery->second->StitchObject({}, schema);
}

if (query)
{
operations.emplace(strQuery, std::move(query));
}

std::shared_ptr<const Object> mutation;
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));
mutation = itrOriginalMutation->second->StitchObject(itrAddedMutation->second);
}
else
{
operations.emplace(strMutation, itrOriginalMutation->second);
mutation = itrOriginalMutation->second;
}
}
else if (itrAddedMutation != added->_operations.end() && itrAddedMutation->second)
{
operations.emplace(strMutation, itrAddedMutation->second);
mutation = itrAddedMutation->second;
}

if (mutation)
{
operations.emplace(strMutation, std::move(mutation));
}

std::shared_ptr<const Object> subscription;
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));
subscription =
itrOriginalSubscription->second->StitchObject(itrAddedSubscription->second);
}
else
{
operations.emplace(strSubscription, itrOriginalSubscription->second);
subscription = itrOriginalSubscription->second;
}
}
else if (itrAddedSubscription != added->_operations.end() && itrAddedSubscription->second)
{
operations.emplace(strSubscription, itrAddedSubscription->second);
subscription = itrAddedSubscription->second;
}

if (subscription)
{
operations.emplace(strSubscription, std::move(subscription));
}

class StitchedRequest : public Request
Expand All @@ -1848,8 +1901,7 @@ std::shared_ptr<const Request> Request::stitch(const std::shared_ptr<const Reque
}
};

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

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