Repository navigation
Open
Labels
enhancementNew feature or request
Description
This is where I'm going to start tracking features for 5.0, the next major version of cppgraphqlgen. This will be a breaking change for existing consumers, so it's an opportunity to cleanup deprecated methods and simplify some of the APIs. I'm also going to adopt a few more C++20 and C++23 features if they are well enough supported on Windows, Linux, and macOS now.
This is using #172, the tracking issue for cppgraphqlgen 4.0, as a template and starting point.
- Stand up a branch for 5.0 and start making PRs against that: https://github.com/microsoft/cppgraphqlgen/tree/next.
- C++20: Modules.
- C++20: Ranges: Should modernize a lot of begin/end iterator usage.
- C++20:
std::fmtinstead ofstd::ostringstream. - C++20: Co-routines/Awaitable interoperability: See Any real-world examples where the coroutine backing can be applied? #271, this may require a breaking change to the
cppgraphqlgenAwaitabletypes. - Sharing input/scalar/enum types between
clientgenandschemagen: See Cannot use multiple client object in single compilation unit. #264 for more discussion. - Build some alternate implementations of
JSONResponse.*with other JSON libraries besidesRapidJSON. - Experiment with resolver visitor pattern instead of always going through
service::ResolverResult. #320 - Generate separate headers/modules for enums and input types and include that in
...Schema.hinstead of inline declarations. #321 - Add support for schema stitching. #323
- Update docs for v5.0 in next branch #330
I'll update this list as new features crop up and as I/we make progress on it. If there's something you'd like to see that I haven't included already, feel free to comment on this issue.
Reacted by Matt Hargett
Metadata
Metadata
Assignees
Labels
enhancementNew feature or request
Activity
wravery commentedon Sep 19, 2024
I'm planning on making a release candidate as soon as the CI build finishes. I haven't done any of the documentation updates yet, but if anyone wants to take it for a spin, 5.0.0 should be completely implemented in the next branch now.
nqf commentedon Sep 20, 2024
Sorry,I am not qualified to demand anything, but let me share my personal feelings, We compared cpp ,Golang and Rust, ,their(rust,Golang ) grphql framework makes it easier for a novice to start from scratch. Would you consider generating something similar, This is an example of Rust, which really makes it very easy for a beginner to get started, rust-graphql-example
The CPP framework requires us to write some checks ourselves, which is very long and difficult for beginners. In fact, many times we don't need to know the underlying details, we just want to write business logic
cppgraphqlgen-beast-http-server
wravery commentedon Sep 20, 2024
There are some of those samples out there for
cppgraphqlgen, in particular the "Star Wars" learning sample is implemented here: next/main. There are also some unrelated samples linked in the root README.To be honest, I prefer working in Rust with async-graphql myself. In a situation where I'm just trying to implement a GraphQL service with the performance of a compiled native language, I'd reach for that first. What
cppgraphqlgenis aimed at is more of a bridge to existing C++ projects. Similarly, I'd probably pick something else besidecppgraphqlgenorasync-graphqlif I were trying to connect logic in another language/stack.The
Boost.BeastHTTP server example serves a couple of purposes:I don't recommend using the
Boost.Beastsample as a starting point for a new web service, though, unless you need to use 100% C++ in your project for some reason. Given my own preference for Rust, even if I needed to expose acppgraphqlgenservice through HTTP, I'd probably use a Rust web server and thinner GraphQL bindings to thecppgraphqlgenservice, like in gqlmapi-rs.nqf commentedon Sep 20, 2024
Thank you for your reply, Actually, our project is 100% cpp, so I would prefer cpp to have a graphql framework similar to rust‘s async-graphql, But in reality, there is no decent GraphQL framework in the CPP , Therefore, we ultimately chose to use Golang to implement the GraphQL server, But personally, But I sincerely hope that CPP also has a decent(simple + easy-to-use) GraphQL framework
wravery commentedon Sep 20, 2024
Yeah, unfortunately C++ just can't do it as simply. I haven't used Golang or its GraphQL frameworks, but I am familiar with a couple of Rust GraphQL frameworks, and a lot of their ease-of-use comes from procedural macros. There are a few proposals out there for static reflection in C++ or meta-classes that could do something similar, but AFAIK those are many years away from becoming part of the standard.
I think static code generators are the closest we can get to hiding the ugly details from C++.
nqf commentedon Sep 20, 2024
In fact, cpp can generate code like Golang and finally expose something similar to a callback function. We only need to implement business logic in the callback function
nqf commentedon Sep 20, 2024
Actually, I think cppgraphqlgen should be able to be packaged into something like this, But it may be necessary to write a code generation tool to generate the graphql_server class
gitmodimo commentedon Sep 20, 2024
I have been using cppgraphqlgen in production code for about 2 years. Here are some of my thouths:
I think it seems viable to allow joining two
graphql::service::Requestservices as follows:Main schema
Extension schema
I my opinion it should be possible to generate two separate compilation units/libraries that could be literally added to each other at runtime. As in:
Addition should expand
TypeMap _operationsand merge introspection infofmation.A. Response is held in
graphql::response::Valuewhich is DOM representation that ususally converted to json using additional library. This could be shortcut with service taking visitor instead of returning response. In fact default visitor could begraphql::response::Valuebuilder to keep backward compatibility, but it could be replaced by JsonBuilder based on for example RapidJSON.B. There should be more fine grained control for subscriptions (especially NotifySubscribe and NotifyUnsubscribe). I think subscription should return a handle to allow external fine grained control.
Ex. In subscription that uses list field it may happen that list changes between NotifySubscribe and NotifyUnsubscribe. In such case NotifyUnsubscribe is useless in "cleaning up" Subscription event sources. I end up using external accounting of tracking the sources and removing then when subscription completes. I want to be able to skip NotifyUnsubscribe step and just remove from subscription list. I my use case I also always generate initial response for subscription (like in live queries) and thus i can use initial response generation to subscribe to all event sources (instead of NotifySubscribe).
C. The library always wraps objects and scalars in awaitable future (and then awaits them) even when result is returned by value. This causes ridiculous overhead when creating scalar arrays. So far i haven't found a use case for
AwaitableObject/AwaitableScalarI agree. And also since all fields originate from schema there will always be a need for external tool to parse schema and generate fields. Static reflection can maybe used to reduce amount of generated code. I don't think there will ever be a compile time AST parser that does not kill the compiler.
EDIT:
4. My need for extensibility has led me to move entire generated part of code to external dll. It was actually possible because graphql dictates return value type and it possible to create models implicitly. My working concept. The only problem is with variant types since we do not know which variant is supposed to be created. I worked this around with the use of additional traits. As a result my entire solution is rid of generated headers. All except XXXSchema.h because I still need enums and input types. Generating Enums and Input types into separate header(separate from what is below this line ) would be useful (also maybe for this).
wravery commentedon Sep 20, 2024
What you're describing sounds more like a web framework than what
cppgraphqlgendoes. I haven't tried it myself, but Oat++ sounds like it fits. If you (or anyone else) wanted to try combining them, I think both projects would benefit. I'd be happy to add another link to the README. 😃wravery commentedon Sep 20, 2024
Great feedback below, thanks!
Interesting, yeah. I'm not sure if having schema extensions be handled differently makes sense, but given 2 separate schemas, it should be possible to merge them at the schema level. The only tricky bit, I think, would be figuring out how to dispatch the resolvers to the correct implementation in the merged service at runtime. It might be better to treat this as a new type of service that multiplexes other static services dynamically, including building a merged schema for validation and introspection.
As far as converting to JSON goes, there is a visitor pattern already when starting from a
response::Value. Theresponse::Writerdoes this. I'm imagining an API that takes aresponse::Writer(and maybe a separate visitor to accumulate errors) and passing that to the resolvers to recursively build the results in an alternate form that bypassesresponse::Valueentirely. For instance,clientgencould generate aWriterfor the expectedResponsetypes. Both of those have some interesting implications for a dynamic multiplexer.I think you're saying that the set of resources/listeners you are tracking to support the subscription might change dynamically, e.g., as items are added or removed from a collection. The approach I'd use for that would probably be to use external bookkeeping for those resources as you do now, but store a handle/ID to those external resources in the
RequestStatefor the subscription. The handler forNotifySubscribewould initialize the external resources, when items are added or removed you can update it externally, and then onNotifyUnsubscribeyou'd release everything using the handle to your external resources.One of the changes in the next branch, fa15fb7, had a surprisingly big impact on the
benchmarkprograms, at least when running in a debug build (about 20% more throughput using clang-18 or MSVC, coming from reduced time spent resolving requests). It would make sense if the benchmark used any fields with futures that reporttimeout, which would spawn another thread, but I don't think it does. I think everything should just be deferred rather than async, which should prevent it from suspending and resuming on another thread. My guess is that making itconstexprshort-circuited more of the coroutine machinery at compile time.I haven't checked a release build yet, but I'd be curious to know if this improves your scalar array scenario.
I'll have to dig into this some more, but I gather you specialize the
GraphQLBuilderfor your object types and create a dynamicservice::Objectimplementation from that? Or have you modified other parts ofGraphQLService.cpporSchemaGenerator.cppto do something more with expected return types, so they're no longerstd::shared_ptr<object::Foo>?Re: splitting the enums and input types into one or more separate headers, I figured I'd keep it simple for now and just include the
...Schema.hfile. There's not that much other content in that header, and to use shared types you're almost certainly going to include it anyway.nqf commentedon Sep 20, 2024
We actually need an easy-to-use graphql service, Both rust and golang are easy to integrate with http servers,What I mean is that cppgraphqlgen doesn't easily fit into the cppweb framework,If this project could be like rust aync-graphql, I think it would be more popular
gitmodimo commentedon Sep 21, 2024
I think it is called Schema stitching. It seems like easiest route for runtime extensibility. It does not cover type extension but it can be emulated by replacing different static services dynamically.
yes and I reckon the response::Value building part can also be redesigned into visitor pattern.
Exactly!
And the writer would build response::Value or JSON or even act directly on visited values depending on user provider visitor.
I think it all boils down to proper SAX style writer API
I will definitely check this out. With my custom changes i did nou have time to port them to newer library verion
No there is no specialiation of
GraphQLBuilderand no other changes AFAIR. The AwaitableObject contrustor handles 3 cases:1. U&& value is the generated object model (just like in current library implementation)
2. U&& value is not generated model but can be used to create one. The type of the object model is known as T.
3. Nr 2 but future
The
GraphQLBuilderhandles building of the model T given U. It handles:Graphql:
Field accessor returns
graphql::service::Union<TypeA, TypeB>And with custom mapping trait defined the
GraphQLBuildercan build desired object modelI purposely decided to use external GraphQLUnion trait class instead of creating mapping inside TypeA/TypeB classes to be able to compile all user code without including any of generated class headers. That gives me the ability to generate multiple different schemas, reuse the objects and even adjust union mappings where necessary.
The only header that I use in user code is XXXSchema.h cause I need definitions of the inputs and enums. And the ugly part is that I also need the implementations of Input types in XXXSchema.cpp to link my user code. And due to XXXSchema.cpp implements also
Operationsit pulls in all the generated headers.If input types and enums were in separate header i could reuse the same types header for multiple different schemas as long as they all use subset of "full schema" types.
At this moment my build flow looks like this:
This solution would work nicely if only there we something equivalent to
schemagen --shared-typesandschemagen --only-typesThis would allow to separately develop different features in separate schema files (assuming no naming collisions). Each feature would generate its own "types.h" header and use it.
Then as a final step merge all the schmea-parts that are desired and generate specialized service including desired feature-set.
My dream is to be able to do it in runtime, but for now it is next best thing.
It does not fully give extensibility but it is somewhere in between.
I am including it. The problem is it pulls all other headers from cpp file.
gitmodimo commentedon Oct 21, 2024
I very much like the work you did here. I need to migrate to next branch ASAP and test the changes in my use case.
I like how you managed to implement visitor and maintain asynchronous resolving. Maybe both visitors should be available in library and let user decide whether to use asynchronous visitor or sequencing one.
I think for real live situation the benchmark should include sample transport layer implementation(like conversion toJson and fromJson). My gut feeling is that in most cases serializing visitor building JSON directly will be faster than asynchronous.
I also like the new stitching ability. Can the same approach can be used to stitch nested fields? It now looks like only query/mutation/subscription can be stitched.
wravery commentedon Oct 21, 2024
Thanks! I'm planning on doing a pilot migration of https://github.com/microsoft/gqlmapi as well, and that might flush out some issues. Having more coverage with your project will help avoid needing to make another version right after the release.
Using an empty
await_asyncshould behave this way. Building aValueTokenStreamjust defers the calls to the visitor until everything is in the right order.There are 2 JSON style visitors now, and I'm considering collapsing them into one, using the
ValueVisitorinstead ofWritersince it preserves greater fidelity for things like enum values. I just need to rework the default implementation ofWriterintoJsonto do that.At that point, I'll probably also add a
visitmethod toResolverResultso there's a way to use the visitors with a complete service request document for transport and not just thedatafield.I haven't built anything that does this yet, but it should be possible. The resolver that returns one of the type-erased
service::Objectobjects would need to create one from each schema, then stitch the sub-objects withObject::StitchObjectand return that.It requires modification of one of the sub-schema's resolvers too make it aware of this, so all I've done so far is to use the preexisting mock
todayandlearnservices to verify that I could query non-overlapping fields from both.BTW, in case of overlaps, the first schema (the one where you call
stitch) orObject(where you callStitchObject) takes priority. However, the mergedResolverMapretains the originalthiscaptures, so, there's no additional dispatch overhead to get to the original resolvers. This means you can layer multiple stitched objects or schemas without additional runtime overhead during query execution aside from the increased size of theResolverMap, which should scale atO(log(n))with binary search lookups.wravery commentedon Oct 22, 2024
One more caveat about schema stitching: since the include guards will collide on types with the same name, the cleanest/simplest way to handle that is probably to import the generated C++20 modules instead of including the headers when creating the partial
Objectfrom each sub-schema.When mixing includes and headers, always do all the includes first and then switch to imports in the main (i.e.,
.cpp) file. Don't try to import modules from a header because that easily breaks the ordering. The only way that can be safe is if it's the last header you include and it follows the rules about doing all of its own includes before it does the first import, which couples that header to the order it's included, and it makes it incompatible with any other headers that do this.gitmodimo commentedon Oct 24, 2024
I think relying on modules and imposing strict including rules will limit portability and ease of use. Can't we just add namespace to include guard? And maybe also to filenames to prevent confusing filename duplicates? Then only requirement for stitching will be to use different namespaces for stitching parts.
gitmodimo commentedon Oct 24, 2024
With stitching I see 3 fields that need individual care:
Proof of concept is already in samples. I think it should be allowed to stitch object at any level not only root. Maybe the library could accept type-erased resolver in place of any object?
Schema should support addition of new types, as well as, type extensions. This may require adding some kind of type erased "extendObject" to be generated from schema.
I am talking about a case equivalent to current stitching sample with
todayschema defineextend type Query...insted oftype Query....This is the most cumbersome part. I cannot think of any nice way to extend ENUM. For this reason I avoid extending enums. Maybe it should be allowed to return string in place of enum since they are stored as such later on? Plus maybe validate the string against schema?
wravery commentedon Oct 24, 2024
Since the type-erased objects are all inherited from
Object, and the logic inGraphQLService.cppis all written in terms of that base-type, it's fairly easy to combine type-erased objects in a resolver usingObject::StichObject(same asRequest::stitchdoes for the root objects).The tricky part with nested resolvers is, where do you get the instance of a type-erased object from the other schema? Root objects are easy, they're fixed at service instantiation, but there's a path of resolvers (some of which may not exist in both schemas) leading to the returned object in a nested resolver. That's what I think blocks auto-stitching, e.g., somewhere in
AwaitableObject. At some point while resolving a request and calling into the field resolvers, the service would need to somehow call the same resolver on both objects and stitch the results, assuming both are returned.Type extensions are parsed the same way as regular type declarations. Technically, it probably shouldn't accept an extension without a base declaration somewhere, but I don't remember if I ever tried that. So, if you want to use the
extend type Query...syntax, it should be possible to just build a schema from the extension and stitch it together the same way. Of course, if you're using extension syntax, you could also work out a way to concatenate the schema definition files and parse them into a single static schema to begin with.The way it's meant to work currently is that the schema will merge the enum values, so it'll pass validation. When you call a resolver in either schema with the same enum type, it'll pass it through the
ModifiedArgument/Argumenttypes that were built with the shared types for the target resolver's sub-schema. If you use a value it didn't know about, it'll reject the argument with an error. When returning values, it'll use a string (with theModifiedResult/Resultfrom the sub-schema of the resolver) and it'll map the integer enum values from the sub-schema to the same string as before. Fields from each sub-schema should allow all of the values they knew about, mapped to their own integer values in case of overlap, as arguments, and they can return any of the values that they contributed or share in the merged enum.Aside from introspection queries against the stitched sample, I haven't fully tested this, so LMK if you're seeing it break.
gitmodimo commentedon Oct 31, 2024
I was thinking some kind of extensible ObjectModel Factory with tag dispatch. Pseudocode sketch:
Base schema:
Extend schema:
Generated from concatenation of all schema parts:
Generated for Base:
Generated for Extend:
User implementation:
An then add some template template magic:
The idea requires more work, but I think it shows the idea. Also I think tag dispatch would also elegantly solve Unions and their extension - user would return a std::variant<std::pair<TypeTag,std::shared_ptr<Object_t>,...> and resolver would Create adequate model. I am aware that following such an approach would be total table flip and library overhaul, but in my opinion It would substantially expand its capabilities. User could even overload StitchedFactory::Create on per Type basis and perform additional runtime logic when needed. Tell me what you think. If you find potential in such approach I am very willing to put some more work into it.
For backwards compatibility ModelFactory could also have specialization for type erased Object.
This what I am doing right now. Take example above. Given an "extension schema part" I create dll for Base schema and one dll for Base+Extend schema. Then at runtime I select desired dll and use it as my resolver. This however does not scale as I need to create every possible combination with multiple "extension schema part"s. Also to achieve that I had to modify the library to move all generated types into dlls - "user" code does not instantiate
graphql::Base::Object::Testnorgraphql::BaseExtended::Object::Test. User code returnsstd::shared_ptr<Test>and factory within dll instantiates one depending on which dll is loaded.gitmodimo commentedon Nov 5, 2024
I finally got some time to dig into my performance issues. Again great work with
ValueTokenStreamimplementation. It doesn't directly help in my particular use case (big array) , but it is relatively simple to speed everything up with:I run
Todaybenchmarks on my PC on release build. Results in requests/second:Unmodified convert
Shortcut for std::vector of non-awaitable scalars.
Convert speedup
The same shortcut could be implemented for multidimensional arrays. Probably the code could be simplified if there were some kind of ValueToken traits. Next I will try to migrate my project to next branch.
gitmodimo commentedon Dec 10, 2024
I have concluded my transition to next branch. I am vary happy with the result. SharedTypes and value visitor simplified everything sybstantially. There are few additional changes that could be merged if you think they could be useful in general. Please have a look at:
next...gitmodimo:cppgraphqlgen:next
I can create PR for features you see fit.
talksik commentedon Feb 21, 2025
Similarly here. It took considerable time to get a simple example client running despite what's written in the docs about the samples and tests. I believe a client like this should abstract things like the http client used, and be 3 lines of code to make a type-safe query/mutation:
client.querywith parameter of a specific request objectLynxesExe commentedon Apr 27, 2025
Agreed. Of course we cannot expect C++ to be as elegantly simple as other languages, therefore there isn't much to say when it comes down to the API of the library.
However I am firmly convinced that providing at the very least a get started tutorial would be incredibly beneficial. It would allow users to get started implementing something, which in turn would allow people to start using the library.
If I manage to produce something that works I'll try making one.
I myself have spent the past two evenings trying to decipher how to write a minimal example with the goal of providing a query and retuning some in-memory data; so far without success (though undoubtably my lack of experience is part of the issue here).
LynxesExe commentedon Apr 28, 2025
In case anybody is interested this is guide for the current code on
main(not 5.0, since I haven't used it yet, therefore I don't know if there are any significant changes usage wise) https://github.com/LynxesExe/cppgraphqlgen-tutorial/blob/main/doc/getting_started.mdI do not know it it's good enough or worthy of a pull request, but it's essentially the collection of steps I followed to end up having a basic working service.
I needed 5 evenings to make it work, therefore if it can be of any use to anyone and save some time I'd be happy to fix it up and open a PR.
wravery commentedon May 7, 2025
Definitely worthy of a PR!
I really appreciate that this is from the perspective of a newcomer, that's not something I can accurately represent. If you do get a chance to try out v5, even better, but I think I can make the necessary edits for v5 later if you don't.
LynxesExe commentedon May 7, 2025
Thanks that's great to hear!
I'm not sure when but I will definitely try out version 5, the backend of my project is still in it's early stage, so I was playing around with version 4 to get a "feel" of the project; but targeting 5.
I have opened a PR #349 to merge towards main since I have written that document while referencing the main branch; please do let me know if there are any specific requirements, I have checked the contribution section and didn't find anything specific.
As soon as I get a chance to check v5 I'll be happy to update it!
weiwei201906 commentedon Sep 20, 2025
Can we not
drag around the smelly and lengthyBoost library? After all, many features in C++20 can adequately fulfill many functions originally provided by Boost. I hope the code can be ready to use right out of the box, thank you!Andrej-Peplinski-HBK commentedon Sep 27, 2026
Hi Bill,
thanks first of all for all the great work and effort for creating this library 💐
And I would like to ask you what are the plans to release version 5?