diff --git a/README.md b/README.md index 7a48274..9fc64f5 100644 --- a/README.md +++ b/README.md @@ -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 @@ -366,7 +368,7 @@ extend type Subscription { ### RedemptionUserLink -```**ts** +```ts import { RedemptionUserLinkModule } from 'twitch-graphql' ``` @@ -379,7 +381,7 @@ extend type Redemption { ### ChatPubSub -```**ts** +```ts import { ChatPubSubModule } from 'twitch-graphql' ``` @@ -397,7 +399,7 @@ extend type Subscription { ### ChatUserLink -```**ts** +```ts import { ChatUserLinkModule } from 'twitch-graphql' ``` @@ -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 +} +``` diff --git a/src/index.ts b/src/index.ts index 1c99fb1..c2670fc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' diff --git a/src/package.test.ts b/src/package.test.ts index 0a5701c..2dc9767 100644 --- a/src/package.test.ts +++ b/src/package.test.ts @@ -37,6 +37,12 @@ import { ChatUserLinkModule, ChatUserLinkResolvers, ChatUserLinkSchema, + BitPubSubModule, + BitPubSubResolvers, + BitPubSubSchema, + BitUserLinkModule, + BitUserLinkResolvers, + BitUserLinkSchema, } from './index' import nock from 'nock' import { @@ -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 () => { @@ -130,6 +142,8 @@ describe('npm package', () => { RedemptionUserLinkModule, ChatPubSubModule, ChatUserLinkModule, + BitPubSubModule, + BitUserLinkModule, ], }) const schema = app.createSchemaForApollo() diff --git a/src/schema/bit-pubsub-type-schema.ts b/src/schema/bit-pubsub-type-schema.ts new file mode 100644 index 0000000..7f6f09e --- /dev/null +++ b/src/schema/bit-pubsub-type-schema.ts @@ -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, +}) diff --git a/src/schema/bit-pubsub-user-link-schema.ts b/src/schema/bit-pubsub-user-link-schema.ts new file mode 100644 index 0000000..d54c67f --- /dev/null +++ b/src/schema/bit-pubsub-user-link-schema.ts @@ -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, +}) diff --git a/src/schema/redemption-pubsub-type-schema.ts b/src/schema/redemption-pubsub-type-schema.ts index 0200e90..2b09f16 100644 --- a/src/schema/redemption-pubsub-type-schema.ts +++ b/src/schema/redemption-pubsub-type-schema.ts @@ -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) diff --git a/src/server.ts b/src/server.ts index cb648a7..12bc9c2 100644 --- a/src/server.ts +++ b/src/server.ts @@ -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 @@ -60,6 +62,8 @@ const app = createApplication({ RedemptionUserLinkModule, ChatPubSubModule, ChatUserLinkModule, + BitPubSubModule, + BitUserLinkModule, ], })