Skip to content

feat: hook into onExecuteSubscriptionEvent and useContextPerSubscriptionEvent #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from

Conversation

n1ru4l
Copy link
Collaborator

@n1ru4l n1ru4l commented May 13, 2021

This is the first attempt of trying to solve #80

Example Usage:

import { envelop } from "@envelop/core";

const getEnveloped = envelop({
  plugins: [
    useContextPerSubscriptionEvent(async () => ({
      contextValue: { someValue: 1 },
      onEnd: () => cleanupResources()
    }))
  ]
});

const { subscribe } = getEnveloped();

There are several things that are currently not optimal

1. subscribe override

I created a copy of subscribe from graphql-js (which is surprisingly lightweight) and adjusted it so a custom execute function can be used for the ExecuteSubscriptionEvent phase.

I assume that the reason why it is not possible to provide a custom execute to the original subscribe from graphql-js is that the usage of execute is considered an implementation detail.

There is a pull request that attempts to allow overriding the execute passed to subscribe but no update from the graphql-js maintainers has been made recently. (graphql/graphql-js#2485) I also created graphql/graphql-js#3071

The plugin hard overrides the subscribe function. I cannot imagine any use-cases right now where you would want to compose subscribe functions (but I for example compose execute functions within graphql-live-query, so there might be a niche for that also with subscribe).

Ideally, I would like to avoid having a copy of subscribe, but I see no other option right now.

2. Context composition hook compatibility

This does currently not work with context composition, as we use a custom factory for creating the context. It would be interesting to get more insight on this from different people. I usually use the same context factory function for all operation types (mutation, subscription, or query). So maybe instead of having a custom createContext factory function, we should instead construct the context the same way as for execute (or subscribe). Probably the useContextPerSubscriptionValue createContext argument should be optional and instead, the implementation should use the default composition way of creating the context if the argument is not provided. Right now you would have to do sth like this:

import { envelop } from "@envelop/core";

let contextFactor = null;
const getEnveloped = envelop({
  plugins: [
    useContextPerSubscriptionEvent(async () => ({
      contextValue: await contextFactory()
    }))
  ]
});

const { subscribe, _contextFactory } = getEnveloped();
contextFactory = _contextFactory;

@changeset-bot
Copy link

changeset-bot bot commented May 13, 2021

🦋 Changeset detected

Latest commit: 3bb8394

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@envelop/core Patch
@envelop/types Patch
@envelop/execute-subscription-event Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@n1ru4l n1ru4l changed the title wip: theoretical implementation wip: useContextPerSubscriptionValue May 13, 2021
@n1ru4l n1ru4l linked an issue May 13, 2021 that may be closed by this pull request
@theguild-bot
Copy link
Collaborator

theguild-bot commented May 13, 2021

The latest changes of this PR are available as alpha in npm (based on the declared changesets):

@envelop/core@0.2.2-alpha-21f43b1.0
@envelop/execute-subscription-event@0.1.1-alpha-21f43b1.0
@envelop/types@0.1.5-alpha-21f43b1.0

@n1ru4l n1ru4l requested a review from dotansimha May 13, 2021 15:23
@n1ru4l n1ru4l changed the title wip: useContextPerSubscriptionValue feat: hook into onExecuteSubscriptionEvent and useContextPerSubscriptionValue May 14, 2021
@n1ru4l n1ru4l marked this pull request as ready for review May 14, 2021 06:19
@n1ru4l n1ru4l changed the title feat: hook into onExecuteSubscriptionEvent and useContextPerSubscriptionValue feat: hook into onExecuteSubscriptionEvent and useContextPerSubscriptionEvent May 14, 2021
@n1ru4l n1ru4l force-pushed the feat-context-per-subscription-event branch from ca82cbf to b8f1c2f Compare May 14, 2021 07:36
@n1ru4l n1ru4l force-pushed the feat-context-per-subscription-event branch 3 times, most recently from 9d0fdf5 to ab73859 Compare May 24, 2021 18:46
@n1ru4l n1ru4l force-pushed the feat-context-per-subscription-event branch from ab73859 to b667b3f Compare May 24, 2021 19:03
const getEnveloped = envelop({
plugins: [
useContextValuePerExecuteSubscriptionEvent(() => ({
contextValue: {
Copy link
Member

@dotansimha dotansimha Jun 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@n1ru4l maybe we should append this context to the general one?
I think it might be an issue that we have separate context created here, and not having the original one as well, since resolvers might depend on the context.

{
"name": "@envelop/execute-subscription-event",
"version": "0.1.0",
"author": "Dotan Simha <dotansimha@gmail.com>",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update this ;)

* This is a almost identical port from graphql-js subscribe.
* The only difference is that a custom `execute` function can be injected for customizing the behavior.
*/
export const subscribe = (execute: ExecuteFunction): SubscribeFunction =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some plugins might use setExecuteFn during onExecute with the assumption that they override this function.
Does it mean that these plugins need somehow to override this function here ?

Copy link
Member

@dotansimha dotansimha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good. just some minor changes :) (and we need a rebase :P )

@n1ru4l n1ru4l closed this Jul 16, 2021
@ardatan ardatan deleted the feat-context-per-subscription-event branch October 27, 2021 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Subscription: Create Context per published value
3 participants