Introduction: GraphQL and C++ — A Powerful Duo for Modern APIs
\n\nGraphQL is becoming the de facto standard for building flexible and efficient APIs. While most tutorials focus on JavaScript or Python, C++ remains indispensable for high-load systems, game servers, and embedded solutions. In this step-by-step guide, we will build a full-fledged GraphQL API in C++ from scratch, using the graphql-cpp library and the cpp-httplib server.
You will learn how to set up the environment, define a schema, implement resolvers, and run the server. All code examples can be immediately applied in real projects.
\n\n1. Environment Setup and Dependency Installation
\n\nFor this project, we will need:
\n- \n
- A compiler with C++17 support (GCC 8+, Clang 7+, MSVC 2019+) \n
- CMake (version 3.14+) \n
- The
graphql-cpplibrary (GraphQL implementation) \n - The
cpp-httpliblibrary (HTTP server) \n - The vcpkg package manager (optional, but convenient) \n
Installation via vcpkg
\ngit clone https://github.com/Microsoft/vcpkg.git\ncd vcpkg\n./bootstrap-vcpkg.sh\n./vcpkg install graphql-cpp cpp-httplib nlohmann-json\n\nProject Structure
\ngraphql-cpp-api/\n├── CMakeLists.txt\n├── src/\n│ ├── main.cpp\n│ ├── schema.cpp\n│ ├── schema.h\n│ ├── resolvers.cpp\n│ └── resolvers.h\n└── vcpkg.json\n\nCMakeLists.txt
\ncmake_minimum_required(VERSION 3.14)\nproject(GraphQLCppApi)\n\nset(CMAKE_CXX_STANDARD 17)\nset(CMAKE_CXX_STANDARD_REQUIRED ON)\n\nfind_package(graphql-cpp CONFIG REQUIRED)\nfind_package(cpp-httplib CONFIG REQUIRED)\nfind_package(nlohmann_json CONFIG REQUIRED)\n\nadd_executable(graphql_server\n src/main.cpp\n src/schema.cpp\n src/resolvers.cpp\n)\n\ntarget_link_libraries(graphql_server\n graphql-cpp::graphql-cpp\n cpp-httplib::cpp-httplib\n nlohmann_json::nlohmann_json\n)\n\nNow everything is ready for writing code.
\n\n2. Defining the GraphQL Schema and Types
\n\nLet's start by creating a schema for a simple book management API. We will have Book and Author types, as well as queries and mutations.
schema.h
\n#ifndef SCHEMA_H\n#define SCHEMA_H\n\n#include <graphqlcpp/GraphQLSchema.h>\n\nstd::shared_ptr<graphql::GraphQLSchema> createSchema();\n\n#endif\n\nschema.cpp
\n#include "schema.h"\n#include "resolvers.h"\n\nstd::shared_ptr<graphql::GraphQLSchema> createSchema() {\n using namespace graphql;\n\n // Author type\n auto authorType = object_type("Author", {\n field("id", int_type()),\n field("name", string_type()),\n field("books", list_type(int_type()))\n });\n\n // Book type\n auto bookType = object_type("Book", {\n field("id", int_type()),\n field("title", string_type()),\n field("authorId", int_type()),\n field("year", int_type()),\n field("genre", string_type())\n });\n\n // Query root\n auto queryType = object_type("Query", {\n field("books", list_type(bookType), {\n argument("genre", string_type(), true) // optional argument\n }, resolveBooks),\n field("book", bookType, {\n argument("id", int_type(), false) // required argument\n }, resolveBookById),\n field("authors", list_type(authorType), resolveAuthors)\n });\n\n // Mutation root\n auto mutationType = object_type("Mutation", {\n field("addBook", bookType, {\n argument("title", string_type(), false),\n argument("authorId", int_type(), false),\n argument("year", int_type(), false),\n argument("genre", string_type(), false)\n }, resolveAddBook),\n field("deleteBook", bool_type(), {\n argument("id", int_type(), false)\n }, res