Skip to content

Commit

Permalink
Move request parsing and validation to RequestLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
wravery committed May 20, 2021
1 parent f0dde15 commit a7c90f6
Show file tree
Hide file tree
Showing 5 changed files with 480 additions and 468 deletions.
20 changes: 3 additions & 17 deletions include/ClientGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
#ifndef CLIENTGENERATOR_H
#define CLIENTGENERATOR_H

#include "SchemaLoader.h"
#include "RequestLoader.h"

#include "graphqlservice/GraphQLSchema.h"

namespace graphql::generator::client {

struct GeneratorPaths
Expand All @@ -21,18 +18,15 @@ struct GeneratorPaths

struct GeneratorOptions
{
const std::string requestFilename;
const std::string operationName;
const std::optional<GeneratorPaths> paths;
const bool verbose = false;
const bool noIntrospection = false;
};

class Generator
{
public:
// Initialize the generator with the introspection client or a custom GraphQL client.
explicit Generator(SchemaOptions&& schemaOptions, GeneratorOptions&& options);
explicit Generator(SchemaOptions&& schemaOptions, RequestOptions&& requestOptions, GeneratorOptions&& options);

// Run the generator and return a list of filenames that were output.
std::vector<std::string> Build() const noexcept;
Expand All @@ -43,10 +37,6 @@ class Generator
std::string getHeaderPath() const noexcept;
std::string getSourcePath() const noexcept;

void validateRequest() const;
std::shared_ptr<schema::Schema> buildSchema() const;
void addTypesToSchema(const std::shared_ptr<schema::Schema>& schema) const;

bool outputHeader() const noexcept;
void outputObjectDeclaration(
std::ostream& headerFile, const ObjectType& objectType, bool isQueryType) const;
Expand All @@ -65,17 +55,13 @@ class Generator
std::string getResultAccessType(const OutputField& result) const noexcept;
std::string getTypeModifiers(const TypeModifierStack& modifiers) const noexcept;

static std::shared_ptr<const schema::BaseType> getIntrospectionType(
const std::shared_ptr<schema::Schema>& schema, std::string_view type,
const TypeModifierStack& modifiers) noexcept;

SchemaLoader _loader;
const SchemaLoader _schemaLoader;
const RequestLoader _requestLoader;
const GeneratorOptions _options;
const std::string _headerDir;
const std::string _sourceDir;
const std::string _headerPath;
const std::string _sourcePath;
peg::ast _request;
};

} /* namespace graphql::generator::client */
Expand Down
26 changes: 21 additions & 5 deletions include/RequestLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,36 @@
#ifndef REQUESTLOADER_H
#define REQUESTLOADER_H

#include "graphqlservice/GraphQLParse.h"
#include "graphqlservice/GraphQLService.h"
#include "SchemaLoader.h"

#include "graphqlservice/GraphQLSchema.h"

namespace graphql::generator {

struct RequestOptions
{
const std::string requestFilename;
const std::string operationName;
const bool noIntrospection = false;
};

class RequestLoader
{
public:
explicit RequestLoader();

void visit(const peg::ast& request, std::string_view operationName);
explicit RequestLoader(RequestOptions&& requestOptions, const SchemaLoader& schemaLoader);

private:
std::shared_ptr<schema::Schema> buildSchema(const SchemaLoader& schemaLoader) const;
void addTypesToSchema(const SchemaLoader& schemaLoader, const std::shared_ptr<schema::Schema>& schema) const;
void validateRequest() const;

static std::shared_ptr<const schema::BaseType> getIntrospectionType(
const std::shared_ptr<schema::Schema>& schema, std::string_view type,
const TypeModifierStack& modifiers) noexcept;

const RequestOptions _requestOptions;
peg::ast _ast;
std::shared_ptr<schema::Schema> _schema;
};

} /* namespace graphql::generator */
Expand Down
1 change: 0 additions & 1 deletion include/SchemaLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "graphqlservice/GraphQLService.h"

#include <array>
#include <cstdio>
#include <unordered_map>
#include <unordered_set>

Expand Down
Loading

0 comments on commit a7c90f6

Please sign in to comment.