Closed mbirkegaard closed 9 months ago
@mbirkegaard Hey, can you please rebase your branch onto master, so the CI checks run on this PR?
Done
Casting like this in define.ts
resolve: opts.resolve ?? ((v: Event) => v as unknown as Out)
makes the argument to resolve unknown
which makes this typecheck
// subscribe yields an Event type that does not extend Out
// so this does not type check
t.subscriptionField({
name: 'foo',
type: t.Boolean,
subscribe: async function* () {
yield { bar: true };
},
resolve: (p) => p,
});
instead of erroring with
type { bar: boolean } is not assignable to bool
I updated the references to the spec and graphql-js in the description, but I'm a bit confused about whether the behavior I've read into this is actually what the spec says it should be.
This adds a resolve function to subscription fields.
As per the spec,
graphql-js
calls resolve on each event in the stream returned by subscribe. However, when using the type factory ingqtx
the resolve function is just the identity.In other words, if subscribe yields values of type
Event
that are not compatible with the field type (Out
), we need to be able to supplyresolve
to map fromEvent
toOut
.A neater and non-breaking solution would be to only require the resolve function if the type of the event in the stream is not compatible with the output type of the field, but I was unable to get that working using an
Event extends Out ? ...
trick similar to how the resolve function is optional for fields ifSrc[TKey] extends Out
.An attempt at making it optional is here https://github.com/mbirkegaard/gqtx/commit/c1355124fe72d5ef516659cf9027da31d22e071e, but various attempts failed with the compiler error
despite
Event extends Out