GithubHelp home page GithubHelp logo

graylikeme / rxapollo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from scottrhoyt/rxapollo

2.0 2.0 0.0 68 KB

RxSwift extensions for Apollo

License: MIT License

Objective-C 0.98% Swift 97.71% Shell 1.31%

rxapollo's Introduction

RxApollo

RxSwift extensions for Apollo.

Installation

Carthage

github "scottrhoyt/RxApollo"

Manual

Add RxApollo.swift to your project.

Usage

All the reactive extensions are encapsulated in the rx property of an ApolloClient.

import Apollo
import RxSwift
import RxApollo

let apollo: ApolloClient
let disposeBag = DisposeBag()

Fetch

Fetching works just how you would expect it to:

// Let's get our hero's name and print it or the error if there is one.
apollo.rx.fetch(query: HeroNameQuery())
  .map { $0.hero?.name }
  .subscribe(onNext: { heroName in
    print("Our hero's name is \(heroName).")
  }, onError: { error in
    print("Received error: \(error).")
  })
  .disposed(by: disposeBag)

Watch

// Let's watch to see if our hero's name changes and print it or the error if there is one.
apollo.rx.watch(query: HeroNameQuery())
  .map { $0.hero?.name }
  .subscribe(onNext: { heroName in
    print("Our hero's name is \(heroName).")
  }, onError: { error in
    print("Received error: \(error).")
  })
  .disposed(by: disposeBag)

Watching also works quite well with using RxCocoa bindings:

import RxCocoa

let heroField: UITextField

// Let's watch to see if our hero's name changes and set a text field.
apollo.rx.watch(query: HeroNameQuery())
  .map { $0.hero?.name }
  .asDriver(onErrorJustReturn: nil)
  .drive(heroField.rx.text)
  .disposed(by: disposeBag)

Mutate

Mutations follow the same pattern as well:

// Let's upvote a post.
apollo.rx.perform(mutation: UpvotePostMutation(postId: postId))
  .subscribe()
  .disposed(by: disposeBag)

License

MIT

rxapollo's People

Contributors

scottrhoyt avatar graylikeme avatar

Stargazers

Anton Savitskiy avatar Rémi Dayres avatar

Watchers

 avatar 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.