Skip to content

Commit

Permalink
Merge pull request #93 from ColeWalker/feature/add-follows
Browse files Browse the repository at this point in the history
Feature/add follows
  • Loading branch information
ColeWalker authored Sep 13, 2020
2 parents f6c1f13 + d7b5c4b commit af9e01e
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 17 deletions.
177 changes: 162 additions & 15 deletions src/schema/user-type-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ describe('UserModule', () => {
})

expect(result?.errors?.length).toBeFalsy()
expect(result?.data?.latestSub?.user).toBeTruthy()
expect(result?.data?.latestSub?.user).toHaveProperty('displayName')
expect(result?.data?.latestSub?.user).toHaveProperty('id')
expect(result?.data?.latestSub?.user).toHaveProperty('profilePictureURL')
expect(result?.data?.latestSub?.user).toHaveProperty('views')
expect(result?.data?.latestSub?.user).toHaveProperty('description')
const user = result?.data?.latestSub?.user
expect(user).toBeTruthy()
expect(user).toHaveProperty('displayName')
expect(user).toHaveProperty('id')
expect(user).toHaveProperty('profilePictureURL')
expect(user).toHaveProperty('views')
expect(user).toHaveProperty('description')
})
it('getUserById should work', async () => {
const app = createApplication({
Expand Down Expand Up @@ -73,11 +74,7 @@ describe('UserModule', () => {
expect(user).toHaveProperty('profilePictureURL')
expect(user).toHaveProperty('views')

expect(user).toHaveProperty('id')
expect(user.id).toEqual(userId)

expect(user).toHaveProperty('displayName')
expect(user.displayName).toEqual(displayName)
expect(user).toMatchObject({ id: userId, displayName })
}
})
it('getUserByDisplayName should work', async () => {
Expand Down Expand Up @@ -114,11 +111,161 @@ describe('UserModule', () => {
expect(user).toHaveProperty('profilePictureURL')
expect(user).toHaveProperty('views')

expect(user).toHaveProperty('id')
expect(user.id).toEqual(userId)
expect(user).toMatchObject({ id: userId, displayName })
}
})
it('getFollowToId should work', async () => {
const app = createApplication({
modules: [QueryModule, SubscriberModule, UserModule],
})
const schema = app.createSchemaForApollo()

const userId = '23573216'

const document = parse(`
{
latestSub {
user {
displayName
getFollowToId(userId: "${userId}"){
followDate
followDateUTC
followerUser{
displayName
}
followedUser{
displayName
}
}
}
}
}
`)
const contextValue = { request: {}, response: {} }
const result = await execute({
schema,
contextValue,
document,
})

expect(result?.errors?.length).toBeFalsy()

const follow = result?.data?.latestSub?.user?.getFollowTo
if (follow) {
expect(follow).toHaveProperty('followDate')
expect(follow).toHaveProperty('followDateUTC')

expect(follow).toHaveProperty('followerUser')
expect(follow.followerUser).toHaveProperty('displayName')

expect(user).toHaveProperty('displayName')
expect(user.displayName).toEqual(displayName)
expect(follow).toHaveProperty('followedUser')
expect(follow.followedUser).toHaveProperty('displayName')
}
})
it('getFollowToDisplayName should work', async () => {
const app = createApplication({
modules: [QueryModule, SubscriberModule, UserModule],
})
const schema = app.createSchemaForApollo()

const displayName = 'SupCole'

const document = parse(`
{
latestSub{
user {
displayName
getFollowToDisplayName(displayName: "${displayName}"){
followDate
followDateUTC
followerUser{
displayName
}
followedUser{
displayName
}
}
}
}
}
`)
const contextValue = { request: {}, response: {} }
const result = await execute({
schema,
contextValue,
document,
})
expect(result?.errors?.length).toBeFalsy()
const follow = result?.data?.latestSub?.user?.getFollowTo
if (follow) {
expect(follow).toHaveProperty('followDate')
expect(follow).toHaveProperty('followDateUTC')

expect(follow).toHaveProperty('followerUser')
expect(follow.followerUser).toHaveProperty('displayName')

expect(follow).toHaveProperty('followedUser')
expect(follow.followedUser).toHaveProperty('displayName')
}
})
it('followsId should work', async () => {
const app = createApplication({
modules: [QueryModule, SubscriberModule, UserModule],
})
const schema = app.createSchemaForApollo()

const userId = '23573216'

const document = parse(`
{
latestSub{
user {
displayName
followsId(userId: "${userId}")
}
}
}
`)
const contextValue = { request: {}, response: {} }
const result = await execute({
schema,
contextValue,
document,
})
expect(result?.errors?.length).toBeFalsy()
const follows = result?.data?.latestSub?.user?.followsId
expect(typeof follows).toBe('boolean')
})
it('followsDisplayName should work', async () => {
const app = createApplication({
modules: [QueryModule, SubscriberModule, UserModule],
})
const schema = app.createSchemaForApollo()

const displayName = 'SupCole'

const document = parse(`
{
latestSub{
user {
displayName
followsDisplayName(displayName: "${displayName}")
}
}
}
`)
const contextValue = { request: {}, response: {} }
const result = await execute({
schema,
contextValue,
document,
})
expect(result?.errors?.length).toBeFalsy()
const follows = result?.data?.latestSub?.user?.followsDisplayName
expect(typeof follows).toBe('boolean')
})
})
64 changes: 63 additions & 1 deletion src/schema/user-type-schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createModule, gql } from 'graphql-modules'
import { HelixSubscription, HelixUser } from 'twitch/lib'
import { HelixFollow, HelixSubscription, HelixUser } from 'twitch/lib'
import { TwitchClients } from '../injections/Twitch-Clients'
import { TwitchId } from '../injections/Twitch-Id'
import { UserId } from '../injections/User-Id'
Expand Down Expand Up @@ -34,6 +34,7 @@ export const UserResolvers = {
},
User: {
displayName(user: HelixUser) {
HelixFollow
return user.displayName
},
description(user: HelixUser) {
Expand All @@ -48,6 +49,54 @@ export const UserResolvers = {
views(user: HelixUser) {
return user.views
},
async getFollowToId(user: HelixUser, args: { userId: string }) {
return user.getFollowTo(args.userId)
},
async getFollowToDisplayName(
user: HelixUser,
args: { displayName: string },
{ injector }: GraphQLModules.ModuleContext
) {
const clients = await injector.get(TwitchClients)
const apiClient = await clients.apiClient()

const followed = await apiClient.helix.users.getUserByName(
args.displayName
)

return followed && user.getFollowTo(followed.id)
},
async followsId(user: HelixUser, args: { userId: string }) {
return user.follows(args.userId)
},
async followsDisplayName(
user: HelixUser,
args: { displayName: string },
{ injector }: GraphQLModules.ModuleContext
) {
const clients = await injector.get(TwitchClients)
const apiClient = await clients.apiClient()

const followed = await apiClient.helix.users.getUserByName(
args.displayName
)

return !!followed && user.follows(followed.id)
},
},
Follow: {
followDate(follow: HelixFollow) {
return follow.followDate.toDateString()
},
followDateUTC(follow: HelixFollow) {
return follow.followDate
},
async followerUser(follow: HelixFollow) {
return await follow.getUser()
},
async followedUser(follow: HelixFollow) {
return await follow.getFollowedUser()
},
},
}

Expand All @@ -58,6 +107,19 @@ export const UserSchema = gql`
id: String!
profilePictureURL: String!
views: Int!
getFollowToId(userId: String!): Follow
getFollowToDisplayName(displayName: String!): Follow
followsId(userId: String!): Boolean!
followsDisplayName(displayName: String!): Boolean!
}
type Follow {
followDateUTC: String!
followDate: String!
followerUser: User!
followedUser: User!
}
extend type Subscriber {
Expand Down
9 changes: 8 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from 'express'
import cors from 'cors'
import { graphqlHTTP } from 'express-graphql'
import { QueryModule } from './schema/query-type-schema'
import { SubscriberModule } from './schema/subscriber-type-schema'
import { UserModule } from './schema/user-type-schema'
import { StreamModule } from './schema/stream-type-schema'
Expand Down Expand Up @@ -38,7 +39,13 @@ if (
}

const app = createApplication({
modules: [SubscriberModule, UserModule, StreamModule, GameModule],
modules: [
QueryModule,
SubscriberModule,
UserModule,
StreamModule,
GameModule,
],
})
const execute = app.createExecution()
const server = express()
Expand Down

0 comments on commit af9e01e

Please sign in to comment.