cppgraphqlgen icon indicating copy to clipboard operation
cppgraphqlgen copied to clipboard

Subscription event payloads need to implement stubs for every field

Open wravery opened this issue 10 months ago • 0 comments

Suppose you have a Subscription type in your schema like so:

type Subscription {
  fieldA: String!
  fieldB: String!
}

The type-erased object::Subscription interface will have resolvers that call field accessors for getFieldA and getFieldB. If you generate the schema with schemagen --stubs, it will implement placeholder stubs that throw std::runtime_error if you try to access either of those fields without defining the field accessor. However, if you don't specify --stubs, it will static_assert that all of the field accessors have been implemented.

When subscribing, the selection set can only ever have 1 field per event; see line 5 in Source Stream. So, the object::Subscription type needs to support subscribing to any field, but the payloads of the events can only ever access 1 of the fields. If you use the same type for multiple field payloads, essentially a std::variant of the fields, that's OK, but otherwise you either need to use --stubs (which makes it easier to forget/mismatch a field accessor signature anywhere else in the schema), or implement your own stubs for the fields that you are not using in the subscription event payload.

The current suggestion is to emit an additional event payload type for each Subscription field which only requires that field accessor to be implemented, or possibly a single payload type that includes stubs but has static_asserts that at least 1 field accessor has been implemented unless you pass --stubs for the global stub generation.

wravery avatar Apr 26 '24 16:04 wravery