GraphQL API with C++: Step-by-Step Tutorial
GraphQL is a powerful query language for APIs that allows clients to request exactly the data they need, and nothing extra. While C++ is not the most popular language for building web APIs, its performance and memory control make it an excellent choice for high-load systems. In this tutorial, we will create a working GraphQL server in C++ using the graphql-cpp library.
What You Need to Get Started
For this project, you will need:
- A compiler with C++17 support (GCC 8+, Clang 7+, MSVC 2019+)
- CMake version 3.14 or higher
- The
graphql-cpplibrary (install via vcpkg or build from source) - The
libmicrohttpdlibrary for the HTTP server
Install dependencies via vcpkg:
git clone https://github.com/microsoft/vcpkg.gitcd vcpkg./bootstrap-vcpkg.sh./vcpkg install graphql-cpp libmicrohttpdCreating the Basic Project Structure
Let's start by setting up the CMake project. Create a CMakeLists.txt file:
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)Now create the main file main.cpp with a minimal server:
#include <graphql-cpp/GraphQL.h>#include <microhttpd.h>#include <iostream>#include <string>
using namespace graphql::cpp;
// HTTP request handler functionstatic 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;}Defining the GraphQL Schema
Now let's add a real GraphQL schema. We'll create a Book type and a root Query type:
#include <graphql-cpp/Schema.h>#include <graphql-cpp/Type.h>#include <vector>#include <memory>
using namespace graphql::cpp;
// Data modelstruct Book { int id; std::string title; std::string author; int year;};
// Book repository (simulating a database)class BookRepository {public: static std::vector<Book> getAll() { return { {1, "War and Peace", "Leo Tolstoy", 1869}, {2, "Crime and Punishment", "Fyodor Dostoevsky", 1866}, {3, "The Master and Margarita", "Mikhail Bulgakov", 1967} }; } static Book getById(int id) { auto books = getAll(); for (const auto& book