How to use the graphql-subscriptions.withFilter function in graphql-subscriptions

To help you get started, we’ve selected a few graphql-subscriptions examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Thorium-Sim / thorium / server / src / resolvers / teams.js View on Github external
export const TeamsSubscriptions = {
  teamsUpdate: {
    resolve(rootValue, { simulatorId, type, cleared }) {
      // Get the simulator
      let returnVal = rootValue;
      if (type) {
        returnVal = returnVal.filter(t => t.type === type);
      }
      if (simulatorId) {
        returnVal = returnVal.filter(t => t.simulatorId === simulatorId);
      }
      if (cleared) return returnVal;
      return returnVal.filter(c => !c.cleared);
    },
    subscribe: withFilter(
      () => pubsub.asyncIterator("teamsUpdate"),
      (rootValue, { simulatorId, type }) => true
    )
  }
};

export const TeamsTypes = {
  Team: {
    location(team) {
      const deck = App.decks.find(d => d.id === team.location);
      if (deck) {
        return deck;
      }
      return App.rooms.find(r => r.id === team.location);
    },
    officers(team) {
github withspectrum / spectrum / iris / subscriptions / message.js View on Github external
import { withFilter } from 'graphql-subscriptions';
import { getThread } from '../models/thread';
import { userCanViewChannel, userCanViewDirectMessageThread } from './utils';
const { listenToNewMessages } = require('../models/message');
import asyncify from '../utils/asyncify';
import { trackUserThreadLastSeenQueue } from 'shared/bull/queues.js';
import type { Message } from '../models/message';

/**
 * Define the message subscription resolvers
 */
module.exports = {
  Subscription: {
    messageAdded: {
      resolve: (message: any) => message,
      subscribe: withFilter(
        asyncify(listenToNewMessages, err => {
          throw new Error(err);
        }),
        async (message, { thread }, { user }) => {
          // Message was sent in different thread, early return
          if (message.threadId !== thread) return false;
          if (message.threadType === 'directMessageThread') {
            if (!user) return false;
            return userCanViewDirectMessageThread(message.threadId, user.id);
          }

          const threadData = await getThread(message.threadId);
          if (!threadData) return false;

          return await userCanViewChannel(
            threadData.channelId,
github Svehla / node-graphql-boilerplate / src / gql / models / Comment / subscriptions / newCommentSubscription.ts View on Github external
}
})


export default {
  // auth stuffs
  newComment: {
    type: CommentType,
    args: {
      input: {
        type: NewCommentSubscriptionInput
      }
    },
    // resolve have to be there... dont know why yet
    resolve: data => data,
    subscribe: withFilter(
      () => pubsub.asyncIterator(SubsTypes.NewComment), (payload, variables = {}, context) => {
        // TODO: can't add context in playground and test subscription without frontend
        // disable auth for dev env?
        if (isNilOrEmpty(context.user)) {
          return false
        }
        const globalReportId = variables.input && variables.input.reportId
        const reportId = Number(fromGlobalId(globalReportId).id)
        return payload.report_id === reportId
      }
    ),
  }
}
github mrdulin / apollo-server-express-starter / src / subscription / demo-2 / server.js View on Github external
links: () => db.links
  },
  Mutation: {
    createLink: (_, { url, description }) => {
      const link = { id: shortid.generate(), description, url };
      db.links.push(link);
      pubsub.publish(CHANNEL.LINK, { [SUBSCRIPTION.LINK]: link });
      return link;
    }
  },
  Subscription: {
    [SUBSCRIPTION.LINK]: {
      resolve: (payload, args, context, info) => {
        return payload.link;
      },
      subscribe: withFilter(
        (rootValue, args, context, info) => {
          return pubsub.asyncIterator(CHANNEL.LINK);
        },
        (payload, variables, context, info) => {
          console.log('payload: ', payload);
          console.log('variables: ', variables);
          return true;
        }
      )
    }
  }
};

const schema = makeExecutableSchema({
  typeDefs,
  resolvers
github Thorium-Sim / thorium / server / resolvers / coolant.js View on Github external
(rootValue, { simulatorId }) => {
        if (simulatorId)
          return !!rootValue.find(s => s.simulatorId === simulatorId);
        return true;
      }
    )
  },
  coolantSystemUpdate: {
    resolve(rootValue, { simulatorId, systemId }) {
      let returnRes = rootValue;
      if (simulatorId)
        returnRes = returnRes.filter(s => s.simulatorId === simulatorId);
      if (systemId) returnRes = returnRes.filter(s => s.id === systemId);
      return returnRes;
    },
    subscribe: withFilter(
      () => pubsub.asyncIterator("coolantSystemUpdate"),
      (rootValue, { simulatorId, systemId }) => {
        let returnRes = rootValue;
        if (simulatorId)
          returnRes = returnRes.filter(s => s.simulatorId === simulatorId);
        if (systemId) returnRes = returnRes.filter(s => s.id === systemId);
        return returnRes.length > 0 ? true : false;
      }
    )
  }
};
github Thorium-Sim / thorium / server / src / resolvers / tacticalMap.js View on Github external
},
  showViewscreenTactical(rootValue, args, context) {
    App.handleEvent(args, "showViewscreenTactical", context);
  }
};

export const TacticalMapSubscriptions = {
  tacticalMapsUpdate: {
    resolve(rootValue, { flightId }) {
      let returnRes = rootValue;
      if (flightId) {
        returnRes = returnRes.filter(s => s.flightId === flightId);
      }
      return returnRes;
    },
    subscribe: withFilter(
      () => pubsub.asyncIterator("tacticalMapsUpdate"),
      (rootValue, { flightId }) => {
        if (flightId) {
          return rootValue.filter(s => s.flightId === flightId).length > 0;
        }
        return !!(rootValue && rootValue.length);
      }
    )
  },
  tacticalMapUpdate: {
    resolve(rootValue, { id }) {
      return rootValue;
    },
    subscribe: withFilter(
      () => pubsub.asyncIterator("tacticalMapUpdate"),
      (rootValue, { id }) => {
github Thorium-Sim / thorium / server / resolvers / coreLayouts.js View on Github external
App.handleEvent({ layout }, "updateCoreLayout", context);
  },
  addCoreLayout(_, { layout }, context) {
    App.handleEvent({ layout }, "addCoreLayout", context);
  },
  removeCoreLayout(_, { id }, context) {
    App.handleEvent({ id }, "removeCoreLayout", context);
  }
};

export const CoreLayoutSubscriptions = {
  coreLayoutChange: {
    resolve(rootValue) {
      return rootValue;
    },
    subscribe: withFilter(
      () => pubsub.asyncIterator("coreLayoutChange"),
      rootValue => {
        return !!(rootValue && rootValue.length);
      }
    )
  }
};
github Thorium-Sim / thorium / server / resolvers / thx.js View on Github external
App.handleEvent(args, "activateThx", context);
  },
  deactivateThx(root, args, context) {
    App.handleEvent(args, "deactivateThx", context);
  },
  resetThx(root, args, context) {
    App.handleEvent(args, "resetThx", context);
  }
};

export const ThxSubscriptions = {
  thxUpdate: {
    resolve(rootValue, { simulatorId }) {
      return rootValue.filter(s => s.simulatorId === simulatorId);
    },
    subscribe: withFilter(
      () => pubsub.asyncIterator("thxUpdate"),
      (rootValue, { simulatorId }) => {
        if (simulatorId) {
          return !!rootValue.find(s => s.simulatorId === simulatorId);
        }
        return true;
      }
    )
  }
};
github Thorium-Sim / thorium / server / src / resolvers / transporters.js View on Github external
setTransporterTargets(_, { transporter, targets }, context) {
    App.handleEvent({ transporter, targets }, "setTransporterTargets", context);
    return "";
  },
  setTransporterChargeSpeed(root, args, context) {
    App.handleEvent(args, "setTransporterChargeSpeed", context);
  }
};

export const TransporterSubscriptions = {
  transporterUpdate: {
    resolve(rootValue, { simulatorId }) {
      if (rootValue.simulatorId !== simulatorId) return false;
      return rootValue;
    },
    subscribe: withFilter(
      () => pubsub.asyncIterator("transporterUpdate"),
      rootValue => !!rootValue
    )
  }
};
github Thorium-Sim / thorium / server / resolvers / softwarePanels.js View on Github external
App.handleEvent(args, "updateSoftwarePanel", context);
  },
  removeSoftwarePanel(rootValue, args, context) {
    App.handleEvent(args, "removeSoftwarePanel", context);
  }
};

export const SoftwarePanelsSubscriptions = {
  softwarePanelsUpdate: {
    resolve(rootValue, { simulatorId }) {
      if (simulatorId) {
        return rootValue.filter(s => s.simulatorId === simulatorId);
      }
      return rootValue.filter(s => !s.simulatorId);
    },
    subscribe: withFilter(
      () => pubsub.asyncIterator("softwarePanelsUpdate"),
      rootValue => !!(rootValue && rootValue.length)
    )
  }
};

graphql-subscriptions

GraphQL subscriptions for node.js

MIT
Latest version published 1 month ago

Package Health Score

89 / 100
Full package analysis