Skip to content

Commit

Permalink
use yoga server for subscriptions tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gilgardosh committed Aug 28, 2022
1 parent ff04557 commit 665071c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 68 deletions.
8 changes: 4 additions & 4 deletions packages/loaders/openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
},
"dependencies": {
"@graphql-mesh/cross-helpers": "0.2.2",
"@graphql-mesh/utils": "0.40.0",
"@graphql-mesh/string-interpolation": "0.3.2",
"@graphql-mesh/types": "0.80.2",
"@graphql-mesh/utils": "0.40.0",
"@omnigraph/json-schema": "0.31.0",
"change-case": "4.1.2",
"json-machete": "0.13.2",
Expand All @@ -42,15 +42,15 @@
"@graphql-tools/utils": "8.10.0",
"@graphql-yoga/node": "2.13.9",
"@types/cookie-parser": "1.4.3",
"@types/eventsource": "1.1.9",
"@types/multer": "1.4.7",
"@whatwg-node/fetch": "0.3.2",
"body-parser": "1.20.0",
"cookie-parser": "1.4.6",
"eventsource": "2.0.2",
"express": "4.18.1",
"json-bigint-patch": "0.0.8",
"multer": "1.4.5-lts.1",
"subscriptions-transport-ws": "^0.11.0",
"ws": "^8.8.1"
"multer": "1.4.5-lts.1"
},
"publishConfig": {
"access": "public",
Expand Down
91 changes: 39 additions & 52 deletions packages/loaders/openapi/tests/example_api7.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { createServer, YogaNodeServerInstance } from '@graphql-yoga/node';
import { fetch } from '@whatwg-node/fetch';
import { createServer, Server } from 'http';
import { graphql, execute, subscribe, GraphQLSchema } from 'graphql';
import { SubscriptionServer, SubscriptionClient, ServerOptions } from 'subscriptions-transport-ws';
import ws from 'ws';
import { graphql, GraphQLSchema } from 'graphql';
import EventSource from 'eventsource';

import { loadGraphQLSchemaFromOpenAPI } from '../src/loadGraphQLSchemaFromOpenAPI';
import { startServer, stopServer, pubsub } from './example_api7_server';
Expand All @@ -13,8 +12,7 @@ const HTTP_PORT = 3008;
// Update PORT for this test case:
const baseUrl = `http://localhost:${HTTP_PORT}/api`;

let wsServer: Server;
let subscriptionServer: SubscriptionServer;
let yogaServer: YogaNodeServerInstance<any, any, any>;

describe('OpenAPI Loader: example_api7', () => {
// Set up the schema first and run example API servers
Expand All @@ -28,27 +26,13 @@ describe('OpenAPI Loader: example_api7', () => {

createdSchema = schema;
try {
wsServer = createServer((req, res) => {
res.writeHead(404);
res.end();
yogaServer = createServer({
schema,
port: TEST_PORT,
context: { pubsub },
});

wsServer.listen(TEST_PORT);

subscriptionServer = new SubscriptionServer(
{
execute,
subscribe,
schema,
onConnect: () => {
return { pubsub };
},
} as ServerOptions,
{
server: wsServer,
path: '/subscriptions',
}
);
await yogaServer.start();
} catch (e) {
console.log('error', e);
}
Expand All @@ -59,7 +43,8 @@ describe('OpenAPI Loader: example_api7', () => {
* Shut down API servers
*/
afterAll(async () => {
await Promise.all([subscriptionServer.close(), wsServer.close(), stopServer()]);
await new Promise(resolve => setTimeout(resolve, 500));
await Promise.all([yogaServer.stop(), stopServer()]);
});

it('Receive data from the subscription after creating a new instance', async () => {
Expand Down Expand Up @@ -88,38 +73,40 @@ describe('OpenAPI Loader: example_api7', () => {
`;

await new Promise<void>((resolve, reject) => {
const client = new SubscriptionClient(`ws://localhost:${TEST_PORT}/subscriptions`, {}, ws);
const url = new URL(`http://localhost:${TEST_PORT}/graphql`);

url.searchParams.append('query', query);
url.searchParams.append(
'variables',
JSON.stringify({
method: 'POST',
userName,
})
);

client.onError(e => reject(e));
const eventsource = new EventSource(url.href, {
withCredentials: true, // is this needed?
});

client
.request({
query,
operationName: 'watchDevice',
variables: {
method: 'POST',
userName,
},
})
.subscribe({
next: result => {
if (result.errors) {
reject(result.errors);
}
eventsource.onerror = e => {
eventsource.close();
reject(e);
};

if (result.data) {
expect(result.data).toEqual({
devicesEventListener: {
name: deviceName,
status: false,
},
});
resolve();
}
eventsource.onmessage = event => {
const res = JSON.parse(event.data);

expect(res.data).toEqual({
devicesEventListener: {
name: deviceName,
status: false,
},
error: e => reject(e),
});

eventsource.close();
resolve();
};

setTimeout(() => {
graphql({
schema: createdSchema,
Expand Down
23 changes: 11 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4951,6 +4951,11 @@
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.50.tgz#1e0caa9364d3fccd2931c3ed96fdbeaa5d4cca83"
integrity sha512-C6N5s2ZFtuZRj54k2/zyRhNDjJwwcViAM3Nbm8zjBpbqAdZ00mr0CFxvSKeO8Y/e03WVFLpQMdHYVfUd6SB+Hw==

"@types/[email protected]":
version "1.1.9"
resolved "https://registry.yarnpkg.com/@types/eventsource/-/eventsource-1.1.9.tgz#ec35903741773c12b78a1859c82d2778b5eafcf9"
integrity sha512-F3K4oyM12o8W9jxuJmW+1sc8kdw0Hj0t+26urwkcolPJTgkfppEfIdftdcXmUU2QPBIwcrYO6diqgIqgCDf1FA==

"@types/express-serve-static-core@*", "@types/express-serve-static-core@^4.17.18", "@types/express-serve-static-core@^4.17.21":
version "4.17.30"
resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.30.tgz#0f2f99617fa8f9696170c46152ccf7500b34ac04"
Expand Down Expand Up @@ -10081,6 +10086,11 @@ events@^3.2.0:
resolved "https://registry.yarnpkg.com/events/-/events-3.3.0.tgz#31a95ad0a924e2d2c419a813aeb2c4e878ea7400"
integrity sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==

[email protected]:
version "2.0.2"
resolved "https://registry.yarnpkg.com/eventsource/-/eventsource-2.0.2.tgz#76dfcc02930fb2ff339520b6d290da573a9e8508"
integrity sha512-IzUmBGPR3+oUG9dUeXynyNmf91/3zUSJg1lCktzKw47OXuhco54U3r9B7O4XX+Rb1Itm9OZ2b0RkTs10bICOxA==

execa@^0.8.0:
version "0.8.0"
resolved "https://registry.yarnpkg.com/execa/-/execa-0.8.0.tgz#d8d76bbc1b55217ed190fd6dd49d3c774ecfc8da"
Expand Down Expand Up @@ -18968,17 +18978,6 @@ [email protected]:
symbol-observable "^1.0.4"
ws "^5.2.0"

subscriptions-transport-ws@^0.11.0:
version "0.11.0"
resolved "https://registry.yarnpkg.com/subscriptions-transport-ws/-/subscriptions-transport-ws-0.11.0.tgz#baf88f050cba51d52afe781de5e81b3c31f89883"
integrity sha512-8D4C6DIH5tGiAIpp5I0wD/xRlNiZAPGHygzCe7VzyzUoxHtawzjNAY9SUTXU05/EY2NMY9/9GF0ycizkXr1CWQ==
dependencies:
backo2 "^1.0.2"
eventemitter3 "^3.1.0"
iterall "^1.2.1"
symbol-observable "^1.0.4"
ws "^5.2.0 || ^6.0.0 || ^7.0.0"

subscriptions-transport-ws@^0.9.18, subscriptions-transport-ws@^0.9.19:
version "0.9.19"
resolved "https://registry.yarnpkg.com/subscriptions-transport-ws/-/subscriptions-transport-ws-0.9.19.tgz#10ca32f7e291d5ee8eb728b9c02e43c52606cdcf"
Expand Down Expand Up @@ -20953,7 +20952,7 @@ write-pkg@^4.0.0:
type-fest "^0.4.1"
write-json-file "^3.2.0"

[email protected], ws@^8.2.2, ws@^8.3.0, ws@^8.4.2, ws@^8.8.1:
[email protected], ws@^8.2.2, ws@^8.3.0, ws@^8.4.2:
version "8.8.1"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.8.1.tgz#5dbad0feb7ade8ecc99b830c1d77c913d4955ff0"
integrity sha512-bGy2JzvzkPowEJV++hF07hAD6niYSr0JzBNo/J29WsB57A2r7Wlc1UFcTR9IzrPvuNVO4B8LGqF8qcpsVOhJCA==
Expand Down

0 comments on commit 665071c

Please sign in to comment.