Git Product home page Git Product logo

graphql-java-reactive's Introduction

graphql-java-reactive Build Status

Reactive Streams-based execution strategy for http://github.com/graphql-java/graphql-java.

Table of Contents

Overview

While GraphQL allows you to define very rich queries with multiple levels of depth, it is not possible to track the data changes efficiently. Once you received the data, you have to poll your GraphQL API. Moreover, even if a single field changed the whole object will be sent.

Subscription model helps to avoid it, and instead of sending the data it sends the changes.

Hello World

This is "hello world" in graphql-java-reactive:

GraphQLSchema schema = newSchema()
        .query(newObject()
                .name("helloWorldQuery")
                .field(newFieldDefinition()
                        .type(GraphQLString)
                        .name("hello")
                        .dataFetcher(env -> Flowable
                                .interval(1, TimeUnit.SECONDS)
                                .take(3)
                                .map(i -> "world " + i))
                )
        )
        .build();

GraphQL graphQL = new GraphQL(schema, new ReactiveExecutionStrategy());

Publisher<Change> changes = (Publisher<Change>) graphQL.execute("{ hello }").getData();

Flowable.fromPublisher(changes).timeInterval().blockingSubscribe(System.out::println);
// Prints:
// Timed[time=1017, unit=MILLISECONDS, value=Change{path=[], data={hello=world 0}}]
// Timed[time=1000, unit=MILLISECONDS, value=Change{path=[hello], data=world 1}]
// Timed[time=1000, unit=MILLISECONDS, value=Change{path=[hello], data=world 2}]

As you can see, initial object was dispatched as a change with an empty path.
Since hello is defined as an asynchronous publisher with 1 second interval, we received it after 1 second.

Every other change is more specific and changes only hello property.

P.S. This example uses RxJava 2, but you can use any Reactive Streams compatible implementation like Project Reactor, RxJava 1, etc...

Migration

Notable change from the standard execution strategy is that the result is not a value, but Publisher of changes. However, first change always contains fully resolved object.
It means that you can use .take(1).map(it -> it.getData()) to emulate old behavior, but with Reactive types.

DataFetcher might return non-Reactive type. By default only CompletableFuture will be treated as asynchronous type (you can override adapt method from ReactiveExecutionStrategy to add your own types), every other type will be considered as a static value.

License

graphql-java-reactive is licensed under the MIT License. See LICENSE for details.

Copyright (c) 2017, Sergei Egorov and Contributors

graphql-java-reactive's People

Contributors

bsideup avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.