npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

apollo-link-observable

v0.0.1

Published

Link that allows you to make side effects of graphql queries using RxJS

Downloads

4

Readme

apollo-link-observable

Link that allows you to make side effects of graphql queries using RxJS

Motivation

  1. Organization of application side effects such as logging, accessing the browser cache, recording analytics events etc.
  2. A part of the application with longer lifecycle than a component's one.
  3. Creating a complex application business logic.
  4. Performing REST queries as a reaction to the graphql query.
  5. Local state changes that are triggered by graphql queries.

Installation

using npm

npm install apollo-link-observable

or using yarn

yarn add apollo-link-observable

Usage

  1. Creation of the "effect" function

    Effect - is a function, which takes a stream of operations and returns a modified one.

    Example of the effect which logs names of operations

import { tap } from 'rxjs/operators';
import { Effect } from 'apollo-link-observable';

export const logEffect: Effect = (operations$) =>
    operations$.pipe(tap((operation) => console.log(operation.operationName)));
  1. Сreate a link

    To create a link you have to provide your effect function as the rootEffect property of the ApolloLinkObservable constructor's configuration object.

import { ApolloLinkObservable } from 'apollo-link-observable';
import { logEffect } from './log-effect';

const link = new ApolloLinkObservable({ rootEffect: logEffect });
  1. Merge effects

    If you have more than one effect you can merge them into the single root effect using mergeEffects helper from the apollo-link-observable package.

const rootEffect = mergeEffects([myLogEffect, myAnalyticsEffect]);
const link = new ApolloLinkObservable({ rootEffect });
  1. Lazy loading effects

    If you need asynchronous loading for your effects you can do this like this:

import { Subject, of } from 'rxjs';
import { mergeMap, mergeAll } from 'rxjs/operators';
import { Effect } from 'apollo-link-observable';
import { cacheEffect, logEffect } from './effects';

const lazyLoadEffects = new Subject<Effect>();

const rootEffect: Effect = (operations$) =>
    of(of(logEffect, cacheEffect), lazyLoadEffects.asObservable()).pipe(
        mergeAll(),
        mergeMap((effect) => effect(operations$)),
    );

// assynchronous loading of an effect
import { lazyEffect } from './lazy-effect';
lazyLoadEffects.next(lazyEffect);
  1. Filtering operations by a directive

    By default you can create side effects of operations, which have effect directive. For example,

const [myMutation] = useMutation(
    gql`
        mutation MyMutation($data: String!) {
            myMutation(data: $data) @effect
        }
    `,
);

You can change this behaviour. To do this provide a new directive name as the directiveName property of the ApolloLinkObservable constructor's configuration object.

const link = new ApolloLinkObservable({
    rootEffect,
    directiveName: 'myDirective',
});

Pass to the directiveName null if it's necessary to turn off the operation filtration.

const link = new ApolloLinkObservable({
    rootEffect,
    directiveName: null,
});