diff --git a/src/schema/user-type-schema.test.ts b/src/schema/user-type-schema.test.ts index 3ccd5c9..7fbe999 100644 --- a/src/schema/user-type-schema.test.ts +++ b/src/schema/user-type-schema.test.ts @@ -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({ @@ -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 () => { @@ -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') + }) }) diff --git a/src/schema/user-type-schema.ts b/src/schema/user-type-schema.ts index 39515cd..f5ac590 100644 --- a/src/schema/user-type-schema.ts +++ b/src/schema/user-type-schema.ts @@ -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' @@ -34,6 +34,7 @@ export const UserResolvers = { }, User: { displayName(user: HelixUser) { + HelixFollow return user.displayName }, description(user: HelixUser) { @@ -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() + }, }, } @@ -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 { diff --git a/src/server.ts b/src/server.ts index 3c46fb4..b3b44a5 100644 --- a/src/server.ts +++ b/src/server.ts @@ -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' @@ -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()