question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Combine `subscribe` with `execute`?

See original GitHub issue

Right now, subscribe and execute are two functions. And there are some issues with that.

  1. Semantically, subscription is a type of execution. In the spec, Query, Mutation and Subscription are on the same level. Subscription is not an outlier.
  2. When I get a Document, I need to go through validations to get the Operation, and then call execute or subscribe based on Operation’s type, and they go through a similar validation, resulting in duplicated work.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jasonkuhrtcommented, Jul 6, 2020

Just happened this use-case. As a user, my initial expectation was that I would be able to do query.execute. I was initially surprised to learn that subscription was a separate method.

But I sympathize with how the polymorphic type this would result in could hurt DX.

A new TS generic parameter and/or casting could do their bit to help, but only modestly.

Most of the time, in practice, what would probably happen, is users narrowing the result before using it.

In the end, having the API allow to do this branching up front (method selection vs result narrowing) seems very reasonable.

I don’t see a “best” option here. Maybe both, e.g.:

graphql.execute(...) // ExecutionResult | Promise<AsyncIterator<ExecutionResult>
graphql.executeSubscription() // Promise<AsyncIterator<ExecutionResult>
graphql.executeQuery() // ExecutionResult
graphql.executeMutation() // ExecutionResult

I don’t know if splitting Query / Mutation for API symmetry has any functional benefit here. Maybe that’s acceptable, not sure.

0reactions
ericlscommented, Sep 3, 2019

@IvanGoncharov , thanks again for you reply.

buildExecutionContext is indeed very helpful. I think I’ll use it for now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Receiving and Handling Events with Combine
The first closure executes when it receives Subscribers. ... If you no longer want to subscribe to the publisher, you can cancel the...
Read more >
subscribe(on:) vs receive(on:) - try Code
subscribe (on:) sets the scheduler on which you'd like the current subscription to be “managed” on. This operator sets the scheduler to use...
Read more >
Combine`s subscribe(on:options:) operator - Stack Overflow
Specifies the scheduler on which to perform subscribe, cancel, and request operations. In contrast with receive(on:options:), which affects ...
Read more >
Getting Started with Combine in Swift - Medium
Subscribing to a Simple Publisher​​ The Combine Framework adds a publisher property to an Array. We can use this property to turn an...
Read more >
Understanding Combine's publishers and subscribers
A publisher/subscriber relationship in Combine is solidified in a third object, the subscription. When a subscriber is created and subscribes to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found