How to use the apollo-server.withFilter function in apollo-server

To help you get started, we’ve selected a few apollo-server 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 nemanjam / rn-chat / server / src / data / resolvers.js View on Github external
const chat = await group.getChat();
          return Boolean(chat.id === payload.messageAdded.chatId);
        },
      ),
    },
    groupAdded: {
      subscribe: withFilter(
        () => pubsub.asyncIterator(GROUP_ADDED_TOPIC),
        (payload, args) => {
          // console.log(JSON.stringify(payload, null, 2));
          return Boolean(true /*args.userId === payload.groupAdded.userId*/);
        },
      ),
    },
    messageInGroupAdded: {
      subscribe: withFilter(
        () => pubsub.asyncIterator(MESSAGE_IN_GROUP_ADDED_TOPIC),
        (payload, args) => {
          console.log(JSON.stringify(payload, null, 2));
          return Boolean(
            true /*args.userId === payload.defaultGroupAdded.userId*/,
          );
        },
      ),
    },
  },

  Mutation: {
    async addFriend(_, { userId, friendId }, ctx) {
      const user = await UserModel.findOne({
        where: { id: userId },
      });
github ERS-HCL / nxplorerjs-microservice-starter / server / graphql / resolvers / blog.resolver.ts View on Github external
text: comment.text,
        createdAt: Date()
      };
      blog.comments.push(newComment);

      pubsub.publish('commentAdded', {
        commentAdded: newComment,
        blogId: comment.blogId
      });

      return newComment;
    }
  },
  Subscription: {
    commentAdded: {
      subscribe: withFilter(
        () => pubsub.asyncIterator('commentAdded'),
        (payload, variables) => {
          // The `commentAdded` blog includes events for all blogs, so we filter to only
          // pass through events for the blogs specified in the query
          return payload.blogId === variables.blogId;
        }
      )
    }
  }
};
github LFSCamargo / Chatify / server / src / resolvers.ts View on Github external
interface MessageReceived {
  chat: Chat
  notificationMessage: string
  fromUser: string
}

interface MessageReceivedArgs {
  yourUser: string
}

export default {
  Subscription: {
    messageReceived: {
      resolve: (obj: MessageReceived) => obj,
      subscribe: withFilter(
        (root, args, { pubsub }: GraphQLContext) => pubsub.asyncIterator(SUBSCRIPTION_TRIGGERS.MESSAGE_RECEIVED),
        (obj: MessageReceived, args: MessageReceivedArgs) => {
          // @ts-ignore
          return obj.fromUser.toString() !== args.yourUser && obj.chat.users.includes(args.yourUser)
        },
      ),
    },
  },
  Chat: {
    ...chatResolvers.Chat
  },
	Query: {
    ...userResolvers.Query,
    ...chatResolvers.Query,
	},
	Mutation: {
github DimiMikadze / create-social-network / api / resolvers / user.js View on Github external
return updatedUser;
    }

    throw new Error(
      'Something went wrong while uploading image to Cloudinary.'
    );
  },
};

const Subscription = {
  /**
   * Subscribes to user's isOnline change event
   */
  isUserOnline: {
    subscribe: withFilter(
      () => pubSub.asyncIterator(IS_USER_ONLINE),
      (payload, variables, { authUser }) => variables.authUserId === authUser.id
    ),
  },
};

export default { Query, Mutation, Subscription };
github dooboolab / hackatalk-server / src / resolvers / user.ts View on Github external
raw: true,
        });

        pubsub.publish(USER_UPDATED, { user });
        return user;
      } catch (err) {
        throw new Error(err);
      }
    },
  },
  Subscription: {
    userAdded: {
      subscribe: (_, args, { pubsub }) => pubsub.asyncIterator(USER_ADDED),
    },
    userUpdated: {
      subscribe: withFilter(
        (_, args, { pubsub }) => pubsub.asyncIterator(USER_UPDATED),
        (payload, variables) => {
          return payload.userUpdated.id === variables.id;
        },
      ),
    },
  },
  User: {
    notifications: (_, args, { models }, info) => {
      return models.Notification.findAll({
        where: {
          userId: _.id,
        },
      });
    },
    reviews: (_, args, { models }, info) => {
github DimiMikadze / create-social-network / api / resolvers / notification.js View on Github external
{ multi: true }
      );

      return true;
    } catch (e) {
      return false;
    }
  },
};

const Subscription = {
  /**
   * Subscribes to notification created or deleted event
   */
  notificationCreatedOrDeleted: {
    subscribe: withFilter(
      () => pubSub.asyncIterator(NOTIFICATION_CREATED_OR_DELETED),
      (payload, variables, { authUser }) => {
        const userId = payload.notificationCreatedOrDeleted.notification.user.toString();

        return authUser && authUser.id === userId;
      }
    ),
  },
};

export default { Query, Mutation, Subscription };
github vineeshvk / chat-app-server / src / schema / resolvers / messageResolvers.ts View on Github external
function getNewMessages() {
  return withFilter(
    () => pubSub.asyncIterator(GET_CHAT_SUB),
    (payload, variable) => payload.chatId === variable.chatId
  );
}
github DimiMikadze / create-social-network / api / resolvers / message.js View on Github external
const isAuthUserSenderOrReceiver =
          authUserId === sender.id || authUserId === receiver.id;
        const isUserSenderOrReceiver =
          userId === sender.id || userId === receiver.id;

        return isAuthUserSenderOrReceiver && isUserSenderOrReceiver;
      }
    ),
  },

  /**
   * Subscribes to new conversations event
   */
  newConversation: {
    subscribe: withFilter(
      () => pubSub.asyncIterator(NEW_CONVERSATION),
      (payload, variables, { authUser }) =>
        authUser && authUser.id === payload.newConversation.receiverId
    ),
  },
};

export default { Mutation, Query, Subscription };
github netgroup-polito / CrownLabs / qlkube / src / decorateSubscription.js View on Github external
targetType = capitalizeType(targetType);

  const extension = gql`
  type  ${subscriptionType} {
    updateType: ${enumType}
    payload: ${targetType}
  }
    ${subType} {
      ${subscriptionField}(name: String, namespace: String!): ${subscriptionType}
    }
  `;

  const resolvers = {
    Subscription: {
      [subscriptionField]: {
        subscribe: withFilter(
          () => pubsubAsyncIterator(label),
          (payload, variables, info, context) => {
            const resourceApi = subscriptions.filter(sub => {
              return `${sub.type}Update` === context.fieldName;
            })[0];
            return (
              payload.apiObj.metadata.namespace === variables.namespace &&
              (variables.name === undefined ||
                payload.apiObj.metadata.name === variables.name) &&
              checkPermission(
                info.token,
                resourceApi.group,
                resourceApi.resource,
                variables.namespace,
                variables.name,
                kubeApiUrl