Skip to content

Commit

Permalink
cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
gilgardosh committed Aug 28, 2022
1 parent e97cce9 commit ff04557
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 36 deletions.
57 changes: 24 additions & 33 deletions packages/loaders/openapi/tests/example_api7.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { graphql, execute, subscribe, GraphQLSchema } from 'graphql';

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 { fetch } from '@whatwg-node/fetch';

import { loadGraphQLSchemaFromOpenAPI } from '../src/loadGraphQLSchemaFromOpenAPI';
import { startServers, stopServers, pubsub } from './example_api7_server';
import { startServer, stopServer, pubsub } from './example_api7_server';

let createdSchema: GraphQLSchema;
const TEST_PORT = 3009;
Expand All @@ -20,13 +19,11 @@ let subscriptionServer: SubscriptionServer;
describe('OpenAPI Loader: example_api7', () => {
// Set up the schema first and run example API servers
beforeAll(async () => {
const schema = await loadGraphQLSchemaFromOpenAPI('example_api6', {
const schema = await loadGraphQLSchemaFromOpenAPI('example_api7', {
fetch,
baseUrl,
source: './fixtures/example_oas7.json',
cwd: __dirname,
// fillEmptyResponses: true,
// createSubscriptionsFromCallbacks: true
});

createdSchema = schema;
Expand Down Expand Up @@ -55,48 +52,42 @@ describe('OpenAPI Loader: example_api7', () => {
} catch (e) {
console.log('error', e);
}
await startServers(HTTP_PORT);
await startServer(HTTP_PORT);
});

function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}

/**
* Shut down API servers
*/
afterAll(async () => {
/**
* TODO: There seems to be some trouble closing the servers and connections.
* The timeout allows these to close properly but is there a better way?
*/
await sleep(500);
await Promise.all([subscriptionServer.close(), wsServer.close(), stopServers()]);
await sleep(500);
await Promise.all([subscriptionServer.close(), wsServer.close(), stopServer()]);
});

test('Receive data from the subscription after creating a new instance', () => {
it('Receive data from the subscription after creating a new instance', async () => {
const userName = 'Carlos';
const deviceName = 'Bot';

const query = `subscription watchDevice($method: String!, $userName: String!) {
devicesEventListener(method: $method, userName: $userName) {
name
status
}
}`;

const query2 = `mutation($deviceInput: Device_Input!) {
createDevice(input: $deviceInput) {
... on Device {
const query = /* GraphQL */ `
subscription watchDevice($method: String!, $userName: String!) {
devicesEventListener(method: $method, userName: $userName) {
name
userName
status
}
}
}`;
`;

const query2 = /* GraphQL */ `
mutation ($deviceInput: Device_Input!) {
createDevice(input: $deviceInput) {
... on Device {
name
userName
status
}
}
}
`;

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

client.onError(e => reject(e));
Expand Down
6 changes: 3 additions & 3 deletions packages/loaders/openapi/tests/example_api7_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const Devices = {
/**
* Starts the server at the given port
*/
export function startServers(HTTP_PORT: number) {
export function startServer(HTTP_PORT: number) {
app.use(bodyParser.json());

app.get('/api/user', (req, res) => {
Expand Down Expand Up @@ -96,13 +96,13 @@ export function startServers(HTTP_PORT: number) {
/**
* Stops server.
*/
export function stopServers() {
export function stopServer() {
return Promise.all([server.close()]).then(() => {
console.log(`Stopped HTTP API server`);
});
}

// If run from command line, start server:
if (require.main === module) {
startServers(3008).catch(console.error);
startServer(3008).catch(console.error);
}

0 comments on commit ff04557

Please sign in to comment.