An easy to use C++ GraphQL Client.
Alongside gqlxy-server, I wanted to build a GraphQL Client with few dependencies, based on Apollo Client's structure.
gqlxy-client is a C++20 GraphQL client library modelled after Apollo Client. It provides a composable link chain for transport, an Apollo-style normalized InMemoryCache, fetch policies, and a coroutine-native Observable API, all without requiring a framework or runtime.
Client— single entry point forQuery,Mutation,Subscribe, andRefetchObservable<T>— supports bothco_await(first value) and.subscribe()for streamingHttpLink— HTTP/HTTPS via Boost.Beast; detectstext/event-streamand streams SSE subscriptions automaticallyWsLink— persistent WebSocket using thegraphql-transport-wsprotocol, with auto-reconnect and back-offSplitLink— routes requests between two links via a predicate (e.g. queries over HTTP, subscriptions over WS)InMemoryCache— thread-safe, normalised entity store with__typename:idkeying and configurable type policies- Fetch policies —
CacheFirst(default),NetworkOnly,CacheAndNetwork,NoCache - Document transforms —
AddTypenameapplied by default; fully configurable viaClientOptions::documentTransforms - Custom links — implement
Link::Execute()to plug in any transport or middleware
Add GQLXY to your vcpkg.json:
{ "dependencies": ["gqlxy-client"] }find_package(gqlxy-client CONFIG REQUIRED)
target_link_libraries(my_app PRIVATE gqlxy::client)#include <gqlxy/client/client.h>
#include <gqlxy/client/links/http_link.h>
#include <gqlxy/client/links/ws_link.h>
#include <gqlxy/client/links/split_link.h>
#include <gqlxy/client/cache/in_memory_cache.h>
using namespace gqlxy;
Client client({
.link = std::make_shared<SplitLink>(
[](const GraphQLRequest& req) { return req.type != OperationType::Subscription; },
std::make_shared<HttpLink>(HttpLinkOptions{.url = "http://localhost:4000/graphql"}),
std::make_shared<WsLink>(WsLinkOptions{.url = "ws://localhost:4000/graphql"})
),
.cache = std::make_shared<InMemoryCache>()
});Task<void> run(Client& client) {
GraphQLResponse result = co_await client.Query({
.query = R"( query GetUser($id: ID!) { user(id: $id) { id name } } )",
.variables = {{"id", "42"}}
});
if (result.data) {
// use result.data->at("user")
}
}Subscription sub = client.Subscribe({
.query = R"( subscription { onMessage { text } } )"
}).subscribe(
[](const GraphQLResponse& r) { /* handle event */ },
[](std::exception_ptr) { /* handle error */ }
);
// later:
sub.Unsubscribe();See docs/getting-started.md for FetchContent and from-source options.
Check out the full documentation on https://gqlxy.dev
MIT
