Skip to content

Commit

Permalink
Merge pull request #125 from ColeWalker/feature/search-for-streams
Browse files Browse the repository at this point in the history
Feature/search for streams
  • Loading branch information
ColeWalker authored Sep 20, 2020
2 parents 031ffad + a0eb1a7 commit 519d3ba
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 6 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,30 @@ type Stream {
userDisplayName: String!
userId: String!
}

type StreamConnection {
total: Int!
nodes: [Stream]
cursor: String
}

extend type Query {
getStreams(streamFilter: StreamFilter!, maxPages: Int = 1): StreamConnection
}

input StreamFilter {
gameIds: [String]
gameNames: [String]
languages: [String]
type: StreamType
userIds: [String]
userNames: [String]
}

enum StreamType {
live
none
}
```

### StreamUserLink
Expand Down
44 changes: 44 additions & 0 deletions src/schema/stream-type-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,48 @@ describe('StreamModule', () => {
const stream = result?.data?.latestSub?.user?.stream
expect(stream).toMatchObject(expectedStream)
})

it('getStreams', async () => {
const app = createApplication({
modules: [
QueryModule,
SubscriberModule,
UserModule,
UserSubscriberLinkModule,
StreamModule,
StreamUserLinkModule,
],
})
const schema = app.createSchemaForApollo()

const document = parse(`
{
getStreams(
streamFilter: {
userNames: ["supcole"]
}
) {
nodes {
language
gameId
id
title
viewers
thumbnailUrl
userDisplayName
userId
}
}
}
`)
const contextValue = { request: {}, response: {} }
const result = await execute({
schema,
contextValue,
document,
})
expect(result?.errors?.length).toBeFalsy()
const stream = result?.data?.getStreams?.nodes
expect(stream).toMatchObject([expectedStream])
})
})
73 changes: 72 additions & 1 deletion src/schema/stream-type-schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,53 @@
import { createModule, gql } from 'graphql-modules'
import { HelixStream } from 'twitch/lib'
import { HelixStream } from 'twitch'
import { HelixStreamFilter } from 'twitch/lib/API/Helix/Stream/HelixStreamApi'
import { TwitchClients } from '../injections/Twitch-Clients'
import { TwitchId } from '../injections/Twitch-Id'
import { UserId } from '../injections/User-Id'

export const StreamResolvers = {
Query: {
async getStreams(
_parent: unknown,
args: { streamFilter: any; maxPages: number },
{ injector }: GraphQLModules.ModuleContext
) {
const clients = injector.get(TwitchClients)
const apiClient = await clients.apiClient()
let gameIds: string[] = []

if (args?.streamFilter?.gameNames?.length) {
gameIds = (
await apiClient.helix.games.getGamesByNames(
args.streamFilter.gameNames
)
)?.map((x) => x.id)
}

const streamFilter: HelixStreamFilter = {
...args.streamFilter,
game: args.streamFilter?.gameIds
? [...args.streamFilter?.gameIds, ...gameIds]
: [...gameIds],
}

const page = apiClient.helix.streams.getStreamsPaginated(streamFilter)

let pages = []
if (page.current) pages.push(...page.current)

for (let i = 1; i <= args?.maxPages; i++) {
await page.getNext()
if (page.current) pages.push(...page.current)
}

return {
nodes: pages.map((el) => new HelixStream(el, apiClient)),
cursor: page.currentCursor,
total: pages?.length,
}
},
},
Stream: {
language(stream: HelixStream) {
return stream.language
Expand Down Expand Up @@ -41,11 +87,36 @@ export const StreamSchema = gql`
userDisplayName: String!
userId: String!
}
type StreamConnection {
total: Int!
nodes: [Stream]
cursor: String
}
extend type Query {
getStreams(streamFilter: StreamFilter!, maxPages: Int = 1): StreamConnection
}
input StreamFilter {
gameIds: [String]
gameNames: [String]
languages: [String]
type: StreamType
userIds: [String]
userNames: [String]
}
enum StreamType {
live
none
}
`

export const StreamModule = createModule({
id: `stream-module`,
dirname: __dirname,
providers: [TwitchClients, TwitchId, UserId],
typeDefs: StreamSchema,
resolvers: StreamResolvers,
})
10 changes: 5 additions & 5 deletions src/schema/user-type-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const UserResolvers = {
args: { displayName: string },
{ injector }: GraphQLModules.ModuleContext
) {
const clients = await injector.get(TwitchClients)
const clients = injector.get(TwitchClients)
const apiClient = await clients.apiClient()

return apiClient.helix.users.getUserByName(args.displayName)
Expand All @@ -21,7 +21,7 @@ export const UserResolvers = {
args: { userId: string },
{ injector }: GraphQLModules.ModuleContext
) {
const clients = await injector.get(TwitchClients)
const clients = injector.get(TwitchClients)
const apiClient = await clients.apiClient()

return apiClient.helix.users.getUserById(args.userId)
Expand Down Expand Up @@ -52,7 +52,7 @@ export const UserResolvers = {
args: { displayName: string },
{ injector }: GraphQLModules.ModuleContext
) {
const clients = await injector.get(TwitchClients)
const clients = injector.get(TwitchClients)
const apiClient = await clients.apiClient()

const followed = await apiClient.helix.users.getUserByName(
Expand All @@ -69,7 +69,7 @@ export const UserResolvers = {
args: { displayName: string },
{ injector }: GraphQLModules.ModuleContext
) {
const clients = await injector.get(TwitchClients)
const clients = injector.get(TwitchClients)
const apiClient = await clients.apiClient()

const followed = await apiClient.helix.users.getUserByName(
Expand All @@ -83,7 +83,7 @@ export const UserResolvers = {
args: { maxPages: number },
{ injector }: GraphQLModules.ModuleContext
) {
const clients = await injector.get(TwitchClients)
const clients = injector.get(TwitchClients)
const apiClient = await clients.apiClient()
const page = await apiClient.helix.users.getFollowsPaginated({
user: user,
Expand Down

0 comments on commit 519d3ba

Please sign in to comment.