|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#include "StitchedSchema.h" |
| 5 | + |
| 6 | +#include "StarWarsData.h" |
| 7 | +#include "StarWarsSchema.h" |
| 8 | + |
| 9 | +import GraphQL.Today.Mock; |
| 10 | + |
| 11 | +namespace graphql::stitched { |
| 12 | + |
| 13 | +std::shared_ptr<schema::Schema> GetSchema() |
| 14 | +{ |
| 15 | + static std::weak_ptr<schema::Schema> s_wpSchema; |
| 16 | + auto schema = s_wpSchema.lock(); |
| 17 | + |
| 18 | + if (!schema) |
| 19 | + { |
| 20 | + auto learnSchema = learn::GetSchema(); |
| 21 | + auto todaySchema = today::GetSchema(); |
| 22 | + schema = learnSchema->StitchSchema(todaySchema); |
| 23 | + s_wpSchema = schema; |
| 24 | + } |
| 25 | + |
| 26 | + return schema; |
| 27 | +} |
| 28 | + |
| 29 | +class Operations final : public service::Request |
| 30 | +{ |
| 31 | +public: |
| 32 | + explicit Operations(std::shared_ptr<service::Object> query, |
| 33 | + std::shared_ptr<service::Object> mutation, std::shared_ptr<service::Object> subscription); |
| 34 | + |
| 35 | +private: |
| 36 | + std::shared_ptr<service::Object> _query; |
| 37 | + std::shared_ptr<service::Object> _mutation; |
| 38 | + std::shared_ptr<service::Object> _subscription; |
| 39 | +}; |
| 40 | + |
| 41 | +Operations::Operations(std::shared_ptr<service::Object> query, |
| 42 | + std::shared_ptr<service::Object> mutation, std::shared_ptr<service::Object> subscription) |
| 43 | + : service::Request( |
| 44 | + { |
| 45 | + { service::strQuery, query }, |
| 46 | + { service::strMutation, mutation }, |
| 47 | + { service::strSubscription, subscription }, |
| 48 | + }, |
| 49 | + GetSchema()) |
| 50 | + , _query(std::move(query)) |
| 51 | + , _mutation(std::move(mutation)) |
| 52 | + , _subscription(std::move(subscription)) |
| 53 | +{ |
| 54 | +} |
| 55 | + |
| 56 | +std::shared_ptr<service::Request> GetService() |
| 57 | +{ |
| 58 | + auto learnQuery = star_wars::GetQueryObject(); |
| 59 | + auto todayQuery = std::static_pointer_cast<service::Object>( |
| 60 | + std::make_shared<today::object::Query>(today::mock_query(today::mock_service()))); |
| 61 | + auto stitchedQuery = learnQuery->StitchObject(todayQuery); |
| 62 | + |
| 63 | + auto learnMutation = star_wars::GetMutationObject(); |
| 64 | + auto todayMutation = std::static_pointer_cast<service::Object>( |
| 65 | + std::make_shared<today::object::Mutation>(today::mock_mutation())); |
| 66 | + auto stitchedMutation = learnMutation->StitchObject(todayMutation); |
| 67 | + |
| 68 | + auto learnSubscription = star_wars::GetSubscriptionObject(); |
| 69 | + auto todaySubscription = std::static_pointer_cast<service::Object>( |
| 70 | + std::make_shared<today::object::Subscription>(today::mock_subscription())); |
| 71 | + std::shared_ptr<service::Object> stitchedSubscription; |
| 72 | + |
| 73 | + if (learnSubscription) |
| 74 | + { |
| 75 | + if (todaySubscription) |
| 76 | + { |
| 77 | + stitchedSubscription = learnSubscription->StitchObject(todaySubscription); |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + stitchedSubscription = learnSubscription; |
| 82 | + } |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + stitchedSubscription = todaySubscription; |
| 87 | + } |
| 88 | + |
| 89 | + return std::make_shared<Operations>(stitchedQuery, stitchedMutation, stitchedSubscription); |
| 90 | +} |
| 91 | + |
| 92 | +} // namespace graphql::stitched |
0 commit comments