apollo-server#PubSub TypeScript Examples

The following examples show how to use apollo-server#PubSub. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: user.ts    From Next.js_GraphQL_Express_Apollo_Boilerplate with MIT License 5 votes vote down vote up
pubsub = new PubSub()
Example #2
Source File: index.ts    From ledokku with MIT License 5 votes vote down vote up
pubsub = new PubSub()
Example #3
Source File: execution.ts    From genql with MIT License 4 votes vote down vote up
describe('execute subscriptions', async function() {
    const x: DeepPartial<User> = {
        name: 'John',
    }
    const pubsub = new PubSub()
    const USER_EVENT = 'userxxx'

    const makeServer = () =>
        server({
            resolvers: {
                Subscription: {
                    user: {
                        subscribe: () => {
                            return pubsub.asyncIterator([USER_EVENT])
                        },
                    },
                },
            },
        })

    it('simple ', async () => {
        const client = createClient({
            url: SUB_URL,
        })

        const stop = await makeServer()
        // await pubsub.publish(USER_EVENT, { user: x })
        await sleep(100)
        const sub = await client
            .subscription({
                user: {
                    name: true,
                    common: 1,
                    __typename: true,
                },
            })
            .subscribe({
                next: (x) => {
                    expectType<Maybe<string>>(x.user?.name)
                    expectType<Maybe<string>>(x.user?.__typename)
                    expectType<Maybe<number>>(x.user?.common)
                    console.log(x)
                },
                complete: () => console.log('complete'),
                error: console.error,
            })

        // await sleep(1000)
        await pubsub.publish(USER_EVENT, { user: x })
        // console.log(JSON.stringify(res, null, 2))
        sub.unsubscribe()
        client?.wsClient?.close?.()
        await stop()
        // assert(deepEq(res.user, x))
    })

    it('headers function gets called', async () => {
        let headersCalledNTimes = 0

        const client = createClient({
            url: SUB_URL,
            subscription: {
                headers: async () => {
                    headersCalledNTimes++
                    return {}
                },
            },
        })
        const stop = await makeServer()
        // await pubsub.publish(USER_EVENT, { user: x })
        await sleep(100)
        let subscribeCalledNTimes = 0
        const sub = client
            .subscription({
                user: {
                    name: true,
                    common: 1,
                    __typename: true,
                },
            })
            .subscribe({
                next: (x) => {
                    expectType<Maybe<string>>(x.user?.name)
                    expectType<Maybe<string>>(x.user?.__typename)
                    expectType<Maybe<number>>(x.user?.common)
                    console.log(x)
                    subscribeCalledNTimes++
                },
                complete: () => console.log('complete'),
                error: console.error,
            })

        await sleep(100)
        await pubsub.publish(USER_EVENT, { user: x })
        await pubsub.publish(USER_EVENT, { user: x })
        await sleep(100)
        assert.strictEqual(subscribeCalledNTimes, 2, 'subscribeCalledNTimes')
        // console.log(JSON.stringify(res, null, 2))
        sub.unsubscribe()
        client.wsClient!.close()
        await stop()
        assert.strictEqual(headersCalledNTimes, 1)
    })
})