bool Generator::outputHeader()

in src/ClientGenerator.cpp [170:318]


bool Generator::outputHeader() const noexcept
{
	std::ofstream headerFile(_headerPath, std::ios_base::trunc);
	IncludeGuardScope includeGuard { headerFile,
		std::filesystem::path(_headerPath).filename().string() };

	headerFile << R"cpp(#include "graphqlservice/GraphQLClient.h"
#include "graphqlservice/GraphQLParse.h"
#include "graphqlservice/GraphQLResponse.h"

#include "graphqlservice/internal/Version.h"

// Check if the library version is compatible with clientgen )cpp"
			   << graphql::internal::MajorVersion << R"cpp(.)cpp" << graphql::internal::MinorVersion
			   << R"cpp(.0
static_assert(graphql::internal::MajorVersion == )cpp"
			   << graphql::internal::MajorVersion
			   << R"cpp(, "regenerate with clientgen: major version mismatch");
static_assert(graphql::internal::MinorVersion == )cpp"
			   << graphql::internal::MinorVersion
			   << R"cpp(, "regenerate with clientgen: minor version mismatch");

#include <optional>
#include <string>
#include <vector>
)cpp";

	outputRequestComment(headerFile);

	NamespaceScope fullNamespaceScope { headerFile, getFullNamespace() };
	PendingBlankLine pendingSeparator { headerFile };

	outputGetRequestDeclaration(headerFile);

	// Define all of the enums referenced either in variables or the response.
	for (const auto& enumType : _requestLoader.getReferencedEnums())
	{
		pendingSeparator.reset();

		headerFile << R"cpp(enum class )cpp" << _schemaLoader.getCppType(enumType->name())
				   << R"cpp(
{
)cpp";
		for (const auto& enumValue : enumType->enumValues())
		{
			headerFile << R"cpp(	)cpp" << SchemaLoader::getSafeCppName(enumValue->name())
					   << R"cpp(,
)cpp";
		}

		headerFile << R"cpp(};
)cpp";

		pendingSeparator.add();
	}

	pendingSeparator.reset();

	const auto& variables = _requestLoader.getVariables();

	if (!variables.empty())
	{
		headerFile << R"cpp(struct Variables
{
)cpp";

		// Define all of the input object structs referenced in variables.
		for (const auto& inputType : _requestLoader.getReferencedInputTypes())
		{
			pendingSeparator.reset();

			headerFile << R"cpp(	struct )cpp" << _schemaLoader.getCppType(inputType->name())
					   << R"cpp(
	{
)cpp";

			for (const auto& inputField : inputType->inputFields())
			{

				headerFile << R"cpp(		)cpp"
						   << _requestLoader.getInputCppType(inputField->type().lock())
						   << R"cpp( )cpp" << SchemaLoader::getSafeCppName(inputField->name())
						   << R"cpp( {};
)cpp";
			}

			headerFile << R"cpp(	};
)cpp";

			pendingSeparator.add();
		}

		pendingSeparator.reset();

		for (const auto& variable : variables)
		{
			headerFile << R"cpp(	)cpp" << _schemaLoader.getCppType(variable.type->name())
					   << R"cpp( )cpp" << variable.cppName << R"cpp( {};
)cpp";
		}

		headerFile << R"cpp(};

response::Value serializeVariables(Variables&& variables);
)cpp";

		pendingSeparator.add();
	}

	pendingSeparator.reset();

	const auto& responseType = _requestLoader.getResponseType();

	headerFile << R"cpp(struct Response
{)cpp";

	pendingSeparator.add();

	for (const auto& responseField : responseType.fields)
	{
		pendingSeparator.reset();

		if (outputResponseFieldType(headerFile, responseField, 1))
		{
			pendingSeparator.add();
		}
	}

	for (const auto& responseField : responseType.fields)
	{
		pendingSeparator.reset();

		headerFile << R"cpp(	)cpp"
				   << RequestLoader::getOutputCppType(getResponseFieldCppType(responseField),
						  responseField.modifiers)
				   << R"cpp( )cpp" << responseField.cppName << R"cpp( {};
)cpp";
	}

	headerFile << R"cpp(};

Response parseResponse(response::Value response);
)cpp";

	pendingSeparator.add();
	pendingSeparator.reset();

	return true;
}