All Projects → DxCx → Graphql Rxjs

DxCx / Graphql Rxjs

Licence: other
fork of Graphql which adds Observable support

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Graphql Rxjs

Aws Mobile Appsync Sdk Js
JavaScript library files for Offline, Sync, Sigv4. includes support for React Native
Stars: ✭ 806 (+933.33%)
Mutual labels:  graphql, graphql-js, graphql-subscriptions
Grial
A Node.js framework for creating GraphQL API servers easily and without a lot of boilerplate.
Stars: ✭ 194 (+148.72%)
Mutual labels:  graphql, graphql-js, graphql-subscriptions
Game Music Player
All your music are belong to us
Stars: ✭ 76 (-2.56%)
Mutual labels:  rxjs, observables
Type Graphql
Create GraphQL schema and resolvers with TypeScript, using classes and decorators!
Stars: ✭ 6,864 (+8700%)
Mutual labels:  graphql, graphql-js
Graphql Yoga
🧘 Fully-featured GraphQL Server with focus on easy setup, performance & great developer experience
Stars: ✭ 6,573 (+8326.92%)
Mutual labels:  graphql, graphql-subscriptions
Graphql Query Complexity
GraphQL query complexity analysis and validation for graphql-js
Stars: ✭ 424 (+443.59%)
Mutual labels:  graphql, graphql-js
Graphql Iso Date
A set of RFC 3339 compliant date/time GraphQL scalar types.
Stars: ✭ 480 (+515.38%)
Mutual labels:  graphql, graphql-js
React App
Create React App with server-side code support
Stars: ✭ 614 (+687.18%)
Mutual labels:  graphql, graphql-js
Kikstart Graphql Client
🚀 Small NodeJS Wrapper around apollo-client that provides easy access to running queries, mutations and subscriptions.
Stars: ✭ 27 (-65.38%)
Mutual labels:  graphql, graphql-subscriptions
Platform
Reactive libraries for Angular
Stars: ✭ 7,020 (+8900%)
Mutual labels:  rxjs, observables
Tutorial Graphql Subscriptions Redis
GraphQL server implementation with Redis backing allowing pubsub
Stars: ✭ 12 (-84.62%)
Mutual labels:  graphql, graphql-subscriptions
Graphql Tools
🔧 Build, mock, and stitch a GraphQL schema using the schema language
Stars: ✭ 4,556 (+5741.03%)
Mutual labels:  graphql, graphql-js
Typegql
Create GraphQL schema with TypeScript classes.
Stars: ✭ 415 (+432.05%)
Mutual labels:  graphql, graphql-js
Graphql Cost Analysis
A Graphql query cost analyzer.
Stars: ✭ 527 (+575.64%)
Mutual labels:  graphql, graphql-js
Graphql Ws
Coherent, zero-dependency, lazy, simple, GraphQL over WebSocket Protocol compliant server and client.
Stars: ✭ 398 (+410.26%)
Mutual labels:  graphql, observables
Graphql Core
A Python 3.6+ port of the GraphQL.js reference implementation of GraphQL.
Stars: ✭ 344 (+341.03%)
Mutual labels:  graphql, graphql-js
Inferno Most Fp Demo
A demo for the ReactJS Tampa Bay meetup showing how to build a React+Redux-like architecture from scratch using Inferno, Most.js, reactive programmning, and various functional programming tools & techniques
Stars: ✭ 45 (-42.31%)
Mutual labels:  rxjs, observables
Graphql Js
A reference implementation of GraphQL for JavaScript
Stars: ✭ 18,251 (+23298.72%)
Mutual labels:  graphql, graphql-js
Learn Rxjs
Clear examples, explanations, and resources for RxJS
Stars: ✭ 3,475 (+4355.13%)
Mutual labels:  rxjs, observables
Graphql Redis Subscriptions
A graphql subscriptions implementation using redis and apollo's graphql-subscriptions
Stars: ✭ 829 (+962.82%)
Mutual labels:  graphql, graphql-subscriptions

GraphQL-RxJs

fork of graphql-js which adds AsyncIterator & Observable support (RxJs).

npm version Build Status

Intro

This package adds Reactivity for GraphQLResolver, Which means you can now return:

  • Observables
  • AsyncIterator

This package also adds reactive directives support:

The package is pretty small because it is importing the original graphql-js package, and then patches it to provide a reactive execution engine over it.

Examples

Documentation

There isn't much to document, all of GraphQL documentation is relevant. See the complete documentation at http://graphql.org/ and http://graphql.org/graphql-js/.

Versioning

I'll try to follow along with graphql-js versions, so basiclly, each graphql-js version should have a working graphql-rxjs package with it.

API

The library exports the following functions:

AsyncIterator support

export function graphqlReactive(
  schema: GraphQLSchema,
  requestString: string,
  rootValue?: any,
  contextValue?: any,
  variableValues?: {[key: string]: any},
  operationName?: string
  fieldResolver?: GraphQLFieldResolver<any, any>,
): AsyncIterator<ExecutionResult>;

export function executeReactive(
  schema: GraphQLSchema,
  document: DocumentNode,
  rootValue?: any,
  contextValue?: any,
  variableValues?: {[key: string]: any},
  operationName?: string
  fieldResolver?: GraphQLFieldResolver<any, any>,
): AsyncIterator<ExecutionResult>;

The signature is equal to GraphQL original implementation (graphql + execute), except it returns an asyncIterator instead of a promise. The asyncIterator will stream immutable results.

Observable support

export function graphqlRx(
  schema: GraphQLSchema,
  requestString: string,
  rootValue?: any,
  contextValue?: any,
  variableValues?: {[key: string]: any},
  operationName?: string
  fieldResolver?: GraphQLFieldResolver<any, any>,
): Observable<ExecutionResult>;

export function executeRx(
  schema: GraphQLSchema,
  document: DocumentNode,
  rootValue?: any,
  contextValue?: any,
  variableValues?: {[key: string]: any},
  operationName?: string
  fieldResolver?: GraphQLFieldResolver<any, any>,
): Observable<ExecutionResult>;

export function subscribeRx(
  schema: GraphQLSchema,
  document: DocumentNode,
  rootValue?: any,
  contextValue?: any,
  variableValues?: {[key: string]: any},
  operationName?: string,
  fieldResolver?: GraphQLFieldResolver<any, any>,
  subscribeFieldResolver?: GraphQLFieldResolver<any, any>
): Observable<ExecutionResult>;

The signature is equal to GraphQL original implementation (graphql + execute + subscribe), except it returns an observable instead of a promise. The observable will stream immutable results.

Preparing schema for GraphQL-RxJs

export function prepareSchema(
  schema: GraphQLSchema,
): prepareSchema;

This function is used to prepare schema for graphql-rxjs. wrapping resolvers, adding reactive directives support, etc.. At the moment, it will be automatically invoked when running GraphQL-RxJS.

NOTE: if you are using original graphql's validate, you will have to trigger prepareSchema manually and not count on auto-trigger.

if you don't want to call it directly, you can use the inner APIs seperately:

Reactive Directives
export function addReactiveDirectivesToSchema(
  schema: GraphQLSchema,
): void;

Calling this function on your existing GraphQLSchema object will enable reactive directives suppot for the schema. More information about reactive directives can be found below.

Observable support in resolvers
export function wrapResolvers(
  schema: GraphQLSchema,
): void;

Calling this function on your existing GraphQLSchema object will enable reactive directives suppot for the schema. More information about reactive directives can be found below.

Getting Started:

  1. The Data source

Let's start off by explaining our Observable, or Data-source that we are going to stream.

const clockSource = Observable.interval(1000).map(() => new Date()).publishReplay(1).refCount();

We can see that it is an Observable that emits i+1 every second:

let source = Observable.interval(1000);

Next we are going to map the value to a the current timestep:

source = source.map(() => new Date());

Finally, We are going to convert the observable into an hot observable, or multicast.

This means that there is a ref count on the observable, only once the first subscriber subscribe, the provider function will be triggered, and from then on, each subscriber will get the last value (without triggering the provider function) and then, both subscribers will get next values.

Once a subscriber unsubscribe, the refCount will decrease, until the counter reachs zero, and only then the unsubscribe function will be triggered.

const clockSource = source.publishReplay(1).refCount();

This approach will let us manage resources much more efficiently.

  1. The Scheme

Next, let's look at our scheme

# Root Query
type Query {
  someInt: Int
}

# Root Subscription
type Subscription {
  clock: String
}

Query type exposes someInt, just because it cannot be empty, let's ignore that.

Subscription type expoes clock which is basiclly periodic timestamp strings.

  1. Wrapping them togather

So, here is a basic code to demonstrate the concept:

import { Observable } from 'rxjs';
import { makeExecutableSchema } from 'graphql-tools';
import { prepareSchema, graphqlRx } from 'graphql-rxjs';

const clockSource = Observable.interval(1000).map(() => new Date()).publishReplay(1).refCount();

const typeDefs = `
# Root Query
type Query {
  someInt: Int
}

# Root Subscription
type Subscription {
  clock: String
}
`;

const resolvers = {
    Subscription: {
        clock(root, args, ctx) {
              return ctx.clockSource;
        },
    },
};

// Compose togather resolver and typeDefs.
const scheme = makeExecutableSchema({typeDefs: typeDefs, resolvers: resolvers});
prepareSchema(schema);

// subscribe the clock
const query = `
  subscription {
	clock
  }
`

// Calling the reactive version of graphql
graphqlRx(scheme, query, null, { clockSource })
.subscribe(console.log.bind(console), console.error.bind(console));

The following response will emit in console:

{"data":{"clock":"Fri Feb 02 2017 20:28:01 GMT+0200 (IST)"}}
{"data":{"clock":"Fri Feb 02 2017 20:28:02 GMT+0200 (IST)"}}
{"data":{"clock":"Fri Feb 02 2017 20:28:03 GMT+0200 (IST)"}}
...

Reactive Directives

This library also implements reactive directives, those are supported at the moment:

  1. GraphQLDeferDirective (@defer)
  • This directive does not require any arguments.

  • This directive instructs the executor to not resolve this field immedaitly, but instead returning the response without the deferred field, once the field is deferred, it will emit a corrected result.

  • executor will ignore this directive if resolver for this value is not async.

  • can be applied on:

    • specific field
    • spread fragment
    • named fragment
  • Example:

    import { Observable } from 'rxjs';
    import { makeExecutableSchema } from 'graphql-tools';
    import { prepareSchema, graphqlReactive } from 'graphql-rxjs';
    
    const remoteString = new Promise((resolve, reject) => {
      setTimeout(() => resolve('Hello World!'), 5000);
    });
    
    const typeDefs = `
    # Root Query
    type Query {
      remoteString: String
    }
    `;
    
    const resolvers = {
        Query: {
          remoteString: (root, args, ctx) => ctx.remoteString,
        },
    };
    
    const scheme = makeExecutableSchema({typeDefs, resolvers});
    prepareSchema(scheme);
    
    const query = `
      query {
        remoteString @defer
      }
    `;
    
    const log = (result) => console.log("[" + (new Date).toLocaleTimeString() + "] " + JSON.stringify(result));
    
    graphqlReactive(scheme, query, null, { remoteString })
    .subscribe(log, console.error.bind(console));
    

    The following response will emit in console:

    [8:58:05 PM] {"data":{}}
    [8:58:10 PM] {"data":{"remoteString":"Hello World!"}}
    
  1. GraphQLLiveDirective (@live)
  • This directive does not require any arguments.

  • This directive instructs the executor that the value should be monitored live which means that once updated, it will emit the updated respose.

  • executor will ignore this directive if field is not resolved with an observable (or at least have a parent observable).

  • can be applied on:

    • specific field
    • spread fragment
    • named fragment
    • fragment definition - (Live Fragment)
  • Example:

    import { Observable } from 'rxjs';
    import { makeExecutableSchema } from 'graphql-tools';
    import { prepareSchema, graphqlReactive } from '..';
    
    const clockSource = Observable.interval(1000).map(() => new Date()).publishReplay(1).refCount();
    
    const typeDefs = `
    # Root Query
    type Query {
      clock: String
    }
    `;
    
    const resolvers = {
        Query: {
          clock: (root, args, ctx) => ctx.clockSource,
        },
    };
    
    const scheme = makeExecutableSchema({typeDefs, resolvers});
    prepareSchema(scheme);
    
    const query = `
      query {
        clock
      }
    `;
    
    const liveQuery = `
      query {
        clock @live
      }
    `;
    
    graphqlReactive(scheme, query, null, { clockSource })
    .subscribe(console.log.bind(console, "standard: "), console.error.bind(console));
    
    graphqlReactive(scheme, liveQuery, null, { clockSource })
    .subscribe(console.log.bind(console, "live: "), console.error.bind(console));
    

    The following response will emit in console:

    standard:  { data: { clock: 'Sun Apr 16 2017 21:04:57 GMT+0300 (EEST)' } }
    live:  { data: { clock: 'Sun Apr 16 2017 21:04:57 GMT+0300 (EEST)' } }
    live:  { data: { clock: 'Sun Apr 16 2017 21:04:58 GMT+0300 (EEST)' } }
    live:  { data: { clock: 'Sun Apr 16 2017 21:04:59 GMT+0300 (EEST)' } }
    live:  { data: { clock: 'Sun Apr 16 2017 21:05:00 GMT+0300 (EEST)' } }
    ...
    

Typescript support

Just install @types/graphql for initial GraphQL support, then the package will automatically add typings for the new functions it provides.

Benchmarks

      * graphqlOri x 7,496 ops/sec ±9.20% (78 runs sampled)
      * graphqlRx x 6,790 ops/sec ±3.48% (75 runs sampled)
      => Fastest is graphqlOri
    ✓ compare performance for simple query (11088ms)

      * graphqlOri x 3,040 ops/sec ±9.81% (70 runs sampled)
      * graphqlRx x 1,303 ops/sec ±4.19% (74 runs sampled)
      => Fastest is graphqlOri
    ✓ compare performance for deep query (17515ms)
    
      * graphqlOri x 5,360 ops/sec ±20.55% (12 runs sampled)
      * graphqlRx x 3,067 ops/sec ±32.41% (10 runs sampled)
      => Fastest is graphqlOri
    ✓ compare performance for serial mutation (27004ms)

as results shows above, the reactive engine is abit slower then the original one, however that was expected, i have yet to put the effort on optimizations, and i am planing to optimize on the future.

Issues

If you found an issue or have an idea, you are welcome to open a new ticket in Issues Page

Support

Using this approach, feels much more intuative then the other approaches to stream results so far. Because of that I tried to push it into upstream graphql-js but got rejected, if you want to support the project you can follow/thumbs up the following:

  1. Issue on graphql-js
  2. PR on graphql-js
  3. Draft design for apollo subscriptions

Contributing

All pull requests are welcome, if you think something needs to be done, just open an issue or a PR :)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].