C++ GraphQL 方案服务生成器
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.
The most complete examples I've built are related to GqlMAPI:
GqlMAPI in eMAPI. It includes JS libraries for calling the native module across the
Electron IPC channel.eMAPI built
in Rust and TypeScript on top of
Tauri.Rust crate I built to expose safe
bindings for GqlMAPI. It is loosely based on electron-gqlmapi, and it is used by
Tauri-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-gqlmapi, and it is still useful as a sample because it does not depend on a platform-specific
API like MAPI, so it works cross-platform.electron-cppgraphql and
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.
The minimum OS and toolchain versions I've tested with this version of cppgraphqlgen are:
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.
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.
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.
GRAPHQL_USE_RAPIDJSON=ON)GRAPHQL_USE_RAPIDJSON=OFF in your CMake configuration to
do that.I'm using Boost for schemagen:
schemagen -? to get a list of options. Many of the files in the samples directory were generated
with schemagen, you can look at samples/CMakeLists.txt for a few examples of how to call it:…I've tested this with several versions of Boost going back to 1.65.0. I expect it will work fine with most versions of
Boost after that. The Boost dependencies are only used by the schemagen utility at or before your build, so you
probably don't need to redistribute it or the Boost libraries with your project.
If you are building shared libraries on Windows (DLLs) using vcpkg or BUILD_SHARED_LIBS=ON in CMake, be aware that this
adds a runtime dependency on a Boost DLL. The schemagen tool won't run without it. However, in addition to automating
the install of Boost, vcpkg also takes care of installing the dependencies next to schemagen.exe when building the
Windows and UWP shared library targets (the platform triplets which don't end in -static).
The clientgen utility is based on schemagen and shares the same external dependencies. The command line arguments
are almost the same, except it takes an extra file for the request document and there is no equivalent to --stubs:
…This utility should output one header and one source file for each request document. A request document may contain more
than one operation, in which case it will output definitions for all of them together. You may limit the output to a
single operation from the request document by specifying the --operation (or -o) argument with the operation name.
The generated code depends on the graphqlclient library for serialization of built-in types. If you link the generated
code, you'll also need to link graphqlclient, graphqlpeg for the pre-parsed, pre-validated request AST, and
graphqlresponse for the graphql::response::Value implementation.
Sample output for clientgen is in the sub-directories of samples/client, and several of them are
consumed by unit tests in test/ClientTests.cpp.
GRAPHQL_BUILD_TESTS=ON)GRAPHQL_BUILD_TESTS=OFF in your CMake
configuration.See GraphQLService.h for the base types implemented in
the graphql::service namespace.
Take a look at the samples/learn directory, starting with StarWarsData.cpp to see a sample implementation of a custom schema defined in schema.learn.graphql. This is the same schema and sample data used in the GraphQL tutorial on https://graphql.org/learn/. This directory builds an interactive command line application which can execute query and mutation operations against the sample data in memory.
There are several helper functions for CMake declared in
cmake/cppgraphqlgen-functions.cmake, which is automatically included if you use
find_package(cppgraphqlgen) in your own CMake project. See
samples/learn/schema/CMakeLists.txt and
samples/learn/CMakeLists.txt, or
暂无开放 Issues,或尚未同步最近议题。