UNPKG

kikstart-graphql-client

Version:

Small NodeJS Wrapper around apollo-client that provides easy access to running queries, mutations and subscriptions.

74 lines 2.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const graphql_yoga_1 = require("graphql-yoga"); exports.pubsub = new graphql_yoga_1.PubSub(); const MESSAGE_TRIGGER = 'MESSAGE_BUS_TRIGGER'; const SUBSCRIPTION_NAME = 'intercom'; const typeDefs = ` scalar JSON type Query { uptime: Float } input PublishInput { type: String! scope: String payload: JSON } input SubscribeInput { type: String scope: String } type MessagePayload { type: String! scope: String payload: JSON } type Mutation { publish(message: PublishInput): MessagePayload } type Subscription { ${SUBSCRIPTION_NAME}(filter: SubscribeInput): MessagePayload } `; const resolvers = { Query: { uptime: () => process.uptime(), }, Mutation: { publish(_, args) { exports.pubsub.publish(MESSAGE_TRIGGER, { [SUBSCRIPTION_NAME]: args.message }); return args.message; }, }, Subscription: { [SUBSCRIPTION_NAME]: { subscribe: graphql_yoga_1.withFilter(() => exports.pubsub.asyncIterator(MESSAGE_TRIGGER), (payload, variables) => { if (!payload || !payload[SUBSCRIPTION_NAME] || !variables.filter) { return true; } const { type, scope } = variables.filter; const hasType = type && payload[SUBSCRIPTION_NAME].type === type; const hasScope = scope && payload[SUBSCRIPTION_NAME].scope === scope; if (type && scope) { return hasType && hasScope; } if (!type && scope) { return hasScope; } if (type && !scope) { return hasType; } return true; }), }, }, }; exports.server = new graphql_yoga_1.GraphQLServer({ typeDefs, resolvers, }); const port = process.env.PORT || 4567; exports.server.start({ port }).then(() => { console.log(`🚀 Listening on http://localhost:${port} `); }); //# sourceMappingURL=message-bus-server.js.map