GraphQL API in C++: Step-by-Step Tutorial with Practical Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: GraphQL and C++ — A Powerful Duo for Modern APIs

\n\n

GraphQL 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.

\n\n

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\n

1. Environment Setup and Dependency Installation

\n\n

For 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-cpp library (GraphQL implementation)
  • \n
  • The cpp-httplib library (HTTP server)
  • \n
  • The vcpkg package manager (optional, but convenient)
  • \n
\n\n

Installation via vcpkg

\n
git clone https://github.com/Microsoft/vcpkg.git\ncd vcpkg\n./bootstrap-vcpkg.sh\n./vcpkg install graphql-cpp cpp-httplib nlohmann-json
\n\n

Project Structure

\n
graphql-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\n

CMakeLists.txt

\n
cmake_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\n

Now everything is ready for writing code.

\n\n

2. Defining the GraphQL Schema and Types

\n\n

Let'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.

\n\n

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\n

schema.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                

Blogs

Book Recommendations