Skip to content

Commit

Permalink
Merge pull request #139 from ColeWalker/feature/add-bits-pubsubs
Browse files Browse the repository at this point in the history
Feature/add bits pubsubs
  • Loading branch information
ColeWalker authored Oct 4, 2020
2 parents 705e028 + 0c5e6c0 commit f861756
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 5 deletions.
41 changes: 38 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ By default it will run at `http://localhost:5555/graphql`.
- [RedemptionUserLink](#redemptionuserlink)
- [ChatPubSub](#chatpubsub)
- [ChatUserLink](#chatuserlink)
- [BitPubSub](#bitpubsub)
- [BitUserLink](#bituserlink)

## Environment Variables

Expand Down Expand Up @@ -366,7 +368,7 @@ extend type Subscription {

### RedemptionUserLink

```**ts**
```ts
import { RedemptionUserLinkModule } from 'twitch-graphql'
```

Expand All @@ -379,7 +381,7 @@ extend type Redemption {

### ChatPubSub

```**ts**
```ts
import { ChatPubSubModule } from 'twitch-graphql'
```

Expand All @@ -397,7 +399,7 @@ extend type Subscription {

### ChatUserLink

```**ts**
```ts
import { ChatUserLinkModule } from 'twitch-graphql'
```

Expand All @@ -406,3 +408,36 @@ extend type Chat {
user: User
}
```

### BitPubSub

```ts
import { BitPubSubModule } from 'twitch-graphql'
```

```graphql
type Bits {
userId: String
userName: String
message: String
bits: Int
totalBits: Int
isAnonymous: Boolean
}

extend type Subscription {
newBits: Bits
}
```

### BitUserLink

```ts
import { BitPubSubModule } from 'twitch-graphql'
```

```graphql
extend type Bit {
user: User
}
```
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,13 @@ export {
ChatUserLinkResolvers,
ChatUserLinkSchema,
} from './schema/chat-pubsub-user-link-schema'
export {
BitPubSubModule,
BitPubSubResolvers,
BitPubSubSchema,
} from './schema/bit-pubsub-type-schema'
export {
BitUserLinkModule,
BitUserLinkResolvers,
BitUserLinkSchema,
} from './schema/bit-pubsub-user-link-schema'
14 changes: 14 additions & 0 deletions src/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ import {
ChatUserLinkModule,
ChatUserLinkResolvers,
ChatUserLinkSchema,
BitPubSubModule,
BitPubSubResolvers,
BitPubSubSchema,
BitUserLinkModule,
BitUserLinkResolvers,
BitUserLinkSchema,
} from './index'
import nock from 'nock'
import {
Expand Down Expand Up @@ -113,6 +119,12 @@ describe('npm package', () => {
expect(ChatUserLinkModule).toBeTruthy()
expect(ChatUserLinkResolvers).toBeTruthy()
expect(ChatUserLinkSchema).toBeTruthy()
expect(BitPubSubModule).toBeTruthy()
expect(BitPubSubResolvers).toBeTruthy()
expect(BitPubSubSchema).toBeTruthy()
expect(BitUserLinkModule).toBeTruthy()
expect(BitUserLinkResolvers).toBeTruthy()
expect(BitUserLinkSchema).toBeTruthy()
})

it('modules should work together', async () => {
Expand All @@ -130,6 +142,8 @@ describe('npm package', () => {
RedemptionUserLinkModule,
ChatPubSubModule,
ChatUserLinkModule,
BitPubSubModule,
BitUserLinkModule,
],
})
const schema = app.createSchemaForApollo()
Expand Down
56 changes: 56 additions & 0 deletions src/schema/bit-pubsub-type-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { createModule, gql } from 'graphql-modules'
import { TwitchClients } from '../injections/Twitch-Clients'
import { TwitchId } from '../injections/Twitch-Id'
import { UserId } from '../injections/User-Id'
import asyncify from 'callback-to-async-iterator'

export const BitPubSubResolvers = {
Subscription: {
newBits: {
subscribe: async (
_: any,
_args: any,
{ injector }: GraphQLModules.Context
) => {
const clients = injector.get(TwitchClients)

const twitchClient = await clients.apiClient()
const myId = (await twitchClient.getTokenInfo()).userId
const pubSubClient = await clients.pubSubClient()
await pubSubClient.registerUserListener(twitchClient)

const curriedOnBits = (cb: any) => pubSubClient.onBits(myId, cb)

const asyncified = asyncify(curriedOnBits)

return asyncified
},
resolve: (redemption: any) => {
return redemption
},
},
},
}

export const BitPubSubSchema = gql`
type Bit {
userId: String
userName: String
message: String
bits: Int
totalBits: Int
isAnonymous: Boolean
}
extend type Subscription {
newBits: Bit
}
`

export const BitPubSubModule = createModule({
id: `bit-pubsub-module`,
dirname: __dirname,
providers: [TwitchClients, TwitchId, UserId],
typeDefs: BitPubSubSchema,
resolvers: BitPubSubResolvers,
})
33 changes: 33 additions & 0 deletions src/schema/bit-pubsub-user-link-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createModule, gql } from 'graphql-modules'
import { TwitchClients } from '../injections/Twitch-Clients'
import { TwitchId } from '../injections/Twitch-Id'
import { UserId } from '../injections/User-Id'
import { PubSubBitsMessage } from 'twitch-pubsub-client'
export const BitUserLinkResolvers = {
Bit: {
user: async (
bit: PubSubBitsMessage,
_args: any,
{ injector }: GraphQLModules.Context
) => {
const clients = injector.get(TwitchClients)
const twitchClient = await clients.apiClient()

return twitchClient.helix.users.getUserById(bit.userId || '')
},
},
}

export const BitUserLinkSchema = gql`
extend type Bit {
user: User
}
`

export const BitUserLinkModule = createModule({
id: `bit-pubsub-user-link-module`,
dirname: __dirname,
providers: [TwitchClients, TwitchId, UserId],
typeDefs: BitUserLinkSchema,
resolvers: BitUserLinkResolvers,
})
4 changes: 2 additions & 2 deletions src/schema/redemption-pubsub-type-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export const RedemptionPubSubResolvers = {
const clients = injector.get(TwitchClients)

const twitchClient = await clients.apiClient()

const myId = (await twitchClient.getTokenInfo()).userId
const pubSubClient = await clients.pubSubClient()
await pubSubClient.registerUserListener(twitchClient)
const curriedOnRedemption = (cb: any) =>
pubSubClient.onRedemption('23573216', cb)
pubSubClient.onRedemption(myId, cb)

const asyncified = asyncify(curriedOnRedemption)

Expand Down
4 changes: 4 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { RedemptionUserLinkModule } from './schema/redemption-pubsub-user-link-t
import { ChatPubSubModule } from './schema/chat-pubsub-type-schema'
import { ApolloServer } from 'apollo-server-express'
import { ChatUserLinkModule } from './schema/chat-pubsub-user-link-schema'
import { BitPubSubModule } from './schema/bit-pubsub-type-schema'
import { BitUserLinkModule } from './schema/bit-pubsub-user-link-schema'
require('dotenv').config()

let port = 5555
Expand Down Expand Up @@ -60,6 +62,8 @@ const app = createApplication({
RedemptionUserLinkModule,
ChatPubSubModule,
ChatUserLinkModule,
BitPubSubModule,
BitUserLinkModule,
],
})

Expand Down

0 comments on commit f861756

Please sign in to comment.