GraphQL API mit C++: Schritt-für-Schritt-Tutorial
GraphQL ist eine leistungsstarke Abfragesprache für APIs, die es Clients ermöglicht, genau die Daten zu erhalten, die sie benötigen, und nichts Überflüssiges. Obwohl C++ nicht die beliebteste Sprache für die Erstellung von Web-APIs ist, machen seine Leistung und Speicherkontrolle es zu einer ausgezeichneten Wahl für hochbelastete Systeme. In diesem Tutorial erstellen wir einen funktionierenden GraphQL-Server in C++ unter Verwendung der Bibliothek graphql-cpp.
Was für den Start benötigt wird
Für die Arbeit benötigen Sie:
- Einen Compiler mit C++17-Unterstützung (GCC 8+, Clang 7+, MSVC 2019+)
- CMake Version 3.14 oder höher
- Die Bibliothek
graphql-cpp(Installation über vcpkg oder Build aus den Quellen) - Die Bibliothek
libmicrohttpdfür den HTTP-Server
Installieren wir die Abhängigkeiten über vcpkg:
git clone https://github.com/microsoft/vcpkg.gitcd vcpkg./bootstrap-vcpkg.sh./vcpkg install graphql-cpp libmicrohttpdErstellung der grundlegenden Projektstruktur
Beginnen wir mit der Einrichtung des CMake-Projekts. Erstellen Sie die Datei CMakeLists.txt:
cmake_minimum_required(VERSION 3.14)project(graphql-cpp-tutorial)
set(CMAKE_CXX_STANDARD 17)set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(graphql-cpp CONFIG REQUIRED)find_package(MHD CONFIG REQUIRED)
add_executable(graphql-server main.cpp)target_link_libraries(graphql-server graphql-cpp::graphql-cpp MHD::MHD)Jetzt erstellen wir die Hauptdatei main.cpp mit einem minimalen Server:
#include <graphql-cpp/GraphQL.h>#include <microhttpd.h>#include <iostream>#include <string>
using namespace graphql::cpp;
// Funktion zur Verarbeitung von HTTP-Anfragenstatic enum MHD_Result request_handler(void* cls, struct MHD_Connection* connection, const char* url, const char* method, const char* version, const char* upload_data, size_t* upload_data_size, void** con_cls) { const char* response_str = "{\\"data\\":{\\"hello\\":\\"world\\"}}"; struct MHD_Response* response = MHD_create_response_from_buffer( strlen(response_str), (void*)response_str, MHD_RESPMEM_PERSISTENT ); MHD_add_response_header(response, "Content-Type", "application/json"); int ret = MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response(response); return ret == MHD_YES ? MHD_YES : MHD_NO;}
int main() { struct MHD_Daemon* daemon = MHD_start_daemon( MHD_USE_INTERNAL_POLLING_THREAD, 4000, NULL, NULL, &request_handler, NULL, MHD_OPTION_END ); if (daemon == NULL) { std::cerr << "Failed to start server" << std::endl; return 1; } std::cout << "Server running on http://localhost:4000/graphql" << std::endl; getchar(); MHD_stop_daemon(daemon); return 0;}Definition des GraphQL-Schemas
Jetzt fügen wir ein echtes GraphQL-Schema hinzu. Erstellen wir den Typ Book und den Wurzeltyp Query:
#include <graphql-cpp/Schema.h>#include <graphql-cpp/Type.h>#include <vector>#include <memory>
using namespace graphql::cpp;
// Datenmodellstruct Book { int id; std::string title; std::string author; int year;};
// Buch-Repository (Simulation einer Datenbank)class BookRepository {public: static std::vector<Book> getAll() { return { {1, "Krieg und Frieden", "Leo Tolstoi", 1869}, {2, "Verbrechen und Strafe", "Fjodor Dostojewski", 1866}, {3, "Der Meister und Margarita", "Michail Bulgakow", 1967} }; } static Book getById(int id) { auto books = getAll(); for (const auto& book