microsoft / cppgraphqlgen

C++ GraphQL schema service generator
MIT License
325 stars 46 forks source link

Subscription usage #290

Closed 0xmemorygrinder closed 1 year ago

0xmemorygrinder commented 1 year ago

Hello,

I'm using cppgraphqlgen on a project in which the backend is in c++.

The schema defines subscriptions but I don't find any usage of Request::subscribe in example. Where should I retrieve an instance of Request inside the subscription handler ?

Is it possible to add this in sample/today or in the subscriptions documentation ?

Thanks in advance

wravery commented 1 year ago

There are some examples in the unit tests, e.g. https://github.com/microsoft/cppgraphqlgen/blob/1d659227bfc51fb7d9bb5dc9862234e7cfd1b1e3/test/TodayTests.cpp#L550C26-L550C63. There's also a more complete example in https://github.com/microsoft/gqlmapi/blob/main/src/Subscription.cpp, which demonstrates using a default Subscription object to handle the service::ResolverContext::NotifySubscribe and service::ResolverContext::NotifyUnsubscribe events with separate objects (ItemsSubscription, RootFoldersSubscription, and SubFoldersSubscription) for the service::ResolverContext::Subscription event used in a call to service::Request::deliver.

It's up to you how you manage the lifetime of the service::Request/mynamespace::Operations object, typically it's in a std::shared_ptr, and if your subscription callback needs to reference it, you can capture a copy of the std::shared_ptr (or a std::weak_ptr) in a lambda.

0xmemorygrinder commented 1 year ago

I switched from graphql to my own protocol but thanks for the answer anyway !