GraphQL API with C++: Step-by-Step Tutorial for Beginners
GraphQL is a modern query language for APIs that allows clients to request only the data they actually need. Unlike REST, where the response structure is fixed, GraphQL provides flexibility and control. In this tutorial, we will walk through creating a simple GraphQL API in C++ from scratch. You will learn how to connect the graphql-cpp library, define a schema, write resolvers, and test queries. This article is intended for beginners familiar with the basics of C++.
Why C++? This language provides high performance, which is critical for high-load APIs. GraphQL in C++ is often used in real-time systems, game engines, and high-traffic web services. Let's get started!
Setting Up the Environment
For this, we need a compiler with C++17 support (e.g., GCC 8+ or Clang 10+), CMake, and the graphql-cpp library. Let's install everything necessary.
Step 1: Installing Dependencies
On Ubuntu/Debian, run:
sudo apt updatesudo apt install build-essential cmake libboost-all-dev libssl-devFor macOS, use Homebrew:
brew install cmake boost opensslStep 2: Cloning the graphql-cpp Library
The graphql-cpp library is a lightweight implementation of GraphQL in C++. Clone the repository:
git clone https://github.com/your-repo/graphql-cpp.gitcd graphql-cppmkdir build && cd buildcmake .. -DCMAKE_BUILD_TYPE=Releasemake -j4sudo make installAfter installation, create a new project. Folder structure:
my_graphql_api/├── CMakeLists.txt├── src/│ ├── main.cpp│ └── schema.cpp└── include/ └── schema.hStep 3: Configuring CMakeLists.txt
Create the CMakeLists.txt file:
cmake_minimum_required(VERSION 3.10)project(GraphQLExample)
set(CMAKE_CXX_STANDARD 17)
find_package(graphql-cpp REQUIRED)find_package(Boost REQUIRED COMPONENTS system)
add_executable(graphql_example src/main.cpp src/schema.cpp)target_include_directories(graphql_example PRIVATE include)target_link_libraries(graphql_example graphql-cpp::graphql-cpp Boost::system)Defining the GraphQL Schema
The schema is the core of a GraphQL API. It describes data types, queries, and mutations. In our example, we will create an API for managing a list of books.
Creating the Book Type
In the file include/schema.h, declare the data structure and a function for creating the schema:
#ifndef SCHEMA_H#define SCHEMA_H
#include <graphql-cpp/graphql.hpp>#include <string>#include <vector>
struct Book { int id; std::string title; std::string author; int year;};
graphql::Schema createBookSchema();
#endif // SCHEMA_HImplementing the Schema
In the file src/schema.cpp, define the types and the root query:
#include "schema.h"#include <graphql-cpp/graphql.hpp>#include <vector>#include <memory>
using namespace graphql;
// Temporary storage for booksstd::vector<Book> books = { {1, "C++ Primer", "Stanley B. Lippman", 2012}, {2, "Effective Modern C++", "Scott Meyers", 2014}};
Schema createBookSchema() { // 1. Define the Book type ObjectType bookType("Book"); bookType.addField("id", Type::NonNull(Type::Int())); bookType.addField("title", Type::NonNull(Type::String())); bookType.addField("author", Type::NonNull(Type::String())); bookType.addField("year", Type::Int());
// 2. Define the root query ObjectType queryType("Query"); queryType.addField("books", Type::List(Type::NonNull(bookType)), [](const auto& args) -> json { json result =