nexus/tests/subscriptionField.spec.ts

114 lines
2.0 KiB
TypeScript

import { lexicographicSortSchema, printSchema } from 'graphql'
import { makeSchema, subscriptionField } from '../src/core'
import { mockStream, subscribe, take } from './__helpers'
it('defines a field on the mutation type as shorthand', async () => {
const schema = makeSchema({
types: [
subscriptionField('someField', {
type: 'Int',
subscribe() {
return mockStream(10, 0, (n) => n + 1)
},
resolve: (event) => event,
}),
],
outputs: false,
})
expect(printSchema(lexicographicSortSchema(schema))).toMatchInlineSnapshot(`
"type Query {
ok: Boolean!
}
type Subscription {
someField: Int
}
"
`)
expect(
await subscribe(
schema,
`
subscription {
someField
}
`
).then(take(3))
).toMatchInlineSnapshot(`
Array [
Object {
"data": Object {
"someField": 1,
},
},
Object {
"data": Object {
"someField": 2,
},
},
Object {
"data": Object {
"someField": 3,
},
},
]
`)
})
it('can be defined as a thunk', async () => {
const schema = makeSchema({
types: [
subscriptionField('someField', () => ({
type: 'Int',
subscribe() {
return mockStream(10, 0, (n) => n + 1)
},
resolve: (event) => event,
})),
],
outputs: false,
})
expect(printSchema(lexicographicSortSchema(schema))).toMatchInlineSnapshot(`
"type Query {
ok: Boolean!
}
type Subscription {
someField: Int
}
"
`)
expect(
await subscribe(
schema,
`
subscription {
someField
}
`
).then(take(3))
).toMatchInlineSnapshot(`
Array [
Object {
"data": Object {
"someField": 1,
},
},
Object {
"data": Object {
"someField": 2,
},
},
Object {
"data": Object {
"someField": 3,
},
},
]
`)
})