Cppgraphqlgen
C++ GraphQL schema service generator
Install / Use
/learn @microsoft/CppgraphqlgenREADME
Introduction
GraphQL and React go together like peanut butter and jelly, especially if you use a GraphQL client/compiler like Relay or Apollo.
But GraphQL services are only implemented on the server. When using React Native or React JS in a hybrid application, you typically have a native application which hosts islands or entire pages of UI rendered with React components, and you might like to display content that you've cached offline or that you otherwise generate on the client without needing to declare a separate data interface or require a server round trip to load it.
This project includes a graphqlservice library with the core functionality of a GraphQL service
and a schemagen utility to generate types for your custom GraphQL service schema definition. Once
you implement the pure virtual methods on the object interfaces and add hooks to the Relay
Network Layer/Apollo
Terminating Link to call your
service, you can use the same GraphQL client code to access your native data source or a GraphQL
service online. You might even be able to share some more of that code between a progressive web
app and your native/hybrid app.
If what you're after is a way to consume a GraphQL service from C++, this project also includes
a graphqlclient library and a clientgen utility to generate types matching a GraphQL request
document, its variables, and all of the serialization code you need to talk to a graphqlservice
implementation. If you want to consume another service, you will need access to the schema definition
(rather than the Introspection query results), and you will need be able to send requests along with
any variables to the service and parse its responses into a graphql::response::Value (e.g. with the
graphqljson library) in your code.
Related projects
The most complete examples I've built are related to GqlMAPI:
- eMAPI: Windows-only Electron app which lets you access the MAPI interface used by Microsoft Outlook. Its goal is to be a spiritual successor to a debugging and diagnostic tool called MFCMAPI.
- electron-gqlmapi: Native module for Electron
which hosts
GqlMAPIineMAPI. It includes JS libraries for calling the native module across the Electron IPC channel. - Tauri-GqlMAPI: Reimplementation of
eMAPIbuilt in Rust and TypeScript on top of Tauri. - gqlmapi-rs:
Rustcrate I built to expose safe bindings forGqlMAPI. It is loosely based onelectron-gqlmapi, and it is used byTauri-GqlMAPI.
I created a couple of sample projects to demonstrate integrating the
schema.today.graphql service into an Electron app. The
schema.today.graphql schema and service implementation in TodayMock.* are focused on testing,
so the specific bootstrapping used in these projects should not be re-used, but you can use them
as a scaffold to start your own integration with Electron or Node.js. They're available under
my personal account, and I recently updated them to match the latest release of cppgraphqlgen:
- electron-cppgraphql: Node Native Module which compiles
against the version of the Node headers included in Electron. This was the starting point for
electron-gqlmapi, and it is still useful as a sample because it does not depend on a platform-specific API likeMAPI, so it works cross-platform. - cppgraphiql: Electron app which consumes
electron-cppgraphqland exposes an instance of GraphiQL on top of it.
Feel free to use either or both of these as a starting point to integrate your own generated
service with Node, Electron, or Tauri. PRs with links to your own samples are always welcome.
Getting Started
Installation process
The minimum OS and toolchain versions I've tested with this version of cppgraphqlgen are:
- Microsoft Windows: Visual Studio 2019
- Linux: Ubuntu 20.04 LTS with gcc 10.3.0
- macOS: 11 (Big Sur) with AppleClang 13.0.0.
The key compiler requirement is support for C++20 including coroutines and concepts. Some of these compiler versions still treat coroutine support as experimental, and the CMake configuration can auto-detect that, but earlier versions of gcc and clang do not seem to have enough support for C++20.
The easiest way to build and install cppgraphqlgen is to use microsoft/vcpkg.
See the Getting Started section of the vcpkg README
for details. Once you have that configured, run vcpkg install cppgraphqlgen (or cppgraphqlgen:x64-windows,
cppgraphqlgen:x86-windows-static, etc. depending on your platform). That will build and install all of the
dependencies for cppgraphqlgen, and then build cppgraphqlgen itself without any other setup. The cppgraphqlgen
package (and its dependencies) are advertised to the CMake find_package function through the
-DCMAKE_TOOLCHAIN_FILE=<...>/scripts/buildsystems/vcpkg.cmake parameter/variable. There are more details about
this in the vcpkg documentation.
If you want to build cppgraphqlgen yourself, you can do that with CMake from a clone or archive of this repo.
See the Build and Test section below for instructions. You will need to install the dependencies
first where find_package can find them. If vcpkg works otherwise, you can do that with vcpkg install pegtl boost-program-options rapidjson gtest. Some of these are optional, if for example you do not build the tests. If
vcpkg does not work, please see the documentation for each of those dependencies, as well as your
platform/toolchain documentation for perferred installation mechanisms. You may need to build some or all of them
separately from source.
Software dependencies
The build system for this project uses CMake. You will need to have CMake (at least version 3.15.0) installed, and the library dependencies need to be where CMake can find them. Otherwise you need to disable the options which depend on them.
Besides the MIT license for this project, if you redistribute any source code or binaries built from these library dependencies, you should still follow the terms of their individual licenses. As of this writing, this library and all of its dependencies are available under either the MIT License or the Boost Software License (BSL). Both licenses roughly mean that you may redistribute them freely as long as you include an acknowledgement along with the license text. Please see the license or copyright notice which comes with each project for more details.
graphqlpeg
- GraphQL parsing: Parsing Expression Grammar Template Library (PEGTL) release 3.2.6, which is part of The Art of C++ library collection. I've added this as a sub-module, so you do not need to install this separately. If you already have 3.2.6 installed where CMake can find it, it will use that instead of the sub-module and avoid installing another copy of PEGTL.
graphqlservice
The core library depends on graphqlpeg and it references the PEGTL headers itself at build time. Both of those mean it
depends on PEGTL as well.
graphqljson (GRAPHQL_USE_RAPIDJSON=ON)
- JSON support: RapidJSON release 1.1.0. If you don't need JSON support, you can
also avoid installing this dependency. You will need to set
GRAPHQL_USE_RAPIDJSON=OFFin your CMake configuration to do that.
schemagen
I'm using Boost for schemagen:
- Command line handling: Boost.Program_options.
Run
schemagen -?to get a list of options. Many of the files in the samples directory were generated withschemagen, you can look at samples/CMakeLists.txt for a few examples of how to call it:
Usage: schemagen [options] <schema file> <output filename prefix> <output namespace>
Command line options:
--version Print the version number
-? [ --help ] Print the command line options
-v [ --verbose ] Verbose output including generated header names as
well as sources
-s [ --schema ] arg Schema definition file path
-p [ --prefix ] arg Prefix to use for the generated C++ filenames
-n [ --namespace ] arg C++ sub-namespace for the generated types
--source-dir arg Target path for the <prefix>Schema.cpp source file
--header-dir arg Target path for the <prefix>Schema.h header file
--stubs Unimplemented fields throw runtime exceptions instead
of compiler errors
--no-introspection Do not generate support for Introspection
I've tested this with several versions of Boost going back to 1.65.0. I expect it will work fine with mo
Related Skills
node-connect
351.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
110.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
351.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
351.4kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
