mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 01:12:56 +03:00
35d9c059db
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/9420 Co-authored-by: Nicolas Beaussart <7281023+beaussan@users.noreply.github.com> GitOrigin-RevId: 31d983ae8573c91ac5bf11066770f776941c3a11
83 lines
2.1 KiB
JavaScript
83 lines
2.1 KiB
JavaScript
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
|
|
// Node module: openapi-to-graphql
|
|
// This file is licensed under the MIT License.
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
'use strict';
|
|
|
|
const express = require('express');
|
|
const { graphqlHTTP } = require('express-graphql');
|
|
const { execute, printSchema, subscribe } = require('graphql');
|
|
const { createServer } = require('http');
|
|
const { SubscriptionServer } = require('subscriptions-transport-ws');
|
|
const { MQTTPubSub } = require('graphql-mqtt-subscriptions');
|
|
const { connect } = require('mqtt');
|
|
const openAPIToGraphQL = require('../dist/index');
|
|
|
|
const oas5 = require('./fixtures/example_oas5.json');
|
|
|
|
const HTTP_PORT = 3000;
|
|
const app = express();
|
|
|
|
openAPIToGraphQL
|
|
.createGraphQLSchema(oas5, {
|
|
fillEmptyResponses: true,
|
|
createSubscriptionsFromCallbacks: true,
|
|
})
|
|
.then(({ schema, report }) => {
|
|
console.log(JSON.stringify(report, null, 2));
|
|
const myGraphQLSchema = printSchema(schema);
|
|
console.log(myGraphQLSchema);
|
|
|
|
const client = connect(`mqtt://localhost:1885`, {
|
|
keepalive: 60,
|
|
reschedulePings: true,
|
|
protocolId: 'MQTT',
|
|
protocolVersion: 4,
|
|
reconnectPeriod: 2000,
|
|
connectTimeout: 5 * 1000,
|
|
clean: true,
|
|
});
|
|
|
|
const pubsub = new MQTTPubSub({
|
|
client,
|
|
});
|
|
|
|
app.use(
|
|
'/graphql',
|
|
graphqlHTTP({
|
|
schema,
|
|
graphiql: true,
|
|
subscriptionsEndpoint: `ws://localhost:${HTTP_PORT}/subscriptions`,
|
|
context: { pubsub },
|
|
})
|
|
);
|
|
|
|
const server = createServer(app);
|
|
|
|
server.listen(HTTP_PORT, () => {
|
|
console.log(`Running a GraphQL API server at :`);
|
|
console.log(`http://localhost:${HTTP_PORT}/graphql`);
|
|
console.log(`ws://localhost:${HTTP_PORT}/subscriptions`);
|
|
|
|
new SubscriptionServer(
|
|
{
|
|
execute,
|
|
subscribe,
|
|
schema,
|
|
onConnect: (params, socket, ctx) => {
|
|
// Add pubsub to subscribe context
|
|
return { pubsub };
|
|
},
|
|
},
|
|
{
|
|
server,
|
|
path: '/subscriptions',
|
|
}
|
|
);
|
|
});
|
|
})
|
|
.catch(err => {
|
|
console.log(err);
|
|
});
|