Skip to content

Commit

Permalink
Merge pull request #84 from ColeWalker/feature/add-query-schema
Browse files Browse the repository at this point in the history
Feature/add query schema
  • Loading branch information
ColeWalker authored Sep 12, 2020
2 parents 6ee7713 + ed73a31 commit 3b5d007
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 15 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ or

Usage of the package depends on the structure of your application. If you choose to use [graphql-modules](https://graphql-modules.com/) (Recommended), you can import the modules that you require and add them to your application, complete with authentication.

Every module in this library depends on the base module, "Query". You will **_ALWAYS NEED TO IMPORT AND USE `QueryModule`_**

Example:

```ts
Expand All @@ -43,7 +45,13 @@ import { createApplication } from 'graphql-modules'

const port = 5555
const app = createApplication({
modules: [SubscriberModule, UserModule, StreamModule, GameModule],
modules: [
QueryModule,
SubscriberModule,
UserModule,
StreamModule,
GameModule,
],
})
const execute = app.createExecution()
const server = express()
Expand All @@ -70,6 +78,7 @@ Alternatively, you can import the schema and resolvers separately. However, sinc
Examples of importing Schema and Resolvers:

```ts
import { QuerySchema, QueryResolver } from 'twitch-graphql'
import { StreamSchema, StreamResolvers } from 'twitch-graphql'
import { SubscriberSchema, SubscriberResolvers } from 'twitch-graphql'
import { UserSchema, UserResolvers } from 'twitch-graphql
Expand Down Expand Up @@ -139,7 +148,7 @@ This will respond with the refresh token that you need.
### Subscriber

```graphql
type Query {
extend type Query {
latestSub: Subscriber!
randomSub: Subscriber!
allSubs: [Subscriber]!
Expand Down
17 changes: 15 additions & 2 deletions src/schema/game-type-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ 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'
describe('GameModule', () => {
it('game should have all fields', async () => {
const app = createApplication({
modules: [SubscriberModule, UserModule, StreamModule, GameModule],
modules: [
QueryModule,
SubscriberModule,
UserModule,
StreamModule,
GameModule,
],
})
const schema = app.createSchemaForApollo()

Expand Down Expand Up @@ -44,7 +51,13 @@ describe('GameModule', () => {
})
it('can search game with getGameByName', async () => {
const app = createApplication({
modules: [SubscriberModule, UserModule, StreamModule, GameModule],
modules: [
QueryModule,
SubscriberModule,
UserModule,
StreamModule,
GameModule,
],
})
const schema = app.createSchemaForApollo()

Expand Down
34 changes: 34 additions & 0 deletions src/schema/query-type-schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { SubscriberModule } from './subscriber-type-schema'
import { execute, parse } from 'graphql'
import { createApplication } from 'graphql-modules'
import { QueryModule } from './query-type-schema'

describe('QueryModule', () => {
it('query module can be extended to include latestSub', async () => {
const app = createApplication({ modules: [QueryModule, SubscriberModule] })
const schema = app.createSchemaForApollo()

const document = parse(`
{
latestSub {
userId
tier
userDisplayName
isGift
}
}
`)
const contextValue = { request: {}, response: {} }
const result = await execute({
schema,
contextValue,
document,
})

expect(result?.errors?.length).toBeFalsy()
expect(result?.data?.latestSub).toHaveProperty('tier')
expect(result?.data?.latestSub).toHaveProperty('userId')
expect(result?.data?.latestSub).toHaveProperty('isGift')
expect(result?.data?.latestSub).toHaveProperty('userDisplayName')
})
})
20 changes: 20 additions & 0 deletions src/schema/query-type-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createModule, gql } from 'graphql-modules'

export const QueryResolvers = {
Query: {
_: () => false,
},
}

export const QuerySchema = gql`
type Query {
_: Boolean
}
`

export const QueryModule = createModule({
id: `query-module`,
dirname: __dirname,
typeDefs: QuerySchema,
resolvers: QueryResolvers,
})
3 changes: 2 additions & 1 deletion src/schema/stream-type-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { SubscriberModule } from './subscriber-type-schema'
import { UserModule } from './user-type-schema'
import { StreamModule } from './stream-type-schema'
import { parse, execute } from 'graphql'
import { QueryModule } from './query-type-schema'
describe('StreamModule', () => {
it('stream should have all fields', async () => {
const app = createApplication({
modules: [SubscriberModule, UserModule, StreamModule],
modules: [QueryModule, SubscriberModule, UserModule, StreamModule],
})
const schema = app.createSchemaForApollo()

Expand Down
15 changes: 9 additions & 6 deletions src/schema/subscriber-type-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { SubscriberModule } from './subscriber-type-schema'
import { execute, parse } from 'graphql'
import { createApplication } from 'graphql-modules'
import { UserModule } from './user-type-schema'
import { QueryModule } from './query-type-schema'

describe('SubscriberModule', () => {
it('latestSub', async () => {
const app = createApplication({ modules: [SubscriberModule] })
const app = createApplication({ modules: [QueryModule, SubscriberModule] })
const schema = app.createSchemaForApollo()

const document = parse(`
Expand Down Expand Up @@ -33,7 +34,7 @@ describe('SubscriberModule', () => {
})

it('randomSub', async () => {
const app = createApplication({ modules: [SubscriberModule] })
const app = createApplication({ modules: [QueryModule, SubscriberModule] })
const schema = app.createSchemaForApollo()

const document = parse(`
Expand Down Expand Up @@ -61,7 +62,7 @@ describe('SubscriberModule', () => {
})

it('allSubs', async () => {
const app = createApplication({ modules: [SubscriberModule] })
const app = createApplication({ modules: [QueryModule, SubscriberModule] })
const schema = app.createSchemaForApollo()

const document = parse(`
Expand All @@ -86,7 +87,7 @@ describe('SubscriberModule', () => {
})

it('findSub', async () => {
const app = createApplication({ modules: [SubscriberModule] })
const app = createApplication({ modules: [QueryModule, SubscriberModule] })
const schema = app.createSchemaForApollo()

const document = parse(`
Expand Down Expand Up @@ -117,7 +118,7 @@ describe('SubscriberModule', () => {
})

it('subCount', async () => {
const app = createApplication({ modules: [SubscriberModule] })
const app = createApplication({ modules: [QueryModule, SubscriberModule] })
const schema = app.createSchemaForApollo()

const document = parse(`
Expand All @@ -137,7 +138,9 @@ describe('SubscriberModule', () => {
})

it('sub user', async () => {
const app = createApplication({ modules: [SubscriberModule, UserModule] })
const app = createApplication({
modules: [QueryModule, SubscriberModule, UserModule],
})
const schema = app.createSchemaForApollo()

const document = parse(`
Expand Down
2 changes: 1 addition & 1 deletion src/schema/subscriber-type-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const SubscriberResolvers = {
}

export const SubscriberSchema = gql`
type Query {
extend type Query {
latestSub: Subscriber!
randomSub: Subscriber!
allSubs: [Subscriber]!
Expand Down
7 changes: 4 additions & 3 deletions src/schema/user-type-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { createApplication } from 'graphql-modules'
import { SubscriberModule } from './subscriber-type-schema'
import { UserModule } from './user-type-schema'
import { parse, execute } from 'graphql'
import { QueryModule } from './query-type-schema'

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

Expand Down Expand Up @@ -40,7 +41,7 @@ describe('UserModule', () => {
})
it('getUserById should work', async () => {
const app = createApplication({
modules: [SubscriberModule, UserModule],
modules: [QueryModule, SubscriberModule, UserModule],
})
const schema = app.createSchemaForApollo()

Expand Down Expand Up @@ -81,7 +82,7 @@ describe('UserModule', () => {
})
it('getUserByDisplayName should work', async () => {
const app = createApplication({
modules: [SubscriberModule, UserModule],
modules: [QueryModule, SubscriberModule, UserModule],
})
const schema = app.createSchemaForApollo()

Expand Down

0 comments on commit 3b5d007

Please sign in to comment.