@oridune/epic-plugin-graphql
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -19,3 +19,2 @@ "use strict"; | ||
const ioredis_1 = __importDefault(require("ioredis")); | ||
const common_1 = require("../common"); | ||
// GraphQL Subscriptions Availability Status | ||
@@ -31,149 +30,147 @@ exports.GraphQLSubscriptionsAvailable = exports_1.RedisClient instanceof ioredis_1.default; | ||
const UseGraphQLJob = async () => { | ||
if (!common_1.Locals.disabled) { | ||
// GraphQL Fields Function | ||
const GetGraphQLFields = (type) => server_1.Server.Application.Globals.getRouterDetails().reduce((fields, routeDetails) => { | ||
var _a; | ||
if (typeof ((_a = routeDetails.metadata) === null || _a === void 0 ? void 0 : _a.graphql) === "object") { | ||
const FieldType = routeDetails.metadata.graphql.type || "query"; | ||
if ((FieldType instanceof Array ? FieldType : [FieldType]).includes(type)) | ||
fields[routeDetails.name] = { | ||
type: routeDetails.metadata.graphql.responseType, | ||
args: routeDetails.metadata.graphql.args, | ||
description: routeDetails.description, | ||
subscribe: type === "subscription" | ||
? (_, __, { pubsub }) => pubsub.asyncIterator([routeDetails.name]) | ||
: undefined, | ||
resolve: type === "subscription" | ||
? (_) => _ | ||
: async (parent, args, ctx, info) => { | ||
// Resolve Request | ||
ctx.req.params = Object.assign(Object.assign({}, ctx.req.params), args); | ||
ctx.req.graphQLRequest = { | ||
parent, | ||
args, | ||
ctx, | ||
info, | ||
fields: () => { | ||
// GraphQL Requested Fields Normalizer | ||
const NormalizeFields = (fields, result = {}) => { | ||
for (const Key of Object.keys(fields)) | ||
if (typeof fields[Key] === "object") | ||
if (Object.keys(fields[Key]).length) | ||
result[Key] = NormalizeFields(fields[Key]); | ||
else | ||
result[Key] = 1; | ||
return result; | ||
}; | ||
return NormalizeFields((0, graphql_fields_1.default)(info)); | ||
}, | ||
}; | ||
// Execute Request Handler | ||
const Response = await routeDetails.handler.bind(new routeDetails.controller())(ctx.req, ctx.res, () => { | ||
throw new Error(`Cannot call next in a GraphQL resolver context!`); | ||
}); | ||
// Return Response Data | ||
return typeof Response === "object" | ||
? Response.Data || {} | ||
: {}; | ||
}, | ||
// GraphQL Fields Function | ||
const GetGraphQLFields = (type) => server_1.HTTPServer.Application.Globals.getRouterDetails().reduce((fields, routeDetails) => { | ||
var _a; | ||
if (typeof ((_a = routeDetails.metadata) === null || _a === void 0 ? void 0 : _a.graphql) === "object") { | ||
const FieldType = routeDetails.metadata.graphql.type || "query"; | ||
if ((FieldType instanceof Array ? FieldType : [FieldType]).includes(type)) | ||
fields[routeDetails.name] = { | ||
type: routeDetails.metadata.graphql.responseType, | ||
args: routeDetails.metadata.graphql.args, | ||
description: routeDetails.description, | ||
subscribe: type === "subscription" | ||
? (_, __, { pubsub }) => pubsub.asyncIterator([routeDetails.name]) | ||
: undefined, | ||
resolve: type === "subscription" | ||
? (_) => _ | ||
: async (parent, args, ctx, info) => { | ||
// Resolve Request | ||
ctx.req.params = Object.assign(Object.assign({}, ctx.req.params), args); | ||
ctx.req.graphQLRequest = { | ||
parent, | ||
args, | ||
ctx, | ||
info, | ||
fields: () => { | ||
// GraphQL Requested Fields Normalizer | ||
const NormalizeFields = (fields, result = {}) => { | ||
for (const Key of Object.keys(fields)) | ||
if (typeof fields[Key] === "object") | ||
if (Object.keys(fields[Key]).length) | ||
result[Key] = NormalizeFields(fields[Key]); | ||
else | ||
result[Key] = 1; | ||
return result; | ||
}; | ||
return NormalizeFields((0, graphql_fields_1.default)(info)); | ||
}, | ||
}; | ||
// Execute Request Handler | ||
const Response = await routeDetails.handler.bind(new routeDetails.controller())(ctx.req, ctx.res, () => { | ||
throw new Error(`Cannot call next in a GraphQL resolver context!`); | ||
}); | ||
// Return Response Data | ||
return typeof Response === "object" | ||
? Response.Data || {} | ||
: {}; | ||
}, | ||
}; | ||
} | ||
return fields; | ||
}, {}); | ||
// Get GraphQL Fields | ||
const QueryFields = GetGraphQLFields("query"); | ||
const MutationFields = GetGraphQLFields("mutation"); | ||
const SubscriptionFields = GetGraphQLFields("subscription"); | ||
// Create GraphQL Schema | ||
const Schema = new graphql_1.GraphQLSchema({ | ||
query: Object.keys(QueryFields).length | ||
? new graphql_1.GraphQLObjectType({ | ||
name: "RootQueryType", | ||
description: "Followings are the available fields on the query.", | ||
fields: QueryFields, | ||
}) | ||
: undefined, | ||
mutation: Object.keys(MutationFields).length | ||
? new graphql_1.GraphQLObjectType({ | ||
name: "Mutation", | ||
description: "Followings are the available fields on the mutation.", | ||
fields: MutationFields, | ||
}) | ||
: undefined, | ||
subscription: exports.GraphQLSubscriptionsAvailable && Object.keys(SubscriptionFields).length | ||
? new graphql_1.GraphQLObjectType({ | ||
name: "Subscription", | ||
description: "Followings are the available fields on the subscription.", | ||
fields: SubscriptionFields, | ||
}) | ||
: undefined, | ||
}); | ||
// GraphQL Path | ||
const GraphQLPath = "/graphql/"; | ||
// Setup a GraphQL WebSocket server | ||
const GraphQLWSServer = new ws_1.WebSocketServer({ noServer: true }); | ||
// Subscriptions Transport Websocket server | ||
const SubsTransWS = new ws_1.WebSocketServer({ noServer: true }); | ||
subscriptions_transport_ws_1.SubscriptionServer.create({ | ||
schema: Schema, | ||
execute: graphql_1.execute, | ||
subscribe: graphql_1.subscribe, | ||
}, SubsTransWS); | ||
// Make Servers Disposable | ||
const DisposableServers = [ | ||
(0, ws_2.useServer)({ schema: Schema }, GraphQLWSServer), | ||
(0, ws_2.useServer)({ schema: Schema }, SubsTransWS), | ||
]; | ||
// Listen for upgrades and delegate requests according to the WS subprotocol | ||
server_1.HTTPServer.Server.on("upgrade", (req, socket, head) => { | ||
// Extract websocket subprotocol from header | ||
const Protocol = req.headers["sec-websocket-protocol"]; | ||
const Protocols = Array.isArray(Protocol) | ||
? Protocol | ||
: Protocol === null || Protocol === void 0 ? void 0 : Protocol.split(",").map((p) => p.trim()); | ||
// Decide which websocket server to use | ||
const wss = (Protocols === null || Protocols === void 0 ? void 0 : Protocols.includes(subscriptions_transport_ws_1.GRAPHQL_WS)) && // subscriptions-transport-ws subprotocol | ||
!Protocols.includes(graphql_ws_1.GRAPHQL_TRANSPORT_WS_PROTOCOL) // graphql-ws subprotocol | ||
? SubsTransWS | ||
: // graphql-ws will welcome its own subprotocol and | ||
// Gracefully reject invalid ones. if the client supports | ||
// Both transports, graphql-ws will prevail | ||
GraphQLWSServer; | ||
wss.handleUpgrade(req, socket, head, (ws) => { | ||
wss.emit("connection", ws, req); | ||
}); | ||
}); | ||
// Create Apollo Server | ||
const Apollo = new apollo_server_express_1.ApolloServer({ | ||
schema: Schema, | ||
csrfPrevention: true, | ||
context: ({ req, res }) => ({ | ||
req, | ||
res, | ||
pubsub: exports.PubSub, | ||
}), | ||
plugins: [ | ||
// Proper shutdown for the HTTP server. | ||
(0, apollo_server_core_1.ApolloServerPluginDrainHttpServer)({ httpServer: server_1.HTTPServer.Server }), | ||
// Proper shutdown for the WebSocket server. | ||
{ | ||
async serverWillStart() { | ||
return { | ||
async drainServer() { | ||
await Promise.all(DisposableServers.map((_) => _.dispose())); | ||
}, | ||
}; | ||
} | ||
return fields; | ||
}, {}); | ||
// Get GraphQL Fields | ||
const QueryFields = GetGraphQLFields("query"); | ||
const MutationFields = GetGraphQLFields("mutation"); | ||
const SubscriptionFields = GetGraphQLFields("subscription"); | ||
// Create GraphQL Schema | ||
const Schema = new graphql_1.GraphQLSchema({ | ||
query: Object.keys(QueryFields).length | ||
? new graphql_1.GraphQLObjectType({ | ||
name: "RootQueryType", | ||
description: "Followings are the available fields on the query.", | ||
fields: QueryFields, | ||
}) | ||
: undefined, | ||
mutation: Object.keys(MutationFields).length | ||
? new graphql_1.GraphQLObjectType({ | ||
name: "Mutation", | ||
description: "Followings are the available fields on the mutation.", | ||
fields: MutationFields, | ||
}) | ||
: undefined, | ||
subscription: exports.GraphQLSubscriptionsAvailable && Object.keys(SubscriptionFields).length | ||
? new graphql_1.GraphQLObjectType({ | ||
name: "Subscription", | ||
description: "Followings are the available fields on the subscription.", | ||
fields: SubscriptionFields, | ||
}) | ||
: undefined, | ||
}); | ||
// GraphQL Path | ||
const GraphQLPath = "/graphql/"; | ||
// Setup a GraphQL WebSocket server | ||
const GraphQLWSServer = new ws_1.WebSocketServer({ noServer: true }); | ||
// Subscriptions Transport Websocket server | ||
const SubsTransWS = new ws_1.WebSocketServer({ noServer: true }); | ||
subscriptions_transport_ws_1.SubscriptionServer.create({ | ||
schema: Schema, | ||
execute: graphql_1.execute, | ||
subscribe: graphql_1.subscribe, | ||
}, SubsTransWS); | ||
// Make Servers Disposable | ||
const DisposableServers = [ | ||
(0, ws_2.useServer)({ schema: Schema }, GraphQLWSServer), | ||
(0, ws_2.useServer)({ schema: Schema }, SubsTransWS), | ||
]; | ||
// Listen for upgrades and delegate requests according to the WS subprotocol | ||
server_1.Server.Server.on("upgrade", (req, socket, head) => { | ||
// Extract websocket subprotocol from header | ||
const Protocol = req.headers["sec-websocket-protocol"]; | ||
const Protocols = Array.isArray(Protocol) | ||
? Protocol | ||
: Protocol === null || Protocol === void 0 ? void 0 : Protocol.split(",").map((p) => p.trim()); | ||
// Decide which websocket server to use | ||
const wss = (Protocols === null || Protocols === void 0 ? void 0 : Protocols.includes(subscriptions_transport_ws_1.GRAPHQL_WS)) && // subscriptions-transport-ws subprotocol | ||
!Protocols.includes(graphql_ws_1.GRAPHQL_TRANSPORT_WS_PROTOCOL) // graphql-ws subprotocol | ||
? SubsTransWS | ||
: // graphql-ws will welcome its own subprotocol and | ||
// Gracefully reject invalid ones. if the client supports | ||
// Both transports, graphql-ws will prevail | ||
GraphQLWSServer; | ||
wss.handleUpgrade(req, socket, head, (ws) => { | ||
wss.emit("connection", ws, req); | ||
}); | ||
}); | ||
// Create Apollo Server | ||
const Apollo = new apollo_server_express_1.ApolloServer({ | ||
schema: Schema, | ||
csrfPrevention: true, | ||
context: ({ req, res }) => ({ | ||
req, | ||
res, | ||
pubsub: exports.PubSub, | ||
}), | ||
plugins: [ | ||
// Proper shutdown for the HTTP server. | ||
(0, apollo_server_core_1.ApolloServerPluginDrainHttpServer)({ httpServer: server_1.Server.Server }), | ||
// Proper shutdown for the WebSocket server. | ||
{ | ||
async serverWillStart() { | ||
return { | ||
async drainServer() { | ||
await Promise.all(DisposableServers.map((_) => _.dispose())); | ||
}, | ||
}; | ||
}, | ||
}, | ||
], | ||
}); | ||
// Start GraphQL Server | ||
await Apollo.start(); | ||
// Add GraphQL to the Application | ||
Apollo.applyMiddleware({ | ||
app: server_1.Server.Application.Framework, | ||
path: GraphQLPath, | ||
}); | ||
} | ||
}, | ||
], | ||
}); | ||
// Start GraphQL Server | ||
await Apollo.start(); | ||
// Add GraphQL to the Application | ||
Apollo.applyMiddleware({ | ||
app: server_1.HTTPServer.Application.Framework, | ||
path: GraphQLPath, | ||
}); | ||
}; | ||
exports.UseGraphQLJob = UseGraphQLJob; |
{ | ||
"name": "@oridune/epic-plugin-graphql", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Adds the GraphQL support to your Epic application.", | ||
@@ -5,0 +5,0 @@ "private": false, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
89884
373