Skip to content

Commit

Permalink
Merge pull request #106 from ColeWalker/feature/add-link-modules
Browse files Browse the repository at this point in the history
Feature/add link modules
  • Loading branch information
ColeWalker authored Sep 15, 2020
2 parents 58193ad + e667de1 commit 7951aa6
Show file tree
Hide file tree
Showing 17 changed files with 406 additions and 52 deletions.
43 changes: 36 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ import { SubscriberModule } from 'twitch-graphql'
import { UserModule } from 'twitch-graphql'
import { StreamModule } from 'twitch-graphql'
import { GameModule } from 'twitch-graphql'
import { UserSubscriberLinkModule } from 'twitch-graphql'
import { StreamUserLinkModule } from 'twitch-graphql'
import { GameStreamLinkModule } from 'twitch-graphql'
import { createApplication } from 'graphql-modules'

const port = 5555
Expand All @@ -52,6 +55,9 @@ const app = createApplication({
UserModule,
StreamModule,
GameModule,
UserSubscriberLinkModule,
StreamUserLinkModule,
GameStreamLinkModule,
],
})
const execute = app.createExecution()
Expand Down Expand Up @@ -108,7 +114,7 @@ Contributors should be using [prettier](https://prettier.io/) to automatically f

To contribute please follow these steps:

1. Clone the repository: ` git clone https://github.com/ColeWalker/twitch-graphql-server`
1. Clone the repository: `git clone https://github.com/ColeWalker/twitch-graphql-server`
2. Create a new branch named after the issue that you're going to fix. Prefix branch name with a Conventional Commits type. Example:
`git checkout -b feature/add-port-config`
3. Write code and write tests for your code. We use jest and ts-jest. If your code is not properly covered by tests, it will be rejected. Since this is a wrapper for an external API, our tests cannot be thorough, however, you should provide as much coverage as possible. Follow any .test.ts file for examples.
Expand Down Expand Up @@ -204,16 +210,22 @@ type FollowConnection {
cursor: String
}

extend type Subscriber {
user: User!
}

extend type Query {
getUserById(userId: String!): User
getUserByDisplayName(displayName: String!): User
}
```

### UserSubscriberLink

This module extends Subscriber to add the user field. Only use if both modules are being used in your application.

```graphql
extend type Subscriber {
user: User!
}
```

### Stream

```graphql
Expand All @@ -226,13 +238,20 @@ type Stream {
thumbnailUrl: String!
userDisplayName: String!
userId: String!

user: User
}
```

### StreamUserLink

This module extends Stream to add the user field, and User to add the stream field. Only use if both modules are being used in your application.

```graphql
extend type User {
stream: Stream
}
extend type Stream {
user: User
}
```

### Game
Expand All @@ -244,6 +263,16 @@ type Game {
name: String!
}

extend type Query {
getGameByName(gameName: String!): Game
}
```

### GameStreamLink

This module extends Stream to add the game field. Only use if both modules are being used in your application.

```graphql
extend type Stream {
game: Game
}
Expand Down
15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,18 @@ export {
QueryResolvers,
QuerySchema,
} from './schema/query-type-schema'
export {
UserSubscriberLinkModule,
UserSubscriberLinkResolvers,
UserSubscriberLinkSchema,
} from './schema/user-subscriber-link-type-schema'
export {
GameStreamLinkModule,
GameStreamLinkResolvers,
GameStreamLinkSchema,
} from './schema/game-stream-link-type-schema'
export {
StreamUserLinkModule,
StreamUserLinkResolvers,
StreamUserLinkSchema,
} from './schema/stream-user-link-type-schema'
21 changes: 21 additions & 0 deletions src/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ import {
QueryModule,
QueryResolvers,
QuerySchema,
UserSubscriberLinkModule,
UserSubscriberLinkResolvers,
UserSubscriberLinkSchema,
GameStreamLinkModule,
GameStreamLinkResolvers,
GameStreamLinkSchema,
StreamUserLinkModule,
StreamUserLinkResolvers,
StreamUserLinkSchema,
} from './index'

describe('npm package', () => {
Expand All @@ -35,6 +44,15 @@ describe('npm package', () => {
expect(QueryModule).toBeTruthy()
expect(QueryResolvers).toBeTruthy()
expect(QuerySchema).toBeTruthy()
expect(UserSubscriberLinkModule).toBeTruthy()
expect(UserSubscriberLinkResolvers).toBeTruthy()
expect(UserSubscriberLinkSchema).toBeTruthy()
expect(GameStreamLinkModule).toBeTruthy()
expect(GameStreamLinkResolvers).toBeTruthy()
expect(GameStreamLinkSchema).toBeTruthy()
expect(StreamUserLinkModule).toBeTruthy()
expect(StreamUserLinkResolvers).toBeTruthy()
expect(StreamUserLinkSchema).toBeTruthy()
})

it('modules should work together', async () => {
Expand All @@ -44,7 +62,10 @@ describe('npm package', () => {
SubscriberModule,
UserModule,
StreamModule,
UserSubscriberLinkModule,
GameStreamLinkModule,
GameModule,
StreamUserLinkModule,
],
})
const schema = app.createSchemaForApollo()
Expand Down
59 changes: 59 additions & 0 deletions src/schema/game-stream-link-type-schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { createApplication } from 'graphql-modules'
import { SubscriberModule } from './subscriber-type-schema'
import { UserModule } from './user-type-schema'
import { StreamModule } from './stream-type-schema'
import { GameModule } from './game-type-schema'
import { parse, execute } from 'graphql'
import { QueryModule } from './query-type-schema'
import { UserSubscriberLinkModule } from './user-subscriber-link-type-schema'
import { GameStreamLinkModule } from './game-stream-link-type-schema'

describe('GameStreamLinkModule', () => {
it('game should have all fields', async () => {
const app = createApplication({
modules: [
QueryModule,
SubscriberModule,
UserModule,
UserSubscriberLinkModule,
StreamModule,
GameStreamLinkModule,
GameModule,
],
})
const schema = app.createSchemaForApollo()

const document = parse(`
{
latestSub {
user{
displayName
stream {
game {
id
boxArtUrl
name
}
}
}
}
}
`)
const contextValue = { request: {}, response: {} }
const result = await execute({
schema,
contextValue,
document,
})

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

const game = result?.data?.latestSub?.user?.stream?.game

if (game) {
expect(game).toHaveProperty('boxArtUrl')
expect(game).toHaveProperty('name')
expect(game).toHaveProperty('id')
}
})
})
27 changes: 27 additions & 0 deletions src/schema/game-stream-link-type-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { gql, createModule } from 'graphql-modules'
import { HelixStream } from 'twitch'
import { TwitchClients } from '../injections/Twitch-Clients'
import { TwitchId } from '../injections/Twitch-Id'
import { UserId } from '../injections/User-Id'

export const GameStreamLinkResolvers = {
Stream: {
async game(stream: HelixStream) {
return await stream.getGame()
},
},
}

export const GameStreamLinkSchema = gql`
extend type Stream {
game: Game
}
`

export const GameStreamLinkModule = createModule({
id: `game-stream-link-module`,
dirname: __dirname,
providers: [TwitchClients, TwitchId, UserId],
typeDefs: GameStreamLinkSchema,
resolvers: GameStreamLinkResolvers,
})
6 changes: 6 additions & 0 deletions src/schema/game-type-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import { StreamModule } from './stream-type-schema'
import { GameModule } from './game-type-schema'
import { parse, execute } from 'graphql'
import { QueryModule } from './query-type-schema'
import { UserSubscriberLinkModule } from './user-subscriber-link-type-schema'
import { GameStreamLinkModule } from './game-stream-link-type-schema'
describe('GameModule', () => {
it('game should have all fields', async () => {
const app = createApplication({
modules: [
QueryModule,
SubscriberModule,
UserModule,
UserSubscriberLinkModule,
GameStreamLinkModule,
StreamModule,
GameModule,
],
Expand All @@ -22,6 +26,7 @@ describe('GameModule', () => {
{
latestSub {
user{
displayName
stream {
game {
id
Expand Down Expand Up @@ -56,6 +61,7 @@ describe('GameModule', () => {
SubscriberModule,
UserModule,
StreamModule,
UserSubscriberLinkModule,
GameModule,
],
})
Expand Down
11 changes: 1 addition & 10 deletions src/schema/game-type-schema.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { createModule, gql } from 'graphql-modules'
import { HelixStream, HelixGame } from 'twitch/lib'
import { HelixGame } from 'twitch/lib'
import { TwitchClients } from '../injections/Twitch-Clients'
import { TwitchId } from '../injections/Twitch-Id'
import { UserId } from '../injections/User-Id'

export const GameResolvers = {
Stream: {
async game(stream: HelixStream) {
return await stream.getGame()
},
},
Query: {
async getGameByName(
_parent: {},
Expand Down Expand Up @@ -42,10 +37,6 @@ export const GameSchema = gql`
name: String!
}
extend type Stream {
game: Game
}
extend type Query {
getGameByName(gameName: String!): Game
}
Expand Down
12 changes: 11 additions & 1 deletion src/schema/stream-type-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@ import { UserModule } from './user-type-schema'
import { StreamModule } from './stream-type-schema'
import { parse, execute } from 'graphql'
import { QueryModule } from './query-type-schema'
import { UserSubscriberLinkModule } from './user-subscriber-link-type-schema'
import { StreamUserLinkModule } from './stream-user-link-type-schema'
describe('StreamModule', () => {
it('stream should have all fields', async () => {
const app = createApplication({
modules: [QueryModule, SubscriberModule, UserModule, StreamModule],
modules: [
QueryModule,
SubscriberModule,
UserModule,
UserSubscriberLinkModule,
StreamModule,
StreamUserLinkModule,
],
})
const schema = app.createSchemaForApollo()

const document = parse(`
{
latestSub {
user{
displayName
stream {
language
gameId
Expand Down
16 changes: 1 addition & 15 deletions src/schema/stream-type-schema.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { createModule, gql } from 'graphql-modules'
import { HelixUser, HelixStream } from 'twitch/lib'
import { HelixStream } from 'twitch/lib'

export const StreamResolvers = {
User: {
async stream(user: HelixUser) {
return await user.getStream()
},
},
Stream: {
language(stream: HelixStream) {
return stream.language
Expand All @@ -32,9 +27,6 @@ export const StreamResolvers = {
userId(stream: HelixStream) {
return stream.userId
},
async user(stream: HelixStream) {
return await stream.getUser()
},
},
}

Expand All @@ -48,12 +40,6 @@ export const StreamSchema = gql`
thumbnailUrl: String!
userDisplayName: String!
userId: String!
user: User
}
extend type User {
stream: Stream
}
`

Expand Down
Loading

0 comments on commit 7951aa6

Please sign in to comment.