Skip to content

Conversation

@quintstoffers
Copy link
Contributor

@quintstoffers quintstoffers commented Sep 28, 2016

See #10 for previous communication. Solves #7.

This change requires a setup function to return an object containing the properties filter and channelOptions. Both are optional, so no breaking changes in that regard. However, there is no backwards compatibility for the "old style" function-type return value, so existing usages need to be updated to return an object with filter property.

Code example taken from #10, adapted to object-style return value:

{
    setupFunctions: {
        // Taken from GitHunt.
        commentAdded: (options, args) => ({
            commentAdded: {
                filter: comment => comment.repository_name === args.repoFullName
            }
        }),
        // Proposed structure.
        commentUpdated: (options, args) => ({
            commentUpdated: {
                // These options could be passed to PubSub.
                channelOptions: {
                    repoFullName: args.repoFullName
                },
                // Technically redundant since we specified repoFullName above and thus should
                // only receive relevant messages. However, it's here for completeness' sake
                filter: comment => comment.repository_name === args.repoFullName
            }
        })
    }
}

Note that I added a dev dependency to sinon to be able to assert channelOptions are passed into pubsub.subscribe.

Sinon lets us assert a function is called with certian parameters.
Channel options are passed to PubSub so that we can tell it to do
certain things. For instance, we could write a PubSubEngine that is
capable of only listening to a subset of messages based on
`channelOptions` input.
@helfer
Copy link
Contributor

helfer commented Oct 1, 2016

@quintstoffers thanks!

@davidyaha do you think you could review this PR?

@davidyaha
Copy link
Contributor

Will do.

Copy link
Contributor

@davidyaha davidyaha left a comment

Choose a reason for hiding this comment

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

Other than those two remarks I've it looks good!

src/pubsub.ts Outdated
// Deconstruct the trigger options and set any defaults
let {channelOptions, filter} = trigger;

if (!channelOptions) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can be done on line 159 like so let {channelOptions = {}, filter} = trigger;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whoops, will fix!

src/pubsub.ts Outdated
const subscriptionPromises = [];
Object.keys(triggerMap).forEach( triggerName => {
// 2. generate the filter function and the handler function
const trigger = triggerMap[triggerName];
Copy link
Contributor

@davidyaha davidyaha Oct 2, 2016

Choose a reason for hiding this comment

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

There is no actual need for this trigger constant anymore

@davidyaha
Copy link
Contributor

davidyaha commented Oct 2, 2016

Oh also, it might be a good idea to create an inteface for the returned object on the trigger function.
something like:

interface TriggerConfig {
  channelOptions?: Object
  filter?: Function
}

@quintstoffers
Copy link
Contributor Author

@davidyaha: Thanks for your input! I removed the unnecessary variable and defined setupFunctions and children using Interfaces.

@davidyaha
Copy link
Contributor

You'r very welcome! I saw you decided not to use the es6 style default values, is it on purpose?

Thanks for your work so far! I would like to use the improved API :)

@quintstoffers
Copy link
Contributor Author

quintstoffers commented Oct 3, 2016

I'm not a big fan of the default values when destructuring because I often find the default values are too complex and I end up with a lump of hard to read code. I didn't give it a good thought for that reason, but it does look perfectly fine in hindsight:

// Deconstruct the trigger options and set any defaults
const {
    channelOptions = {},
    filter = () => true,
} = triggerMap[triggerName];

As a bonus the variables can now be const, which is nice. I'll update the PR in a minute!

@quintstoffers
Copy link
Contributor Author

@helfer: I think were done here 😄 .

@helfer
Copy link
Contributor

helfer commented Oct 3, 2016

Thanks a lot! @quintstoffers @davidyaha

@helfer helfer merged commit cd691b8 into apollographql:master Oct 3, 2016
@glasser
Copy link
Member

glasser commented Dec 21, 2016

FYI, GitHunt-API (the best way to understand how to use Apollo and graphql-subscriptions) has not yet been updated with this API change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants