Skip to content

Commit

Permalink
feat: ⚗️ Add experimental feature for chat pubsubs
Browse files Browse the repository at this point in the history
  • Loading branch information
ColeWalker committed Oct 4, 2020
1 parent 8041df9 commit 31e1ff7
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ By default it will run at `http://localhost:5555/graphql`.
- [PubSubs](#pubsubs)
- [RedemptionPubSub](#redemptionpubsub)
- [RedemptionUserLink](#redemptionuserlink)
- [ChatPubSub](#chatpubsub)
- [ChatUserLink](#chatuserlink)

## Environment Variables

Expand Down Expand Up @@ -374,3 +376,33 @@ extend type Redemption {
channelRedeemedAt: User
}
```

### ChatPubSub

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

```graphql
type Chat {
message: String
displayName: String
channel: String
}

extend type Subscription {
newChat(channel: String!): Chat
}
```

### ChatUserLink

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

```graphql
extend type Chat {
user: User
}
```
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,13 @@ export {
RedemptionUserLinkResolvers,
RedemptionUserLinkSchema,
} from './schema/redemption-pubsub-user-link-type-schema'
export {
ChatPubSubModule,
ChatPubSubResolvers,
ChatPubSubSchema,
} from './schema/chat-pubsub-type-schema'
export {
ChatUserLinkModule,
ChatUserLinkResolvers,
ChatUserLinkSchema,
} from './schema/chat-pubsub-user-link-schema'
4 changes: 4 additions & 0 deletions src/injections/Twitch-Clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ export class TwitchClients {
async pubSubClient() {
return new PubSubClient()
}

async authProvider() {
return RefreshToken()
}
}
14 changes: 14 additions & 0 deletions src/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ import {
RedemptionUserLinkModule,
RedemptionUserLinkResolvers,
RedemptionUserLinkSchema,
ChatPubSubModule,
ChatPubSubResolvers,
ChatPubSubSchema,
ChatUserLinkModule,
ChatUserLinkResolvers,
ChatUserLinkSchema,
} from './index'
import nock from 'nock'
import {
Expand Down Expand Up @@ -101,6 +107,12 @@ describe('npm package', () => {
expect(RedemptionPubSubModule).toBeTruthy()
expect(RedemptionPubSubResolvers).toBeTruthy()
expect(RedemptionPubSubSchema).toBeTruthy()
expect(ChatPubSubModule).toBeTruthy()
expect(ChatPubSubResolvers).toBeTruthy()
expect(ChatPubSubSchema).toBeTruthy()
expect(ChatUserLinkModule).toBeTruthy()
expect(ChatUserLinkResolvers).toBeTruthy()
expect(ChatUserLinkSchema).toBeTruthy()
})

it('modules should work together', async () => {
Expand All @@ -116,6 +128,8 @@ describe('npm package', () => {
StreamUserLinkModule,
RedemptionPubSubModule,
RedemptionUserLinkModule,
ChatPubSubModule,
ChatUserLinkModule,
],
})
const schema = app.createSchemaForApollo()
Expand Down
63 changes: 63 additions & 0 deletions src/schema/chat-pubsub-type-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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'
import { ChatClient } from 'twitch-chat-client'

export interface Chat {
channel: string
displayName: string
message: string
}

export const ChatPubSubResolvers = {
Subscription: {
newChat: {
subscribe: async (
_: any,
args: { channel: string },
{ injector }: GraphQLModules.Context
) => {
const clients = injector.get(TwitchClients)

const chatClient = new ChatClient(await clients.authProvider(), {
channels: [args.channel],
})
await chatClient.connect()

const curriedOnChat = async (cb: any) =>
chatClient.onMessage(async (channel, user, message) => {
return cb({ channel, displayName: user, message })
})

const asyncified = asyncify(curriedOnChat)

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

export const ChatPubSubSchema = gql`
type Chat {
message: String
displayName: String
channel: String
}
extend type Subscription {
newChat(channel: String!): Chat
}
`

export const ChatPubSubModule = createModule({
id: `chat-pubsub-module`,
dirname: __dirname,
providers: [TwitchClients, TwitchId, UserId],
typeDefs: ChatPubSubSchema,
resolvers: ChatPubSubResolvers,
})
34 changes: 34 additions & 0 deletions src/schema/chat-pubsub-user-link-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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 { Chat } from './chat-pubsub-type-schema'

export const ChatUserLinkResolvers = {
Chat: {
user: async (
chat: Chat,
_args: any,
{ injector }: GraphQLModules.Context
) => {
const clients = injector.get(TwitchClients)
const twitchClient = await clients.apiClient()

return twitchClient.helix.users.getUserByName(chat.displayName)
},
},
}

export const ChatUserLinkSchema = gql`
extend type Chat {
user: User
}
`

export const ChatUserLinkModule = createModule({
id: `chat-pubsub-user-link-module`,
dirname: __dirname,
providers: [TwitchClients, TwitchId, UserId],
typeDefs: ChatUserLinkSchema,
resolvers: ChatUserLinkResolvers,
})
4 changes: 4 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import { GameStreamLinkModule } from './schema/game-stream-link-type-schema'
import { RedemptionPubSubModule } from './schema/redemption-pubsub-type-schema'
import { StreamUserLinkModule } from './schema/stream-user-link-type-schema'
import { RedemptionUserLinkModule } from './schema/redemption-pubsub-user-link-type-schema'
import { ChatPubSubModule } from './schema/chat-pubsub-type-schema'
import { ApolloServer } from 'apollo-server-express'
import { ChatUserLinkModule } from './schema/chat-pubsub-user-link-schema'
require('dotenv').config()

let port = 5555
Expand Down Expand Up @@ -56,6 +58,8 @@ const app = createApplication({
StreamUserLinkModule,
RedemptionPubSubModule,
RedemptionUserLinkModule,
ChatPubSubModule,
ChatUserLinkModule,
],
})

Expand Down

0 comments on commit 31e1ff7

Please sign in to comment.