|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#ifndef GRAPHQLSCHEMA_H |
| 7 | +#define GRAPHQLSCHEMA_H |
| 8 | + |
| 9 | +#include "graphqlservice/GraphQLService.h" |
| 10 | + |
| 11 | +namespace graphql { |
| 12 | +namespace introspection { |
| 13 | + |
| 14 | +enum class TypeKind; |
| 15 | +enum class DirectiveLocation; |
| 16 | + |
| 17 | +} // namespace introspection |
| 18 | + |
| 19 | +namespace schema { |
| 20 | + |
| 21 | +class Schema; |
| 22 | +class Directive; |
| 23 | +class BaseType; |
| 24 | +class ScalarType; |
| 25 | +class ObjectType; |
| 26 | +class InterfaceType; |
| 27 | +class UnionType; |
| 28 | +class EnumType; |
| 29 | +class InputObjectType; |
| 30 | +class WrapperType; |
| 31 | +class Field; |
| 32 | +class InputValue; |
| 33 | +class EnumValue; |
| 34 | + |
| 35 | +class Schema : public std::enable_shared_from_this<Schema> |
| 36 | +{ |
| 37 | +public: |
| 38 | + GRAPHQLSERVICE_EXPORT explicit Schema(); |
| 39 | + |
| 40 | + GRAPHQLSERVICE_EXPORT void AddQueryType(std::shared_ptr<ObjectType> query); |
| 41 | + GRAPHQLSERVICE_EXPORT void AddMutationType(std::shared_ptr<ObjectType> mutation); |
| 42 | + GRAPHQLSERVICE_EXPORT void AddSubscriptionType(std::shared_ptr<ObjectType> subscription); |
| 43 | + GRAPHQLSERVICE_EXPORT void AddType(std::string_view name, std::shared_ptr<BaseType> type); |
| 44 | + GRAPHQLSERVICE_EXPORT const std::shared_ptr<BaseType>& LookupType(std::string_view name) const; |
| 45 | + GRAPHQLSERVICE_EXPORT const std::shared_ptr<BaseType>& WrapType( |
| 46 | + introspection::TypeKind kind, const std::shared_ptr<BaseType>& ofType); |
| 47 | + GRAPHQLSERVICE_EXPORT void AddDirective(std::shared_ptr<Directive> directive); |
| 48 | + |
| 49 | + // Accessors |
| 50 | + const std::vector<std::pair<std::string_view, std::shared_ptr<BaseType>>>& types() |
| 51 | + const noexcept; |
| 52 | + const std::shared_ptr<ObjectType>& queryType() const noexcept; |
| 53 | + const std::shared_ptr<ObjectType>& mutationType() const noexcept; |
| 54 | + const std::shared_ptr<ObjectType>& subscriptionType() const noexcept; |
| 55 | + const std::vector<std::shared_ptr<Directive>>& directives() const noexcept; |
| 56 | + |
| 57 | +private: |
| 58 | + std::shared_ptr<ObjectType> _query; |
| 59 | + std::shared_ptr<ObjectType> _mutation; |
| 60 | + std::shared_ptr<ObjectType> _subscription; |
| 61 | + std::unordered_map<std::string_view, size_t> _typeMap; |
| 62 | + std::vector<std::pair<std::string_view, std::shared_ptr<BaseType>>> _types; |
| 63 | + std::vector<std::shared_ptr<Directive>> _directives; |
| 64 | + std::unordered_map<std::shared_ptr<BaseType>, std::shared_ptr<BaseType>> _nonNullWrappers; |
| 65 | + std::unordered_map<std::shared_ptr<BaseType>, std::shared_ptr<BaseType>> _listWrappers; |
| 66 | +}; |
| 67 | + |
| 68 | +class BaseType : public std::enable_shared_from_this<BaseType> |
| 69 | +{ |
| 70 | +public: |
| 71 | + // Accessors |
| 72 | + introspection::TypeKind kind() const noexcept; |
| 73 | + virtual std::string_view name() const noexcept; |
| 74 | + std::string_view description() const noexcept; |
| 75 | + virtual const std::vector<std::shared_ptr<Field>>& fields() const noexcept; |
| 76 | + virtual const std::vector<std::shared_ptr<InterfaceType>>& interfaces() const noexcept; |
| 77 | + virtual const std::vector<std::weak_ptr<BaseType>>& possibleTypes() const noexcept; |
| 78 | + virtual const std::vector<std::shared_ptr<EnumValue>>& enumValues() const noexcept; |
| 79 | + virtual const std::vector<std::shared_ptr<InputValue>>& inputFields() const noexcept; |
| 80 | + virtual const std::weak_ptr<BaseType>& ofType() const noexcept; |
| 81 | + |
| 82 | +protected: |
| 83 | + BaseType(introspection::TypeKind kind, std::string_view description); |
| 84 | + |
| 85 | +private: |
| 86 | + const introspection::TypeKind _kind; |
| 87 | + const std::string_view _description; |
| 88 | +}; |
| 89 | + |
| 90 | +class ScalarType : public BaseType |
| 91 | +{ |
| 92 | +public: |
| 93 | + GRAPHQLSERVICE_EXPORT explicit ScalarType(std::string_view name, std::string_view description); |
| 94 | + |
| 95 | + // Accessors |
| 96 | + std::string_view name() const noexcept final; |
| 97 | + |
| 98 | +private: |
| 99 | + const std::string_view _name; |
| 100 | +}; |
| 101 | + |
| 102 | +class ObjectType : public BaseType |
| 103 | +{ |
| 104 | +public: |
| 105 | + GRAPHQLSERVICE_EXPORT explicit ObjectType(std::string_view name, std::string_view description); |
| 106 | + |
| 107 | + GRAPHQLSERVICE_EXPORT void AddInterfaces( |
| 108 | + std::vector<std::shared_ptr<InterfaceType>> interfaces); |
| 109 | + GRAPHQLSERVICE_EXPORT void AddFields(std::vector<std::shared_ptr<Field>> fields); |
| 110 | + |
| 111 | + // Accessors |
| 112 | + std::string_view name() const noexcept final; |
| 113 | + const std::vector<std::shared_ptr<Field>>& fields() const noexcept final; |
| 114 | + const std::vector<std::shared_ptr<InterfaceType>>& interfaces() const noexcept final; |
| 115 | + |
| 116 | +private: |
| 117 | + const std::string_view _name; |
| 118 | + |
| 119 | + std::vector<std::shared_ptr<InterfaceType>> _interfaces; |
| 120 | + std::vector<std::shared_ptr<Field>> _fields; |
| 121 | +}; |
| 122 | + |
| 123 | +class InterfaceType : public BaseType |
| 124 | +{ |
| 125 | +public: |
| 126 | + GRAPHQLSERVICE_EXPORT explicit InterfaceType( |
| 127 | + std::string_view name, std::string_view description); |
| 128 | + |
| 129 | + GRAPHQLSERVICE_EXPORT void AddPossibleType(std::weak_ptr<ObjectType> possibleType); |
| 130 | + GRAPHQLSERVICE_EXPORT void AddFields(std::vector<std::shared_ptr<Field>> fields); |
| 131 | + |
| 132 | + // Accessors |
| 133 | + std::string_view name() const noexcept final; |
| 134 | + const std::vector<std::shared_ptr<Field>>& fields() const noexcept final; |
| 135 | + const std::vector<std::weak_ptr<BaseType>>& possibleTypes() const noexcept final; |
| 136 | + |
| 137 | +private: |
| 138 | + const std::string_view _name; |
| 139 | + |
| 140 | + std::vector<std::shared_ptr<Field>> _fields; |
| 141 | + std::vector<std::weak_ptr<BaseType>> _possibleTypes; |
| 142 | +}; |
| 143 | + |
| 144 | +class UnionType : public BaseType |
| 145 | +{ |
| 146 | +public: |
| 147 | + GRAPHQLSERVICE_EXPORT explicit UnionType(std::string_view name, std::string_view description); |
| 148 | + |
| 149 | + GRAPHQLSERVICE_EXPORT void AddPossibleTypes(std::vector<std::weak_ptr<BaseType>> possibleTypes); |
| 150 | + |
| 151 | + // Accessors |
| 152 | + std::string_view name() const noexcept final; |
| 153 | + const std::vector<std::weak_ptr<BaseType>>& possibleTypes() const noexcept final; |
| 154 | + |
| 155 | +private: |
| 156 | + const std::string_view _name; |
| 157 | + |
| 158 | + std::vector<std::weak_ptr<BaseType>> _possibleTypes; |
| 159 | +}; |
| 160 | + |
| 161 | +struct EnumValueType |
| 162 | +{ |
| 163 | + std::string_view value; |
| 164 | + std::string_view description; |
| 165 | + std::optional<std::string_view> deprecationReason; |
| 166 | +}; |
| 167 | + |
| 168 | +class EnumType : public BaseType |
| 169 | +{ |
| 170 | +public: |
| 171 | + GRAPHQLSERVICE_EXPORT explicit EnumType(std::string_view name, std::string_view description); |
| 172 | + |
| 173 | + GRAPHQLSERVICE_EXPORT void AddEnumValues(std::vector<EnumValueType> enumValues); |
| 174 | + |
| 175 | + // Accessors |
| 176 | + std::string_view name() const noexcept final; |
| 177 | + const std::vector<std::shared_ptr<EnumValue>>& enumValues() const noexcept final; |
| 178 | + |
| 179 | +private: |
| 180 | + const std::string_view _name; |
| 181 | + |
| 182 | + std::vector<std::shared_ptr<EnumValue>> _enumValues; |
| 183 | +}; |
| 184 | + |
| 185 | +class InputObjectType : public BaseType |
| 186 | +{ |
| 187 | +public: |
| 188 | + GRAPHQLSERVICE_EXPORT explicit InputObjectType( |
| 189 | + std::string_view name, std::string_view description); |
| 190 | + |
| 191 | + GRAPHQLSERVICE_EXPORT void AddInputValues(std::vector<std::shared_ptr<InputValue>> inputValues); |
| 192 | + |
| 193 | + // Accessors |
| 194 | + std::string_view name() const noexcept final; |
| 195 | + const std::vector<std::shared_ptr<InputValue>>& inputFields() const noexcept final; |
| 196 | + |
| 197 | +private: |
| 198 | + const std::string_view _name; |
| 199 | + |
| 200 | + std::vector<std::shared_ptr<InputValue>> _inputValues; |
| 201 | +}; |
| 202 | + |
| 203 | +class WrapperType : public BaseType |
| 204 | +{ |
| 205 | +public: |
| 206 | + GRAPHQLSERVICE_EXPORT explicit WrapperType( |
| 207 | + introspection::TypeKind kind, const std::shared_ptr<BaseType>& ofType); |
| 208 | + |
| 209 | + // Accessors |
| 210 | + const std::weak_ptr<BaseType>& ofType() const noexcept final; |
| 211 | + |
| 212 | +private: |
| 213 | + const std::weak_ptr<BaseType> _ofType; |
| 214 | +}; |
| 215 | + |
| 216 | +class Field : public std::enable_shared_from_this<Field> |
| 217 | +{ |
| 218 | +public: |
| 219 | + GRAPHQLSERVICE_EXPORT explicit Field(std::string_view name, std::string_view description, |
| 220 | + std::optional<std::string_view> deprecationReason, |
| 221 | + std::vector<std::shared_ptr<InputValue>>&& args, const std::shared_ptr<BaseType>& type); |
| 222 | + |
| 223 | + // Accessors |
| 224 | + std::string_view name() const noexcept; |
| 225 | + std::string_view description() const noexcept; |
| 226 | + const std::vector<std::shared_ptr<InputValue>>& args() const noexcept; |
| 227 | + const std::weak_ptr<BaseType>& type() const noexcept; |
| 228 | + const std::optional<std::string_view>& deprecationReason() const noexcept; |
| 229 | + |
| 230 | +private: |
| 231 | + const std::string_view _name; |
| 232 | + const std::string_view _description; |
| 233 | + const std::optional<std::string_view> _deprecationReason; |
| 234 | + const std::vector<std::shared_ptr<InputValue>> _args; |
| 235 | + const std::weak_ptr<BaseType> _type; |
| 236 | +}; |
| 237 | + |
| 238 | +class InputValue : public std::enable_shared_from_this<InputValue> |
| 239 | +{ |
| 240 | +public: |
| 241 | + GRAPHQLSERVICE_EXPORT explicit InputValue(std::string_view name, std::string_view description, |
| 242 | + const std::shared_ptr<BaseType>& type, std::string_view defaultValue); |
| 243 | + |
| 244 | + // Accessors |
| 245 | + std::string_view name() const noexcept; |
| 246 | + std::string_view description() const noexcept; |
| 247 | + const std::weak_ptr<BaseType>& type() const noexcept; |
| 248 | + std::string_view defaultValue() const noexcept; |
| 249 | + |
| 250 | +private: |
| 251 | + const std::string_view _name; |
| 252 | + const std::string_view _description; |
| 253 | + const std::weak_ptr<BaseType> _type; |
| 254 | + const std::string_view _defaultValue; |
| 255 | +}; |
| 256 | + |
| 257 | +class EnumValue : public std::enable_shared_from_this<EnumValue> |
| 258 | +{ |
| 259 | +public: |
| 260 | + GRAPHQLSERVICE_EXPORT explicit EnumValue(std::string_view name, std::string_view description, |
| 261 | + std::optional<std::string_view> deprecationReason); |
| 262 | + |
| 263 | + // Accessors |
| 264 | + std::string_view name() const noexcept; |
| 265 | + std::string_view description() const noexcept; |
| 266 | + const std::optional<std::string_view>& deprecationReason() const noexcept; |
| 267 | + |
| 268 | +private: |
| 269 | + const std::string_view _name; |
| 270 | + const std::string_view _description; |
| 271 | + const std::optional<std::string_view> _deprecationReason; |
| 272 | +}; |
| 273 | + |
| 274 | +class Directive : public std::enable_shared_from_this<Directive> |
| 275 | +{ |
| 276 | +public: |
| 277 | + GRAPHQLSERVICE_EXPORT explicit Directive(std::string_view name, std::string_view description, |
| 278 | + std::vector<introspection::DirectiveLocation>&& locations, |
| 279 | + std::vector<std::shared_ptr<InputValue>>&& args); |
| 280 | + |
| 281 | + // Accessors |
| 282 | + std::string_view name() const noexcept; |
| 283 | + std::string_view description() const noexcept; |
| 284 | + const std::vector<introspection::DirectiveLocation>& locations() const noexcept; |
| 285 | + const std::vector<std::shared_ptr<InputValue>>& args() const noexcept; |
| 286 | + |
| 287 | +private: |
| 288 | + const std::string_view _name; |
| 289 | + const std::string_view _description; |
| 290 | + const std::vector<introspection::DirectiveLocation> _locations; |
| 291 | + const std::vector<std::shared_ptr<InputValue>> _args; |
| 292 | +}; |
| 293 | + |
| 294 | +} // namespace schema |
| 295 | +} // namespace graphql |
| 296 | + |
| 297 | +#endif // GRAPHQLSCHEMA_H |
0 commit comments