Skip to content

Commit 529abc4

Browse files
committed
Validate requests against the schema
1 parent a98690b commit 529abc4

File tree

6 files changed

+622
-1283
lines changed

6 files changed

+622
-1283
lines changed

include/ClientGenerator.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
#include "graphqlservice/GraphQLGrammar.h"
1010
#include "graphqlservice/GraphQLParse.h"
11-
#include "graphqlservice/GraphQLService.h"
11+
#include "graphqlservice/GraphQLResponse.h"
12+
#include "graphqlservice/GraphQLSchema.h"
1213

1314
#include <array>
1415
#include <cstdio>
@@ -222,7 +223,8 @@ using OperationTypeList = std::vector<OperationType>;
222223

223224
struct GeneratorClient
224225
{
225-
const std::string clientFilename;
226+
const std::string schemaFilename;
227+
const std::string requestFilename;
226228
const std::string filenamePrefix;
227229
const std::string clientNamespace;
228230
};
@@ -235,11 +237,9 @@ struct GeneratorPaths
235237

236238
struct GeneratorOptions
237239
{
238-
const std::optional<GeneratorClient> customClient;
240+
const GeneratorClient client;
239241
const std::optional<GeneratorPaths> paths;
240242
const bool verbose = false;
241-
const bool separateFiles = false;
242-
const bool noStubs = false;
243243
const bool noIntrospection = false;
244244
};
245245

@@ -256,7 +256,6 @@ class Generator
256256
std::string getHeaderDir() const noexcept;
257257
std::string getSourceDir() const noexcept;
258258
std::string getHeaderPath() const noexcept;
259-
std::string getObjectHeaderPath() const noexcept;
260259
std::string getSourcePath() const noexcept;
261260

262261
void visitDefinition(const peg::ast_node& definition);
@@ -321,12 +320,16 @@ class Generator
321320
response::Value _value;
322321
};
323322

324-
void validateClient();
323+
void validateSchema();
325324
void fixupOutputFieldList(OutputFieldList& fields,
326325
const std::optional<std::unordered_set<std::string_view>>& interfaceFields,
327326
const std::optional<std::string_view>& accessor);
328327
void fixupInputFieldList(InputFieldList& fields);
329328

329+
void validateQuery() const;
330+
std::shared_ptr<schema::Schema> buildSchema() const;
331+
void addTypesToSchema(const std::shared_ptr<schema::Schema>& schema) const;
332+
330333
std::string_view getCppType(std::string_view type) const noexcept;
331334
std::string getInputCppType(const InputField& field) const noexcept;
332335
std::string getOutputCppType(const OutputField& field) const noexcept;
@@ -341,34 +344,31 @@ class Generator
341344
bool outputSource() const noexcept;
342345
void outputObjectImplementation(
343346
std::ostream& sourceFile, const ObjectType& objectType, bool isQueryType) const;
344-
void outputObjectIntrospection(std::ostream& sourceFile, const ObjectType& objectType) const;
345347
std::string getArgumentDefaultValue(
346348
size_t level, const response::Value& defaultValue) const noexcept;
347349
std::string getArgumentDeclaration(const InputField& argument, const char* prefixToken,
348350
const char* argumentsToken, const char* defaultToken) const noexcept;
349351
std::string getArgumentAccessType(const InputField& argument) const noexcept;
350352
std::string getResultAccessType(const OutputField& result) const noexcept;
351353
std::string getTypeModifiers(const TypeModifierStack& modifiers) const noexcept;
352-
std::string getIntrospectionType(
353-
std::string_view type, const TypeModifierStack& modifiers) const noexcept;
354354

355-
std::vector<std::string> outputSeparateFiles() const noexcept;
355+
static std::shared_ptr<const schema::BaseType> getIntrospectionType(
356+
const std::shared_ptr<schema::Schema>& schema, std::string_view type,
357+
TypeModifierStack modifiers, bool nonNull = true) noexcept;
356358

357-
static const std::string_view s_introspectionNamespace;
358359
static const BuiltinTypeMap s_builtinTypes;
359360
static const CppTypeMap s_builtinCppTypes;
360361
static const std::string_view s_scalarCppType;
361362
static const std::string s_currentDirectory;
362363

363364
const GeneratorOptions _options;
364-
const bool _isIntrospection;
365365
std::string_view _clientNamespace;
366366
const std::string _headerDir;
367367
const std::string _sourceDir;
368368
const std::string _headerPath;
369-
const std::string _objectHeaderPath;
370369
const std::string _sourcePath;
371-
peg::ast _ast;
370+
peg::ast _schema;
371+
peg::ast _request;
372372

373373
SchemaTypeMap _clientTypes;
374374
PositionMap _typePositions;

include/graphqlservice/GraphQLSchema.h

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include "graphqlservice/GraphQLService.h"
1010

11-
#include <initializer_list>
1211
#include <shared_mutex>
1312

1413
namespace graphql {
@@ -142,8 +141,8 @@ class ObjectType : public BaseType
142141
std::string_view name, std::string_view description);
143142

144143
GRAPHQLSERVICE_EXPORT void AddInterfaces(
145-
std::initializer_list<std::shared_ptr<const InterfaceType>> interfaces);
146-
GRAPHQLSERVICE_EXPORT void AddFields(std::initializer_list<std::shared_ptr<Field>> fields);
144+
std::vector<std::shared_ptr<const InterfaceType>>&& interfaces);
145+
GRAPHQLSERVICE_EXPORT void AddFields(std::vector<std::shared_ptr<const Field>>&& fields);
147146

148147
// Accessors
149148
GRAPHQLSERVICE_EXPORT std::string_view name() const noexcept final;
@@ -173,7 +172,7 @@ class InterfaceType : public BaseType
173172
std::string_view name, std::string_view description);
174173

175174
GRAPHQLSERVICE_EXPORT void AddPossibleType(std::weak_ptr<ObjectType> possibleType);
176-
GRAPHQLSERVICE_EXPORT void AddFields(std::initializer_list<std::shared_ptr<Field>> fields);
175+
GRAPHQLSERVICE_EXPORT void AddFields(std::vector<std::shared_ptr<const Field>>&& fields);
177176

178177
// Accessors
179178
GRAPHQLSERVICE_EXPORT std::string_view name() const noexcept final;
@@ -203,7 +202,7 @@ class UnionType : public BaseType
203202
std::string_view name, std::string_view description);
204203

205204
GRAPHQLSERVICE_EXPORT void AddPossibleTypes(
206-
std::initializer_list<std::weak_ptr<const BaseType>> possibleTypes);
205+
std::vector<std::weak_ptr<const BaseType>>&& possibleTypes);
207206

208207
// Accessors
209208
GRAPHQLSERVICE_EXPORT std::string_view name() const noexcept final;
@@ -236,7 +235,7 @@ class EnumType : public BaseType
236235
GRAPHQLSERVICE_EXPORT static std::shared_ptr<EnumType> Make(
237236
std::string_view name, std::string_view description);
238237

239-
GRAPHQLSERVICE_EXPORT void AddEnumValues(std::initializer_list<EnumValueType> enumValues);
238+
GRAPHQLSERVICE_EXPORT void AddEnumValues(std::vector<EnumValueType>&& enumValues);
240239

241240
// Accessors
242241
GRAPHQLSERVICE_EXPORT std::string_view name() const noexcept final;
@@ -263,7 +262,7 @@ class InputObjectType : public BaseType
263262
std::string_view name, std::string_view description);
264263

265264
GRAPHQLSERVICE_EXPORT void AddInputValues(
266-
std::initializer_list<std::shared_ptr<InputValue>> inputValues);
265+
std::vector<std::shared_ptr<const InputValue>>&& inputValues);
267266

268267
// Accessors
269268
GRAPHQLSERVICE_EXPORT std::string_view name() const noexcept final;
@@ -308,8 +307,7 @@ class Field : public std::enable_shared_from_this<Field>
308307

309308
GRAPHQLSERVICE_EXPORT static std::shared_ptr<Field> Make(std::string_view name,
310309
std::string_view description, std::optional<std::string_view> deprecationReason,
311-
std::weak_ptr<const BaseType> type,
312-
std::initializer_list<std::shared_ptr<InputValue>> args = {});
310+
std::weak_ptr<const BaseType> type, std::vector<std::shared_ptr<const InputValue>>&& args = {});
313311

314312
// Accessors
315313
GRAPHQLSERVICE_EXPORT std::string_view name() const noexcept;
@@ -389,9 +387,8 @@ class Directive : public std::enable_shared_from_this<Directive>
389387
explicit Directive(init&& params);
390388

391389
GRAPHQLSERVICE_EXPORT static std::shared_ptr<Directive> Make(std::string_view name,
392-
std::string_view description,
393-
std::initializer_list<introspection::DirectiveLocation> locations,
394-
std::initializer_list<std::shared_ptr<InputValue>> args = {});
390+
std::string_view description, std::vector<introspection::DirectiveLocation>&& locations,
391+
std::vector<std::shared_ptr<const InputValue>>&& args = {});
395392

396393
// Accessors
397394
GRAPHQLSERVICE_EXPORT std::string_view name() const noexcept;

src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ if(WIN32 AND BUILD_SHARED_LIBS)
327327
add_version_rc(graphqlintrospection)
328328
endif()
329329

330+
if(GRAPHQL_BUILD_CLIENTGEN)
331+
target_link_libraries(clientgen PRIVATE graphqlintrospection)
332+
endif()
333+
330334
# RapidJSON is the only option for JSON serialization used in this project, but if you want
331335
# to use another JSON library you can implement an alternate version of the functions in
332336
# JSONResponse.cpp to serialize to and from GraphQLResponse and build graphqljson from that.

0 commit comments

Comments
 (0)