|
| 1 | +// Copyright LoopBack contributors 2024 |
| 2 | +// Node module: @loopback/example-graphql |
| 3 | +// This file is licensed under the MIT License. |
| 4 | +// License text available at https://opensource.org/licenses/MIT |
| 5 | + |
| 6 | +import {Application, createBindingFromClass} from '@loopback/core'; |
| 7 | +import {GraphQLServer} from '@loopback/graphql'; |
| 8 | +import { |
| 9 | + createRestAppClient, |
| 10 | + expect, |
| 11 | + givenHttpServerConfig, |
| 12 | + supertest, |
| 13 | +} from '@loopback/testlab'; |
| 14 | +import {createClient, Client as WsClient} from 'graphql-ws'; |
| 15 | +import WebSocket from 'ws'; |
| 16 | +import {GraphqlDemoApplication} from '../../application'; |
| 17 | +import {RecipesDataSource} from '../../datasources'; |
| 18 | +import {RecipeResolver} from '../../graphql-resolvers/recipe-resolver'; |
| 19 | +import {RecipeRepository} from '../../repositories'; |
| 20 | +import {sampleRecipes} from '../../sample-recipes'; |
| 21 | +import {RecipeService} from '../../services/recipe.service'; |
| 22 | +import {exampleQuery} from './graphql-tests'; |
| 23 | + |
| 24 | +describe('GraphQL server subscription', () => { |
| 25 | + let server: GraphQLServer; |
| 26 | + let repo: RecipeRepository; |
| 27 | + let client: supertest.SuperTest<supertest.Test>; |
| 28 | + let wsClient: WsClient; |
| 29 | + let notificationIter: AsyncIterableIterator<unknown>; |
| 30 | + |
| 31 | + before('setup server and add recipe', async function () { |
| 32 | + await setupServerAndSubscribe(); |
| 33 | + await addRecipe(client); |
| 34 | + }); |
| 35 | + |
| 36 | + after('unsubscribe and stop server', async function () { |
| 37 | + await unsubscribe(wsClient, notificationIter); |
| 38 | + await stopServer(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should receive notification', async () => |
| 42 | + receiveNotification(notificationIter)); |
| 43 | + |
| 44 | + async function setupServerAndSubscribe() { |
| 45 | + server = new GraphQLServer({ |
| 46 | + host: '127.0.0.1', |
| 47 | + port: 0, |
| 48 | + }); |
| 49 | + server.resolver(RecipeResolver); |
| 50 | + |
| 51 | + server.bind('recipes').to([...sampleRecipes]); |
| 52 | + const repoBinding = createBindingFromClass(RecipeRepository); |
| 53 | + server.add(repoBinding); |
| 54 | + server.add(createBindingFromClass(RecipesDataSource)); |
| 55 | + server.add(createBindingFromClass(RecipeService)); |
| 56 | + await server.start(); |
| 57 | + repo = await server.get<RecipeRepository>(repoBinding.key); |
| 58 | + await repo.start(); |
| 59 | + |
| 60 | + client = supertest(server.httpServer?.url); |
| 61 | + wsClient = await createWsClient(server.httpServer!.url); |
| 62 | + notificationIter = subscribe(wsClient); |
| 63 | + } |
| 64 | + |
| 65 | + async function stopServer() { |
| 66 | + if (!server) return; |
| 67 | + await server.stop(); |
| 68 | + repo.stop(); |
| 69 | + } |
| 70 | +}); |
| 71 | + |
| 72 | +describe('GraphQL application subscription', () => { |
| 73 | + let server: GraphQLServer; |
| 74 | + let app: Application; |
| 75 | + let client: supertest.SuperTest<supertest.Test>; |
| 76 | + let wsClient: WsClient; |
| 77 | + let notificationIter: AsyncIterableIterator<unknown>; |
| 78 | + |
| 79 | + before('setup app and subscribe', async function () { |
| 80 | + await setupAppAndSubscribe(); |
| 81 | + await addRecipe(client); |
| 82 | + }); |
| 83 | + |
| 84 | + after('unsubscribe and stop server', async function () { |
| 85 | + await unsubscribe(wsClient, notificationIter); |
| 86 | + await stopApp(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should receive notification', async () => |
| 90 | + receiveNotification(notificationIter)); |
| 91 | + |
| 92 | + async function setupAppAndSubscribe() { |
| 93 | + app = new Application(); |
| 94 | + const serverBinding = app.server(GraphQLServer); |
| 95 | + app.configure(serverBinding.key).to({host: '127.0.0.1', port: 0}); |
| 96 | + server = await app.getServer(GraphQLServer); |
| 97 | + server.resolver(RecipeResolver); |
| 98 | + |
| 99 | + app.bind('recipes').to([...sampleRecipes]); |
| 100 | + const repoBinding = createBindingFromClass(RecipeRepository); |
| 101 | + app.add(repoBinding); |
| 102 | + app.add(createBindingFromClass(RecipesDataSource)); |
| 103 | + app.add(createBindingFromClass(RecipeService)); |
| 104 | + await app.start(); |
| 105 | + |
| 106 | + client = supertest(server.httpServer?.url); |
| 107 | + wsClient = await createWsClient(server.httpServer!.url); |
| 108 | + notificationIter = subscribe(wsClient); |
| 109 | + } |
| 110 | + |
| 111 | + async function stopApp() { |
| 112 | + if (!app) return; |
| 113 | + await app.stop(); |
| 114 | + } |
| 115 | +}); |
| 116 | + |
| 117 | +describe('GraphQL as middleware subscription', () => { |
| 118 | + let app: GraphqlDemoApplication; |
| 119 | + let client: supertest.SuperTest<supertest.Test>; |
| 120 | + let wsClient: WsClient; |
| 121 | + let notificationIter: AsyncIterableIterator<unknown>; |
| 122 | + |
| 123 | + before('setup app and subscribe', async function () { |
| 124 | + await setupAppWithMiddlewareAndSubscribe(); |
| 125 | + await addRecipe(client); |
| 126 | + }); |
| 127 | + |
| 128 | + after('unsubscribe and stop server', async function () { |
| 129 | + await unsubscribe(wsClient, notificationIter); |
| 130 | + await stopApp(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should receive notification', async () => |
| 134 | + receiveNotification(notificationIter)); |
| 135 | + |
| 136 | + async function setupAppWithMiddlewareAndSubscribe() { |
| 137 | + app = new GraphqlDemoApplication({ |
| 138 | + rest: givenHttpServerConfig(), |
| 139 | + graphql: {asMiddlewareOnly: true}, |
| 140 | + }); |
| 141 | + await app.boot(); |
| 142 | + await app.start(); |
| 143 | + |
| 144 | + client = createRestAppClient(app); |
| 145 | + wsClient = await createWsClient( |
| 146 | + `${app.restServer.rootUrl ?? app.restServer.url!}`, |
| 147 | + ); |
| 148 | + notificationIter = subscribe(wsClient); |
| 149 | + } |
| 150 | + |
| 151 | + async function stopApp() { |
| 152 | + await app?.stop(); |
| 153 | + } |
| 154 | +}); |
| 155 | + |
| 156 | +async function addRecipe(client: supertest.SuperTest<supertest.Test>) { |
| 157 | + await client |
| 158 | + .post('/graphql') |
| 159 | + .set('content-type', 'application/json') |
| 160 | + .accept('application/json') |
| 161 | + .send({operationName: 'AddRecipe', variables: {}, query: exampleQuery}) |
| 162 | + .expect(200); |
| 163 | +} |
| 164 | + |
| 165 | +async function createWsClient(serverUrl: string): Promise<WsClient> { |
| 166 | + const url = serverUrl.replace(/^http/, 'ws'); |
| 167 | + return new Promise((resolve, reject) => { |
| 168 | + const webSocketsClient = createClient({ |
| 169 | + webSocketImpl: WebSocket, |
| 170 | + url, |
| 171 | + lazy: false, |
| 172 | + }); |
| 173 | + webSocketsClient.on('connected', () => resolve(webSocketsClient)); |
| 174 | + webSocketsClient.on('error', err => { |
| 175 | + process.stderr.write(String(err)); |
| 176 | + reject(new Error(`failed to create WS client: ${JSON.stringify(err)}`)); |
| 177 | + }); |
| 178 | + }); |
| 179 | +} |
| 180 | + |
| 181 | +function subscribe(wsClient: WsClient) { |
| 182 | + return wsClient.iterate({ |
| 183 | + operationName: 'AllNotifications', |
| 184 | + query: exampleQuery, |
| 185 | + }); |
| 186 | +} |
| 187 | + |
| 188 | +async function unsubscribe( |
| 189 | + wsClient: WsClient, |
| 190 | + notificationIter: AsyncIterableIterator<unknown>, |
| 191 | +): Promise<void> { |
| 192 | + await notificationIter.return?.(); |
| 193 | + await wsClient.dispose(); |
| 194 | +} |
| 195 | + |
| 196 | +async function receiveNotification( |
| 197 | + notificationIter: AsyncIterableIterator<unknown>, |
| 198 | +) { |
| 199 | + const {value: notification} = await notificationIter.next(); |
| 200 | + expect(notification.data.recipeCreated).to.containEql({ |
| 201 | + id: '4', |
| 202 | + numberInCollection: 4, |
| 203 | + }); |
| 204 | +} |
0 commit comments