Nexus: Subscription field does not propagate payload type

Created on 19 Jun 2020  路  10Comments  路  Source: graphql-nexus/nexus

When creating a subscriptionField, if one returns a properly typed AsyncIterator it should properly type the root of the resolve method. Unfortunately because the type is T = any, it seems like it overrides the real typing.

Example:

export const MySub = subscriptionField('MySub', {
  type: SomeType,
  resolve: (payload) => {
    // payload is type object
  },
  subscribe: (_root, _args, _ctx) => {
    return createIterator() as AsyncIterator<SomeTypePayload>
  },
})
scopsubscriptions scoptypes typfeat

Most helpful comment

Closing, since we're technically now correct on the typings, and it's out of scope for Nexus to solve the inference issue.

All 10 comments

@Sytten I believe this is fixed in 0.19 - caught some errors in the kitchen sink actually. Can you give it a shot and see if it's any better?

I will trust you on this, I am still on 0.14 and I don't expect I will have time to update for a while... I can test with a pet project to see though.

What is the return type of the method subscribe? I found that sometimes with complex/nested constructs they don't properly bubble up the type of generic parameters. I have this problem with my nexus-shield plugin on objects.

AsyncGenerator<boolean, void, unknown>

@jasonkuhrt I could be wrong, but I don't know if it's possible to use the return type to infer the type usage in resolve. I could go dig up the TS issue, but I'm pretty sure you need to specify the type annotation of the first arg of resolve, it'll be type-safe (will check that the typing you supply for the source is correct), it's just not inferred.

I think you are right!

I thought that TS was able to infer a generic parameter from the output but it doesn't seem like it. :(

Closing, since we're technically now correct on the typings, and it's out of scope for Nexus to solve the inference issue.

Was this page helpful?
0 / 5 - 0 ratings